简体   繁体   中英

How to download jar from artifact registry (GCP)?

I have a maven Artifact Registry and am able to add dependency in pom.xml and get the jar.

I have another usecase where I would like to only download the jar using CLI something which you can easily do with other external maven repos eg curl https://repo1.maven.org/maven2/org/apache/iceberg/iceberg-spark-runtime/0.7.0-incubating/iceberg-spark-runtime-0.7.0-incubating.jar --output temp.jar

I don't see any instructions about how to do this.

I needed this too. I have configured a service account following gcp guide

Then, I have executed the following command to get authbasic credz:

gcloud artifacts print-settings gradle \
    [--project=PROJECT] \
    [--repository=REPOSITORY] \
    [--location=LOCATION] \
    --json-key=KEY-FILE \
    [--version-policy=VERSION-POLICY] \
    [--allow-snapshot-overwrites]

In the output you have the artifactRegistryMavenSecret.

Finally you get your artifact with:

curl -L -u _json_key_base64:{{ artifactRegistryMavenSecret }} https://{{ region }}-maven.pkg.dev/{{ projectId }}/{{ repository }}/path/of/artifact/module/{{ version }}/app-{{ version }}.jar -o file.jar

It seems like this feature as mentioned does not exist yet for Artifact Registry based on this open feature request (this feature request has currently no ETA). However, you can try to implement a Cloud build automation not to only save your built artifact in Artifact Registry, but also to store them in Google Cloud Storage or other Storage repositories; so you can easily access the JARs (since Cloud Storage supports direct downloading).

In order to do this, you would need to integrate Cloud Build with Artifact Registry. The documentation page has instructions to use Maven projects with Cloud Build and Artifact Registry. In addition, you can configure Cloud Build to store built artifacts in Cloud Storage .

Both of these integrations are configured through a Cloud Build configuration file . In this file, the steps for building a project are defined, including integrations to other serverless services. This integration would involve defining a target Maven repository:

steps:
- name: gcr.io/cloud-builders/mvn
  args: ['deploy']

And a location to deploy the artifacts into Cloud Storage:

artifacts:
  objects:
    location: [STORAGE_LOCATION]
    paths: [[ARTIFACT_PATH],[ARTIFACT_PATH], ...]

Additional to @Nicolas Roux's answer:

artifactRegistryMavenSecret is basically an encode64 of the Service Account json key.

So instead of runnig gcloud artifacts print-settings gradle and curl -u _json_key_base64:{{ artifactRegistryMavenSecret }} , another way is you can directly use the token from gcloud auth print-access-token , then apply this token to cURL .

For example:

1. gcloud auth activate-service-account SERVICE_ACCOUNT@DOMAIN.COM \
        --key-file=/path/key.json --project=PROJECT_ID
2. curl --oauth2-bearer "$(gcloud auth print-access-token)" \
        -o app-{{ version }}.jar \
        -L https://{{ region }}-maven.pkg.dev/{{ projectId }}/{{ repository }}/path/of/artifact/module/{{ version }}/app-{{ version }}.jar

By that, if you're working with Google Auth Action (google-github-actions/auth@v0) in Github Actions Workflow, then you can easily run the curl command without needing to extract artifactRegistryMavenSecret .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM