I have a gitlab-ci pipeline with multiple stages such as lint, test, build, deploy-sandbox and destroy-sandbox.
deploy-sandbox starts when there is a merge request and within deploy-sandbox I’m running script to init terraform, terraform apply, and adding terraform state files to artifacts.
destroy-sandbox stages run when the merge request is merged into the default branch and has script that runs terraform destroy.
Since this stage runs as a separate pipeline with no connection to previous stages, terraform destroy does not work properly even if I add dependency as deploy-sandbox and also add artifacts into the stage.
All I’m getting this message
No changes. No objects need to be destroyed.
Either you have not created any objects yet or the existing objects were
already deleted outside of Terraform.
These are the stage for reference
deploy-sandbox:
image:
name: hashicorp/terraform:light
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
stage: deploy-sandbox
script:
- terraform init
- terraform apply -auto-approve
artifacts:
paths:
- terraform.tfstate
- .terraform
- .terraform.lock.hcl
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
destroy-sandbox:
image:
name: hashicorp/terraform:light
entrypoint:
- '/usr/bin/env'
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
stage: destroy-sandbox
script:
- terraform init
- terraform destroy -auto-approve
artifacts:
paths:
- terraform.tfstate
- .terraform
- .terraform.lock.hcl
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
when: manual
dependencies:
- deploy-sandbox
Please suggest how can I successfully run terraform destroy.