doc/user/project/integrations/google_artifact_management.md
{{< details >}}
{{< /details >}}
{{< history >}}
google_cloud_support_feature_flag. This feature is in beta.google_cloud_support_feature_flag removed.{{< /history >}}
You can use the Google Artifact Management integration to configure and connect a Google Artifact Registry repository to your GitLab project.
After you connect the Google Artifact Registry to your project, you can view, push, and pull Docker and OCI images in a Google Artifact Registry repository.
Prerequisites:
To connect a Google Artifact Registry repository to a GitLab project:
You should now see a Google Artifact Registry entry in the sidebar under Deploy.
Prerequisites:
To view the list of images in the connected Artifact Registry repository in the GitLab UI:
After the Artifact Registry integration is activated, the following predefined environment variables are available in CI/CD. You can use these environment variables to interact with the Artifact Registry, like pulling or pushing an image to the connected repository.
| Variable | GitLab | Runner | Description |
|---|---|---|---|
GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID | 16.10 | 16.10 | The Google Cloud project ID where the Artifact Registry repository is located. |
GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME | 16.10 | 16.10 | The name of the connected Artifact Registry repository. |
GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION | 16.10 | 16.10 | The Google Cloud location of the connected Artifact Registry repository. |
You can configure a pipeline to authenticate with the Google Artifact Registry during pipeline
execution. GitLab uses the configured workload identity pool IAM policies
and populates the GOOGLE_APPLICATION_CREDENTIALS and CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE
environment credentials. These environment credentials are automatically detected by client tools,
like gcloud CLI and crane.
To authenticate with the Google Artifact Registry, in the project's .gitlab-ci.yml file, use the identity keyword set to google_cloud.
Your Google Cloud project must have specific IAM policies to use the Google Artifact Management integration. When you set up this integration, on-screen instructions provide the steps to create the following IAM policies in your Google Cloud project:
To create these IAM policies manually, use the following gcloud commands. Replace these values:
<your_google_cloud_project_id> with the ID of the Google Cloud project where the Artifact Registry repository is located.<your_workload_identity_pool_id> with the ID of the workload identity pool. This is the same value used for the Google Cloud IAM integration.<your_google_cloud_project_number> with the number of the Google Cloud project where the workload identity pool is located. This is the same value used for the Google Cloud IAM integration.gcloud projects add-iam-policy-binding '<your_google_cloud_project_id>' \
--member='principalSet://iam.googleapis.com/projects/<your_google_cloud_project_number>/locations/global/workloadIdentityPools/<your_workload_identity_pool_id>/attribute.guest_access/true' \
--role='roles/artifactregistry.reader'
gcloud projects add-iam-policy-binding '<your_google_cloud_project_id>' \
--member='principalSet://iam.googleapis.com/projects/<your_google_cloud_project_number>/locations/global/workloadIdentityPools/<your_workload_identity_pool_id>/attribute.developer_access/true' \
--role='roles/artifactregistry.writer'
For a list of available claims, see OIDC custom claims.
list-images:
image: gcr.io/google.com/cloudsdktool/google-cloud-cli:466.0.0-alpine
identity: google_cloud
script:
- gcloud artifacts docker images list $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev/$GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID/$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME/app
list-images:
image:
name: gcr.io/go-containerregistry/crane:debug
entrypoint: [""]
identity: google_cloud
before_script:
# Temporary workaround for https://github.com/google/go-containerregistry/issues/1886
- wget -q "https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/download/v2.1.22/docker-credential-gcr_linux_amd64-2.1.22.tar.gz" -O - | tar xz -C /tmp && chmod +x /tmp/docker-credential-gcr && mv /tmp/docker-credential-gcr /usr/bin/
- docker-credential-gcr configure-docker --registries=$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev
script:
- crane ls $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev/$GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID/$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME/app
The following example shows how to set up authentication for Docker with the standalone Docker credential helper provided by Google.
pull-image:
image: docker:24.0.5-cli
identity: google_cloud
services:
- docker:24.0.5-dind
variables:
# The following two variables ensure that the DinD service starts in TLS
# mode and that the Docker CLI is properly configured to communicate with
# the API. More details about the importance of this can be found at
# https://docs.gitlab.com/ci/docker/using_docker_build/#use-the-docker-executor-with-docker-in-docker
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
before_script:
- wget -q "https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/download/v2.1.22/docker-credential-gcr_linux_amd64-2.1.22.tar.gz" -O - | tar xz -C /tmp && chmod +x /tmp/docker-credential-gcr && mv /tmp/docker-credential-gcr /usr/bin/
- docker-credential-gcr configure-docker --registries=$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev
script:
- docker pull $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev/$GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID/$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME/app:v0.1.0
Google provides the upload-artifact-registry CI/CD component, which you can use to copy an image from the GitLab container registry to Artifact Registry.
To use the upload-artifact-registry component, add the following to your .gitlab-ci.yml:
include:
- component: gitlab.com/google-gitlab-components/artifact-registry/upload-artifact-registry@main
inputs:
stage: deploy
source: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
target: $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev/$GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID/$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME/$CI_PROJECT_NAME:$CI_COMMIT_SHORT_SHA
For details, see the component documentation.
Using the upload-artifact-registry component simplifies copying images to Artifact Registry and is the intended method for this integration. If you want to use Docker or Crane, see the following examples.
In the following example, the gcloud CLI is used to set up the Docker authentication, as an alternative to the standalone Docker credential helper.
copy-image:
image: gcr.io/google.com/cloudsdktool/google-cloud-cli:466.0.0-alpine
identity: google_cloud
services:
- docker:24.0.5-dind
variables:
SOURCE_IMAGE: $CI_REGISTRY_IMAGE:v0.1.0
TARGET_IMAGE: $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev/$GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID/$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME/app:v0.1.0
DOCKER_HOST: tcp://docker:2375
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- gcloud auth configure-docker $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev
script:
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
copy-image:
image:
name: gcr.io/go-containerregistry/crane:debug
entrypoint: [""]
identity: google_cloud
variables:
SOURCE_IMAGE: $CI_REGISTRY_IMAGE:v0.1.0
TARGET_IMAGE: $GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev/$GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID/$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME/app:v0.1.0
before_script:
# Temporary workaround for https://github.com/google/go-containerregistry/issues/1886
- wget -q "https://github.com/GoogleCloudPlatform/docker-credential-gcr/releases/download/v2.1.22/docker-credential-gcr_linux_amd64-2.1.22.tar.gz" -O - | tar xz -C /tmp && chmod +x /tmp/docker-credential-gcr && mv /tmp/docker-credential-gcr /usr/bin/
- docker-credential-gcr configure-docker --registries=$GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION-docker.pkg.dev
script:
- crane auth login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- crane copy $SOURCE_IMAGE $TARGET_IMAGE