Cloud Key Management Service는 암호화 키를 관리하고 사용할 수 있게 해주는 Google Cloud 서비스입니다. 이 페이지에서는 Cloud Build의 Cloud KMS에서 암호화된 정보를 사용하는 방법을 설명합니다.
시작하기 전에
- 
  
   
   
     
   
  
   
   
     
   
  
 
  
  
    
      Enable the Cloud Build and Cloud KMS APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 이 가이드에서 명령줄 예시를 사용하려면 Google Cloud CLI를 설치하고 구성합니다. 
- Cloud KMS를 사용하여 민감한 정보를 암호화합니다. Cloud KMS는 파일에 암호화된 콘텐츠를 저장합니다. 
- [선택사항] 암호화된 데이터를 사용하도록 빌드를 구성하려면 ENCRYPTED_FILE을 base64로 변환합니다(이 단계는 암호화된 파일을 사용하는 빌드 구성에 필요하지 않음). - base64 ENCRYPTED_FILE
필수 IAM 권한
빌드 서비스 계정에 Cloud KMS CryptoKey 복호화(roles/cloudkms.cryptoKeyDecrypter) IAM 역할을 부여합니다.
- Google Cloud 콘솔에서 Cloud Build 설정 페이지로 이동합니다. 
- Cloud KMS CryptoKey 복호화기 역할이 있는 행을 찾고 상태를 사용 설정됨으로 설정합니다. 
암호화된 데이터를 사용하도록 빌드 구성
- 프로젝트 루트 디렉터리에 - cloudbuild.yaml또는- cloudbuild.json이라는 Cloud Build 빌드 구성 파일을 만듭니다.
- 빌드 구성 파일에서 다음을 수행합니다. - 모든 빌드 steps이후availableSecrets필드를 추가하여 암호화된 값을 환경 변수로 지정하고 이를 암호화하기 위해 사용할kmsKeyName을 지정합니다.kmsKeyName값에 대체 변수를 사용할 수 있습니다.
- 보안 비밀을 지정하려는 빌드 단계에서 다음을 수행합니다.
- 빌드 단계에서 bash 도구를 사용하기 위해 bash를 가리키는entrypoint필드를 추가합니다. 이것은 보안 비밀에 대해 환경 변수를 참조하기 위해 필요합니다.
- 암호화된 값에 대해 환경 변수를 지정하는 secretEnv필드를 추가합니다.
- args필드에서- -c플래그를 첫 번째 인수로 추가합니다. -c 다음에 전달하는 문자열은 모두 명령어로 취급됩니다. -c로 시작하는 bash 명령어 실행에 대한 자세한 내용은 bash 문서를 참조하세요.
- args필드에 암호화된 값을 지정할 때- $$.
 
- 빌드 단계에서 bash 도구를 사용하기 위해 
 - The following example build config file shows how to login to Docker and pull a private image: - YAML- steps: - name: 'gcr.io/cloud-builders/docker' entrypoint: 'bash' args: ['-c', 'docker login --username=$$USERNAME --password=$$PASSWORD'] secretEnv: ['USERNAME', 'PASSWORD'] - name: 'gcr.io/cloud-builders/docker' entrypoint: 'bash' args: ['-c', 'docker pull $$USERNAME/IMAGE:TAG'] secretEnv: ['USERNAME'] availableSecrets: inline: - kmsKeyName: projects/PROJECT_ID/locations/global/keyRings/USERNAME_KEYRING_NAME/cryptoKeys/USERNAME_KEY_NAME envMap: USERNAME: 'ENCRYPTED_USERNAME' - kmsKeyName: projects/PROJECT_ID/locations/global/keyRings/PASSWORD_KEYRING_NAME/cryptoKeys/PASSWORD_KEY_NAME envMap: PASSWORD: 'ENCRYPTED_PASSWORD'가 프리픽스로 추가된 환경 변수를 사용하여 지정합니다.- JSON- { "steps": [ { "name": "gcr.io/cloud-builders/docker", "entrypoint": "bash", "args": [ "-c", "docker login --username=$$USERNAME --password=$$PASSWORD" ], "secretEnv": [ "USERNAME", "PASSWORD" ] }, { "name": "gcr.io/cloud-builders/docker", "entrypoint": "bash", "args": [ "-c", "docker pull $$USERNAME/REPOSITORY:TAG" ], "secretEnv": [ "USERNAME" ] } ], "availableSecrets": { "inline": [{ "kmsKeyName": "projects/PROJECT_ID/locations/global/keyRings/USERNAME_KEYRING_NAME/cryptoKeys/USERNAME_KEY_NAME", "envMap": { "USERNAME": "ENCRYPTED_USERNAME" } }, { "kmsKeyName": "projects/PROJECT_ID/locations/global/keyRings/PASSWORD_KEYRING_NAME/cryptoKeys/PASSWORD_KEY_NAME", "envMap": { "PASSWORD": "ENCRYPTED_PASSWORD" } }] } }- Replace the placeholder values in the above commands with the following: - PROJECT_ID: The ID of the Google Cloud project which contains your Cloud KMS service.
- USERNAME_KEYRING_NAME: The key ring name of your Docker username.
- USERNAME_KEY_NAME: The key name of your Docker username.
- ENCRYPTED_USERNAME: Your encrypted Docker username in base64 format.
- PASSWORD_KEYRING_NAME: The key ring name of your Docker password.
- PASSWORD_KEY_NAME: The key name of your Docker password.
- ENCRYPTED_PASSWORD: Your encrypted Docker password in base64 format.
- REPOSITORY: The name of your Docker repository from where you're pulling the image.
- TAG: The tag name of your image.
 
- 모든 빌드 
- Use the build config file to manually start a build or to automate builds using triggers. 
Configuring builds to use encrypted files
- In your project root directory, create a Cloud Build build config file named - cloudbuild.yamlor- cloudbuild.json.
- In your build config file, before any build steps that interact with the decrypted file, add a - gcloudbuild step to decrypt the encrypted file using the encryption key. The following example build config file shows how to login to Docker using the encrypted file with Docker password:- YAML- steps: - name: gcr.io/cloud-builders/gcloud args: - kms - decrypt - "--ciphertext-file=ENCRYPTED_PASSWORD_FILE" - "--plaintext-file=PLAINTEXT_PASSWORD_FILE" - "--location=global" - "--keyring=KEYRING_NAME" - "--key=KEY_NAME" - name: gcr.io/cloud-builders/docker entrypoint: bash args: - "-c" - docker login --username=DOCKER_USERNAME --password-stdin < PLAINTEXT_PASSWORD_FILE- JSON- { "steps": [ { "name": "gcr.io/cloud-builders/gcloud", "args": [ "kms", "decrypt", "--ciphertext-file=ENCRYPTED_PASSWORD_FILE", "--plaintext-file=PLAINTEXT_PASSWORD_FILE", "--location=global", "--keyring=KEYRING_NAME", "--key=KEY_NAME" ] }, { "name": "gcr.io/cloud-builders/docker", "entrypoint": "bash", "args": [ "-c", "docker login --username=DOCKER_USERNAME --password-stdin < PLAINTEXT_PASSWORD_FILE" ] } ] }- Replace the placeholder values in the above commands with the following: - KEYRING_NAME: The key ring name of your Docker password.
- KEY_NAME: The key name of your Docker password.
- ENCRYPTED_PASSWORD_FILE: Encrypted file with your Docker password.
- PLAINTEXT_PASSWORD_FILE: Plaintext file with your Docker password.
 
- Use the build config file to manually start a build or to automate builds using triggers. 
Configuring builds to use encrypted data (legacy)
To encrypt sensitive data using Cloud KMS and use that data in a build config file:
- In your build config file, add a - secretsfield to specify the encrypted value and the- CryptoKeyto use to decrypt it. Then, in the build step where you want to use the encrypted variable, add a- secretEnvfield to specify the variable as an environment variable. Include the variable's name in the- secretEnvfield. If you specify the variable value, or a non-secret environment variable with the same name, Cloud Build throws an error.- YAML- steps: - name: 'gcr.io/cloud-builders/docker' entrypoint: 'bash' args: ['-c', 'docker login --username=user-name --password=$$PASSWORD'] secretEnv: ['PASSWORD'] - name: 'gcr.io/cloud-builders/docker' args: ['push', 'user-name/myubuntu'] secrets: - kmsKeyName: projects/project-id/locations/global/keyRings/keyring-name/cryptoKeys/key-name secretEnv: PASSWORD: 'encrypted-password'- JSON- { "steps": [ { "name": "gcr.io/cloud-builders/docker", "entrypoint": "bash", "args": [ "-c", "docker login --username=user-name --password=$$PASSWORD" ], "secretEnv": [ "PASSWORD" ] }, { "name": "gcr.io/cloud-builders/docker", "args": [ "push", "user-name/myubuntu" ] } ], "secrets": [ { "kmsKeyName": "projects/project-id/locations/global/keyRings/keyring-name/cryptoKeys/key-name", "secretEnv": { "PASSWORD": "encrypted-password" } } ] }- 다음 단계- Secret Manager에서 보안 비밀에 액세스하도록 빌드를 구성하는 방법 알아보기
- 비공개 GitHub 저장소 액세스 방법 알아보기
 - 달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다. - 최종 업데이트: 2025-10-19(UTC)