Pull plugin "com.google.cloud.artifactregistry.gradle-plugin" fail because "Failed to get access token from gcloud or Application Default Credentials"

Viewed 728

I included plugin "com.google.cloud.artifactregistry.gradle-plugin" in my gradle project.

plugins {
......
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.1"
}

I always get an error message during pulling:

Failed to apply plugin 'com.google.cloud.artifactregistry.gradle-plugin'.
Failed to get access token from gcloud or Application Default Credentials

Anyone knows why? I see it mentioned in this post https://github.com/GoogleCloudPlatform/artifact-registry-maven-tools/issues/34. that I need to have a GCP account first. I downloaded the GCP SDK, and logged in to my account, but still get the same message. Anyone can provide more specific solution? Thanks.

2 Answers

I've not tried this

Have a look at Setting up Authentication for Grade in Google's Artifact Registry docs.

Hopefully you can use the credential helper

Application Default Credentials is a very useful Google platform feature that simplifies obtaining credentials. Code (not gcloud) running:

  • off-GCP (locally or e.g. GitHub|GitLab) can be configured to use credentials through an environment variable setting (GOOGLE_APPLICATION_CREDENTIALS)
  • on-GCP (e.g. Compute Engine) obtains the credentials automatically (e.g. Metadata service).

I suspect (!) that the Maven|Gradle plugins looks for GOOGLE_APPLICATION_CREDENTIALS in this way to authenticate (using a Service Account) your script to Google.

Update: Example

BILLING=[[YOUR-BILLING]]
PROJECT=[[YOUR-PROJECT]]
REPO=[[YOUR-REPO]] # E.g. repo
LOCATION=[[YOUR-LOCATION]] # E.g. us-west2

gcloud projects create ${PROJECT}

gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}

# Enable Artifact Registry
gcloud services enable artifactregistry.googleapis.com \
--project=${PROJECT}

# Create Maven repository
gcloud artifacts repositories create ${REPO} \
--location=${LOCATION} \
--repository-format=maven \
--project=${PROJECT}

# Create Service Account to be used by gradle
ACCOUNT="gradle"
EMAIL=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com

gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}

gcloud iam service-accounts keys create ${PWD}/${ACCOUNT}.json \
--iam-account=${EMAIL}

# Export the Service Account key as Application Default Creds
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/${ACCOUNT}.json

# Grant the Service Account permissions to Artifact Registry
gcloud projects add-iam-policy-binding ${PROJECT} \
--member=serviceAccount:${EMAIL} \
--role=roles/artifactregistry.admin

# Get the Gradle settings
# You'll need to manually merge these
gcloud artifacts print-settings gradle \
--project=${PROJECT} \
--repository=${REPO} \
--location=${LOCATION}

I use ./gradlew init to create a basic Java app. I then tweaked the build.grade. I've not used Gradle much at all and so apologies if this is incorrect:

plugins {
    id 'java'
    id 'application'
    id "maven-publish"
    id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.1"
}

publishing {
  publications {
    maven(MavenPublication) {
      groupId = "com.dazwilkin.test"
      artifactId = "application"
      version = "0.1"

      from components.java
    }
  }
  repositories {
    maven {
      url "artifactregistry://${LOCATION}-maven.pkg.dev/${PROJECT}/${REPO}"
    }
  }
}

repositories {
    jcenter()
    maven {
      url "artifactregistry://${LOCATION}-maven.pkg.dev/${PROJECT}/${REPO}"
    }
}

dependencies {
    implementation 'com.google.guava:guava:28.0-jre'
    testImplementation 'junit:junit:4.12'
}

application {
    mainClassName = 'com.dazwilkin.test.App'
}

Then:

./gradlew publish

BUILD SUCCESSFUL in 3s
5 actionable tasks: 3 executed, 2 up-to-date

And:

gcloud artifacts packages list \
--repository=${REPO} \
 --location=${LOCATION} \
--project=${PROJECT}

Yields:

Listing items under project [[PROJECT]], location [[LOCATION]], repository [[REPO]].

PACKAGE: com.dazwilkin.test:application
CREATE_TIME: 2021-12-09T17:34:19
UPDATE_TIME: 2021-12-09T17:44:20
Related