【发布时间】:2021-01-06 09:55:05
【问题描述】:
我制作了一个 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”)。
【问题讨论】:
-
“新图像”是什么意思?由 terraform 部署的 Docker 映像?你能过去你引用这张图片的地形代码吗?
-
你能分享你到目前为止所做的事情吗,最好是minimal reproducible example?这样我们就可以根据您已经完成的工作更直接地为您提供帮助。
-
我添加了代码让事情更清楚,你可以在EDIT下找到它们
标签: continuous-integration gitlab terraform continuous-deployment