Files
yml/jobs/terraform.yml

84 lines
3.7 KiB
YAML

.terraform_fmt:
script:
# Begin of cd-workdir.sh
- |
#change directory
cd $WORKDIR
# End of cd-workdir.sh
# Begin of fmt.sh
- |
#Terraform fmt
tofu fmt -diff -check -write=false
# End of fmt.sh
.terraform_validate:
script:
# Begin of cd-workdir.sh
- |
#change directory
cd $WORKDIR
# End of cd-workdir.sh
# Begin of init.sh
- "#Terraform init \ntofu init -reconfigure -backend-config=\"address=${GITLAB_TF_ADDRESS}\" -backend-config=\"lock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"unlock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"username=gitlab-ci-token\" -backend-config=\"password=${CI_JOB_TOKEN}\" -backend-config=\"lock_method=POST\" -backend-config=\"unlock_method=DELETE\" -backend-config=\"retry_wait_min=5\" \n"
# End of init.sh
# Begin of validate.sh
- "#Terraform validate\ntofu validate \n"
# End of validate.sh
.terraform_plan:
variables:
PLAN: plan.tfplan
JSON_PLAN_FILE: tfplan.json
script:
# Begin of cd-workdir.sh
- |
#change directory
cd $WORKDIR
# End of cd-workdir.sh
# Begin of init.sh
- "#Terraform init \ntofu init -reconfigure -backend-config=\"address=${GITLAB_TF_ADDRESS}\" -backend-config=\"lock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"unlock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"username=gitlab-ci-token\" -backend-config=\"password=${CI_JOB_TOKEN}\" -backend-config=\"lock_method=POST\" -backend-config=\"unlock_method=DELETE\" -backend-config=\"retry_wait_min=5\" \n"
# End of init.sh
# Begin of plan.sh
- "#Terraform plan \napk add --update curl jq \nalias convert_report=\"jq -r '([.resource_changes[].change.actions?]|flatten)|{\\\"create\\\":(map(select(.==\\\"create\\\"))|length),\\\"update\\\":(map(select(.==\\\"update\\\"))|length),\\\"delete\\\":(map(select(.==\\\"delete\\\"))|length)}'\"\ntofu plan -out=$PLAN $ARGUMENTS\ntofu show --json $PLAN | jq -r '([.resource_changes[].change.actions?]|flatten)|{\"create\":(map(select(.==\"create\"))|length),\"update\":(map(select(.==\"update\"))|length),\"delete\":(map(select(.==\"delete\"))|length)}' > $JSON_PLAN_FILE\n"
# End of plan.sh
artifacts:
reports:
terraform: $WORKDIR/$JSON_PLAN_FILE
.terraform_apply:
script:
# Begin of cd-workdir.sh
- |
#change directory
cd $WORKDIR
# End of cd-workdir.sh
# Begin of init.sh
- "#Terraform init \ntofu init -reconfigure -backend-config=\"address=${GITLAB_TF_ADDRESS}\" -backend-config=\"lock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"unlock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"username=gitlab-ci-token\" -backend-config=\"password=${CI_JOB_TOKEN}\" -backend-config=\"lock_method=POST\" -backend-config=\"unlock_method=DELETE\" -backend-config=\"retry_wait_min=5\" \n"
# End of init.sh
# Begin of apply.sh
- |
#Terraform validate
tofu apply -auto-approve $ARGUMENTS
# End of apply.sh
.terraform_destroy:
script:
# Begin of cd-workdir.sh
- |
#change directory
cd $WORKDIR
# End of cd-workdir.sh
# Begin of init.sh
- "#Terraform init \ntofu init -reconfigure -backend-config=\"address=${GITLAB_TF_ADDRESS}\" -backend-config=\"lock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"unlock_address=${GITLAB_TF_ADDRESS}/lock\" -backend-config=\"username=gitlab-ci-token\" -backend-config=\"password=${CI_JOB_TOKEN}\" -backend-config=\"lock_method=POST\" -backend-config=\"unlock_method=DELETE\" -backend-config=\"retry_wait_min=5\" \n"
# End of init.sh
# Begin of destroy.sh
- |
#Terraform validate
tofu destroy -auto-approve $ARGUMENTS
# End of destroy.sh