Skip to content

Commit

Permalink
Workaround terraform error
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Feb 9, 2024
1 parent 4626e97 commit 68cb29f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 15 additions & 3 deletions image/entrypoints/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function apply() {
# shellcheck disable=SC2086
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME apply -input=false -no-color $AUTO_APPROVE -lock-timeout=300s $PARALLEL_ARG $PLAN_OUT) \
2>"$STEP_TMP_DIR/terraform_apply.stderr" \
| $TFMASK
| $TFMASK \
| tee "$STEP_TMP_DIR/terraform_apply.stdout"
APPLY_EXIT=${PIPESTATUS[0]}
>&2 cat "$STEP_TMP_DIR/terraform_apply.stderr"
else
Expand Down Expand Up @@ -72,7 +73,12 @@ function apply() {
fi
fi

if [[ $APPLY_EXIT -eq 0 ]]; then
if [[ "$TERRAFORM_BACKEND_TYPE" == "cloud" && $APPLY_EXIT -ne 0 ]] && grep -q "Error: Saved plan has no changes" "$STEP_TMP_DIR/terraform_apply.stderr"; then
# Not really an error then is it?
APPLY_EXIT=0
output
update_comment cloud-no-changes-to-apply
elif [[ $APPLY_EXIT -eq 0 ]]; then
output
update_comment apply-complete "$STEP_TMP_DIR/terraform_output.json"
else
Expand Down Expand Up @@ -145,7 +151,13 @@ fi

### Apply the plan

if [[ "$INPUT_AUTO_APPROVE" == "true" || $PLAN_EXIT -eq 0 ]]; then
if [[ $BACKEND_TYPE == "cloud" && $PLAN_EXIT -eq 0 ]]; then
# Terraform cloud will just error if we try to apply a plan with no changes
echo "No changes to apply"
output
update_comment cloud-no-changes-to-apply

elif [[ "$INPUT_AUTO_APPROVE" == "true" || $PLAN_EXIT -eq 0 ]]; then
echo "Automatically approving plan"
apply

Expand Down
7 changes: 7 additions & 0 deletions image/src/github_pr_comment/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ def main() -> int:

comment = update_comment(github, comment, headers=comment.headers | {'closed': True}, status=format_output_status(outputs, remaining_size))

elif sys.argv[1] == 'cloud-no-changes-to-apply':
if comment.comment_url is None:
debug("Can't set status of comment that doesn't exist")
return 1
else:
comment = update_comment(github, comment, headers=comment.headers | {'closed': True}, status=f':white_check_mark: No changes to apply in {job_markdown_ref()}')

elif sys.argv[1] == 'get':
if comment.comment_url is None:
debug("Can't get the plan from comment that doesn't exist")
Expand Down

0 comments on commit 68cb29f

Please sign in to comment.