Google Artifact Registry

Tier: Free, Premium, Ultimate Offering: GitLab.com Status: Beta
GitLab.com에서는 이 기능이 일부 사용자를 대상으로 사용 가능합니다. 그러나 GitLab Dedicated에서는 지원되지 않습니다.

이 기능은 Beta 상태입니다. 이 기능을 테스트하는 사용자 목록에 참여하려면 대기 목록에 등록하세요.

Google Artifact Registry 통합을 사용하여 GitLab 프로젝트에 Google Artifact Registry 저장소를 구성하고 연결할 수 있습니다.

Google Artifact Registry를 프로젝트에 연결한 후에는 Google Artifact Registry 저장소에서 Docker 및 OCI 이미지를 볼 수 있고, 푸시하거나 풀 수 있습니다.

GitLab 프로젝트에 Google Artifact Registry 설정하기

전제 조건:

  • GitLab 프로젝트에 적어도 Maintainer 역할이 있어야 합니다.
  • Artifact Registry 저장소를 관리하기 위한 필수 권한이 있어야 합니다.
  • 구글 클라우드에 인증하기 위해 workload identity federation (WLIF) 풀과 프로바이더를 구성해야 합니다.
  • 다음 구성을 갖춘 Google Artifact Registry 저장소가 있어야 합니다:
    • Docker 형식.
    • Standard 모드. 다른 저장소 형식 및 모드는 지원되지 않습니다.

GitLab 프로젝트에 Google Artifact Registry 저장소를 연결하려면:

  1. GitLab에서 왼쪽 사이드바에서 검색 또는 이동을 선택하고 프로젝트를 찾습니다.
  2. 설정 > 통합을 선택합니다.
  3. Google Artifact Registry를 선택합니다.
  4. 통합 활성화 아래에서 활성 확인란을 선택합니다.
  5. 다음 필드를 작성합니다:
  6. 화면 안내에 따라 Google Cloud Identity and Access Management (IAM) 정책을 설정합니다. 다양한 종류의 정책에 대한 자세한 정보는 IAM 정책을 참조하세요.
  7. 변경 사항 저장을 선택합니다. Deploy 아래의 사이드바에 Google Artifact Registry 항목이 표시됩니다.

Google Artifact Registry에 저장된 이미지 보기

전제 조건:

  • 프로젝트에 Google Artifact Registry가 구성되어 있어야 합니다.

GitLab UI에서 연결된 Artifact Registry 저장소에 저장된 이미지 목록을 보려면:

  1. 왼쪽 사이드바에서 검색 또는 이동을 선택하고 프로젝트를 찾습니다.
  2. 배포 > Google Artifact Registry를 선택합니다.
  3. 이미지 세부 정보를 보려면 이미지를 선택합니다.
  4. Google Cloud 콘솔에서 이미지를 보려면 Google Cloud에서 열기를 선택합니다. 해당 Artifact Registry 저장소를 보려면 필요한 권한이 있어야 합니다.

CI/CD

미리 정의된 변수

Artifact Registry 통합이 활성화된 후에는 다음 미리 정의된 환경 변수를 CI/CD에서 사용할 수 있습니다. 이러한 환경 변수를 사용하여 연결된 저장소에 이미지를 푸시하거나 끌어오는 등의 Artifact Registry 작업을 수행할 수 있습니다.

변수 GitLab Runner 설명
GOOGLE_ARTIFACT_REGISTRY_PROJECT_ID 16.10 16.10 Artifact Registry 저장소가 있는 Google Cloud 프로젝트 ID입니다.
GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_NAME 16.10 16.10 연결된 Artifact Registry 저장소의 이름입니다.
GOOGLE_ARTIFACT_REGISTRY_REPOSITORY_LOCATION 16.10 16.10 연결된 Artifact Registry 저장소의 Google Cloud 위치입니다.

Google Artifact Registry로 인증

워크로드 실행 중에 파이프라인을 구성하여 Google Artifact Registry와 인증할 수 있습니다. GitLab은 구성된 워크로드 신원 풀 IAM 정책을 사용하고 GOOGLE_APPLICATION_CREDENTIALSCLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE 환경 자격 증명을 채웁니다. 이러한 환경 자격 증명은 gcloud CLIcrane와 같은 클라이언트 도구에서 자동으로 감지됩니다.

Google Artifact Registry로 인증하려면 프로젝트의 .gitlab-ci.yml 파일에서 identity 키워드를 google_cloud로 설정하세요.

IAM 정책

Google Cloud 프로젝트에는 Google Artifact Registry 통합을 사용하기 위한 특정 IAM 정책이 있어야 합니다. 이 통합을 설정할 때 화면 지침에서는 Google Cloud 프로젝트에서 다음 IAM 정책을 생성하는 단계를 제공합니다. - Artifact Registry Reader 역할을 게스트 역할 이상의 GitLab 프로젝트 구성원에게 부여합니다. - Artifact Registry Writer 역할을 개발자 역할 이상의 GitLab 프로젝트 구성원에게 부여합니다.

이러한 IAM 정책을 수동으로 생성하려면 다음 gcloud 명령을 사용합니다. 다음 값을 대체하세요. - <your_google_cloud_project_id>: Artifact Registry 리포지토리가 있는 Google Cloud 프로젝트의 ID로 대체합니다. - <your_workload_identity_pool_id>: 워크로드 신원 풀의 ID로 대체합니다. 이 값은 Google Cloud IAM 통합에서 사용한 값과 동일합니다. - <your_google_cloud_project_number>: 워크로드 신원 풀이 있는 Google Cloud 프로젝트의 번호로 대체합니다. 이 값은 Google Cloud IAM 통합에서 사용한 값과 동일합니다.

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'

사용 가능한 클레임 목록은 OIDC custom claims을 참조하세요.

예시

gcloud CLI를 사용하여 이미지 목록 나열

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

crane을 사용하여 이미지 목록 나열

list-images:
  image: 
    name: gcr.io/go-containerregistry/crane:debug
    entrypoint: [""]
  identity: google_cloud
  before_script:
    # 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

Docker를 사용하여 이미지 가져오기

다음 예시는 Google이 제공하는 독립 Docker 자격 증명 도우미를 사용하여 Docker의 인증을 설정하는 방법을 보여줍니다.

pull-image:
  image: docker:24.0.5
  identity: google_cloud
  services:
    - docker:24.0.5-dind
  variables:
    # 두 변수는 DinD 서비스가 TLS 모드로 시작되고 Docker CLI가 API와 통신하도록 적절하게 구성되도록 보장합니다. 자세한 내용은 https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#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
### Docker를 사용하여 GitLab 컨테이너 레지스트리에서 이미지 복제

다음 예시는 GitLab 컨테이너 레지스트리에서 이미지를 Google Artifact Registry로 복제하는 방법을 보여줍니다.
이를 위해 GitLab에서 이미지를 끌어오고 다시 태그를 지어주고, 그 후 Google Artifact Registry로 푸시합니다.

다음 예시에서 `gcloud` CLI를 사용하여 Docker 인증을 설정하는데, [독립형 Docker 자격 증명 도우미](https://cloud.google.com/artifact-registry/docs/docker/authentication#standalone-helper)의 대안으로 사용됩니다.

```yaml
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

crane을 사용하여 GitLab 컨테이너 레지스트리에서 이미지 복제

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:
    # 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

```