I am trying to export the variable value (HAS_DESTROY_CHANGES) from one stage to another stage using artifacts:reports:dotenv method, but my pipeline is failing with the below error.
WARNING: Uploading artifacts as “dotenv” to coordinator… POST https://gitlab.build 400 Bad Request (Invalid Format) id=182245 responseStatus=400 Bad Request status=400 token=64_zESeE
WARNING: Retrying… context=artifacts-uploader error=invalid argument
WARNING: Uploading artifacts as “dotenv” to coordinator… POST https://gitlab.build 400 Bad Request (Invalid Format) id=182245 responseStatus=400 Bad Request status=400 token=64_zESeE
FATAL: invalid argument
Cleaning up project directory and file based variables
ERROR: Job failed: command terminated with exit code 1
Here is my pipeline code.
manual_approval:
stage: manual_approval
before_script:
script:
- terraform plan -input=false -out=tfplan
- if terraform show tfplan | grep -q "No changes"; then echo "No changes"; HAS_DESTROY_CHANGES="false"; echo $HAS_DESTROY_CHANGES >> manual_approval.env; elif terraform show tfplan| grep -q "0 to change, 0 to destroy"; then echo "Changes with no destroys detected, it is safe for the pipeline to proceed automatically"; HAS_DESTROY_CHANGES="false"; echo $HAS_DESTROY_CHANGES >> manual_approval.env; else HAS_DESTROY_CHANGES="true"; echo $HAS_DESTROY_CHANGES >> manual_approval.env; fi
artifacts:
reports:
dotenv: manual_approval.env
wait_for_approval:
stage: wait_for_approval
when: manual
script:
- echo "$HAS_DESTROY_CHANGES"
allow_failure: false
rules:
- if: ($HAS_DESTROY_CHANGES == "true")
dependencies:
- manual_approval
terraform_apply:
stage: terraform_apply
Thanks @balonik for your replay. Could you please share some more details? Also do you have any suggestion how can export a variable from one stage to another stage and run based on a condition (or rule)
In your code replace echo $HAS_DESTROY_CHANGES >> manual_approval.env with echo "HAS_DESTROY_CHANGES=$HAS_DESTROY_CHANGES" >> manual_approval.env and it should work
Hi @balonik , thanks a lot the first stage started working. but I noticed the second stage (wait_for_approval) is now not showing in the UI. It should trigger if only the values of HAS_DESTROY_CHANGES=true. But it is not showing in the UI even if the values are true. Any suggestion? thanks for your help.
In pipelines? Probably not. You could probably make something like that work with dynamic child pipelines, but it won’t look nice.
Approvals like that are not really part of what GitLab does or aims to do. You should run a TF plan during open Merge Request where you will see the summary (changes) of TF plan directly in MR page. When the MR is approved and merged it’s equivalent of “approval” and pipeline to apply runs.