[英]How to build and push a docker image in a Terraform Docker Provider by GCP Cloud Build
部署由 CICD 基于 GCP Cloud Build 完成(并且云构建服务帐户具有相关项目的“所有者”角色)。
在“cloudbuild.yaml”文件中,有一个带有“hashicorp/terraform”工作者的步骤,以及一个类似“terraform apply”的命令。
构建 docker 映像并将其推送到 GCP Artefact Registry 中,以便可以在其他 TF 资源中的容器优化计算引擎部署中使用它。
As the Terraform Google Provider does not have resources to work with the Artefact Registry docker images, I have to use the Terraform Docker Provider.
docker 镜像描述为:
resource "docker_registry_image" "my_image" {
name = "europe-west2-docker.pkg.dev/${var.my_project_id}/my-docker-reg/my-vm-image:test"
build {
context = "${path.module}/image"
dockerfile = "Dockerfile"
}
}
根据使用 terraform 创建和推送 docker 映像到谷歌云注册表的评论: For pushing images, the only way to set credentials is to declare them at the provider level.
因此,registry_auth 块将按照Terraform Docker Provider 文档中的描述提供。
一方面,如GCP Artefact Registry 身份验证文档中所述, You do not need to configure authentication for Cloud Build
。 所以我使用它进行配置(因为它要在 Cloud Build 服务帐户下执行):
provider "docker" {
registry_auth {
address = "europe-west2-docker.pkg.dev"
}
}
并且 Cloud Build 作业(terraform 步骤)失败并出现错误:
Error: Error loading registry auth config: could not open config file from filePath: /root/.docker/config.json. Error: open /root/.docker/config.json: no such file or directory
with provider["registry.terraform.io/kreuzwerker/docker"],
on my-vm-image.tf line 6, in provider "docker":
6: provider "docker" {
由于 Docker 提供者强制希望获得一些身份验证凭据...
可以通过云构建 yaml 中的步骤检索云构建服务帐户的访问令牌:
## Get Cloud Build access token
- id: "=> get CB access token =>"
name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'sh'
args:
- '-c'
- |
access_token=$(gcloud auth print-access-token) || exit 1
echo ${access_token} > /workspace/access_token || exit 1
稍后在 TF 步骤中用作变量值:
...
access_token=$(cat /workspace/access_token)
...
terraform apply -var 'access_token=${access_token}' ....
...
所以 Terraform Docker Provider 应该按照示例gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://LOCATION-docker.pkg.dev
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://LOCATION-docker.pkg.dev
来自GCP Artefact Registry 身份验证文档:
provider "docker" {
registry_auth {
address = "europe-west2-docker.pkg.dev"
username = "oauth2accesstoken"
password = var.access_token
}
}
但是 Cloud Build 作业(terraform 步骤)再次失败:
Error: Error pushing docker image: Error pushing image: unauthorized: failed authentication
所以,如果我不尝试任何完全不同的替代方法,那么 Terraform Docker Provider 如何在 GCP Cloud Build 中工作? 正确的身份验证需要做什么?
由于 Terraform Google Provider 没有资源来处理 Artefact Registry docker 图像
首先,我不明白上面的句子。 这是Google 的 Artifact Registry 资源。
其次,为什么要使用docker_registry_image
? 甚至是docker
提供商?
如果您为您的服务帐户提供正确的角色(无需完全所有权, roles/artifactregistry.writer
即可),那么您可以毫无问题地将 Cloud Build 构建的图像推送到 Artifact Registry。 只需在必要的构建步骤中将映像名称设置为docker
。
例如:
steps:
- id: build
name: docker
args:
- build
- .
- '-t'
- LOCATION-docker.pkg.dev/PROJECT_ID/ARTIFACT_REGISTRY_REPO/IMAGE
- id: push
name: docker
args:
- push
- LOCATION-docker.pkg.dev/PROJECT_ID/ARTIFACT_REGISTRY_REPO/IMAGE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.