我制作了一个 Gitlab 管道来制作我的 CI/CD,我正在寻找如何将 terraform 代码上的图像名称更改为在 Gitlab 上自动构建的新图像。
编辑 这是构建图像(ci)
stages:
- build_image_prod
- build_image_dev
variables:
DOCKER_HOST: tcp://docker:2375
.build-setup:
before_script:
- cd back
build_container_prod:
extends: .build-setup
stage: build_image_prod
image:
name: amazon/aws-cli
entrypoint: [""]
services:
- docker:dind
before_script:
- amazon-linux-extras install docker
- aws --version
- docker --version
- aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin "aws_ecr"
script:
- docker build -t back:$CI_PIPELINE_IID .
- docker tag back:$CI_PIPELINE_IID aws_ecr/back:$CI_PIPELINE_IID
- docker push aws_ecr/back:$CI_PIPELINE_IID
rules:
- if: $CI_COMMIT_BRANCH == "master"
when: always
- when: never
**这对于 terraform 管道
stages:
- init
- workspace
- plan
- apply
image:
name: hashicorp/terraform:0.13.3
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
.terraform-setup:
before_script:
- cd infra
init_terraform_prod:
extends: .terraform-setup
stage: init
script:
- rm -rf .terraform
- terraform init
rules:
- if: $CI_COMMIT_BRANCH == "master"
when: always
- when: never
workspace_terraform_prod:
extends: .terraform-setup
stage: workspace
script:
- terraform workspace select prod
dependencies:
- init_terraform_prod
rules:
- if: $CI_COMMIT_BRANCH == "master"
when: always
- when: never
plan_terraform_prod:
extends: .terraform-setup
stage: plan
script:
- terraform plan -out "planfile"
dependencies:
- workspace_terraform_prod
artifacts:
paths:
- infra/planfile
rules:
- if: $CI_COMMIT_BRANCH == "master"
when: always
- when: never
apply_terraform_prod:
extends: .terraform-setup
stage: apply
when: manual
script:
- terraform apply -input=false "planfile" -refresh=true -target=aws_ecs_service.back -target=aws_ecs_task_definition.back -target=aws_ecs_service.front -target=aws_ecs_task_definition.front
dependencies:
- plan_terraform_prod
only:
- master
*this 为任务定义的容器定义
[
{
"name": "front",
"image": "aws_ecr/back:latest",
"portMappings": [
{
"containerPort": 80
}
]
}
]
每次我执行推送以部署构建的最新映像时,我都想更改定义的映像 URL ("image": "aws_ecr/back:latest")。
答案 0 :(得分:1)
如果您在 terraform 之外管理 ECR 存储库,则应将其作为数据资源引用:
data "aws_ecr_repository" "my_repository" {
name = "back"
}
然后,在您的容器定义中,像这样引用它:
"${data.aws_ecr_repository.my_repository.repository_url}:latest"