Gitlab CI/CD 管道不会破坏创建的 Terraform 创建的资源

问题描述 投票:0回答:1

我的管道成功地将内容部署到我的 Hetzner 帐户中。但是当我运行 destroy 时,它说“没有对象需要被销毁。

创建的资源是虚拟机。没有更多细节可补充。

.gitlab-ci-yml:

stages:
  - validate
  - plan
  - apply
  - destroy
image:
  name: hashicorp/terraform:light
  entrypoint:
    - '/usr/bin/env'
    - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
before_script:
  - export HCLOUD_TOKEN=${HCLOUD_TOKEN}
  - rm -rf .terraform
  - terraform --version
  - terraform init

validate:
  stage: validate
  script:
    - terraform validate

plan:
  stage: plan
  script:
    - terraform plan -out "planfile" --var-file=secret.tfvars
  dependencies:
    - validate
  artifacts:
    paths:
      - planfile

apply:
  stage: apply
  script:
    - terraform apply -input=false "planfile"
  dependencies:
    - plan
  when: manual

destroy:
  stage: destroy
  script:
    - terraform destroy --var-file=secret.tfvars
  when: manual
  dependencies: 
    - apply
terraform gitlab
1个回答
0
投票

Terraform 状态文件不可用于您的销毁作业。看起来您使用的是本地后端,并且 terraform 状态文件不在应用和销毁作业之间共享。

最快的解决方案是将其公开为工件,就像在计划和应用作业之间处理计划文件一样。

apply:
  stage: apply
  script:
    - terraform apply -input=false "planfile"
  dependencies:
    - plan
  when: manual
  artifacts:
    paths:
      - terraform.tfstate

destroy:
  stage: destroy
  script:
    - terraform destroy --var-file=secret.tfvars
  when: manual
  dependencies: 
    - apply    

您也可以通过添加全局缓存将其作为缓存的一部分传递:

cache:
  key: terraform-cache
  paths:
    - .terraform/
    - .terraform.tfstate

您可以将状态文件存储在 Gitlab 本身中。 gitlab 为 Gitlab terraform 状态助手提供助手,包括图像

© www.soinside.com 2019 - 2024. All rights reserved.