Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fail when theres an exit-code 2 #125

Merged
merged 5 commits into from May 18, 2022
Merged

Conversation

dannyibishev
Copy link
Contributor

This should allow plans to succeed using the terraform_wrapper functionality whenever an exit code of 2 is returned.
https://www.terraform.io/docs/cli/commands/plan.html#detailed-exitcode

  • This is useful for adding custom steps in our GitHub action workflows.

Disclaimer: Im not a Javascript developer so Im not sure how valid the OR condition is.

@hashicorp-cla
Copy link

hashicorp-cla commented Jul 13, 2021

CLA assistant check
All committers have signed the CLA.

@DariuszPorowski
Copy link

Hey @dannyibishev I am working on the same case as you now, but what I get from GitHub folks is, Actions itself only supports success (0 exit code) vs failure (anything else). So, it's not lack of support in Terraform Actions task, but general behavior of GitHub Actions.

@jpogran jpogran added the enhancement New feature or request label Mar 4, 2022
@jpogran
Copy link
Collaborator

jpogran commented Mar 4, 2022

Thanks @DariuszPorowski for the additional detail. We'll need to confirm what the best approach is here

@brettcurtis
Copy link

We gone through a bit of song and dance here attempting to get apply job to run automatically if no changes to the plan.

      - name: Terraform Plan - https://www.terraform.io/docs/commands/plan.html
        id: plan
        run: terraform plan -detailed-exitcode -input=false -out=plan.out ${{ inputs.tf_plan_args}} ${{ secrets.tf_plan_secret_args }}
        continue-on-error: true

      - name: Check for Plan Failure
        if: steps.plan.outputs.exitcode == 1
        run: exit 1

      # Encrypt - Terraform plan.out

      - name: Encrypt Terraform "plan.out"
        if: steps.plan.outputs.exitcode == 2
        run: gpg --quiet --batch --yes --passphrase ${{ secrets.gpg_passphrase }} --symmetric --cipher-algo AES256 plan.out

      # Github - Upload Artifact
      # https://github.com/marketplace/actions/upload-a-build-artifact

      - name: Upload Encrypted Artifact
        if: steps.plan.outputs.exitcode == 2
        uses: actions/upload-artifact@v2
        with:
          name: ${{ inputs.tf_workspace }}-encrypted-plan
          path: ${{ inputs.working_dir }}/plan.out.gpg

    outputs:
      planExitcode: ${{ steps.plan.outputs.exitcode }}

  apply:
    name: "Terraform: apply"
    needs: plan
    if: needs.plan.outputs.planExitcode == 2
    runs-on: ubuntu-latest
    environment:
      name: ${{ inputs.github_env }}

The exit code stuff you guys are talking about gives us the annotation errors and is rather confusing to people.

image

@jpogran jpogran added this to the 2.1.0 milestone Apr 18, 2022
dannyibishev and others added 3 commits April 27, 2022 10:26
This should allow plans to succeed using the terraform_wrapper functionality whenever an exit code of 2 is returned.
https://www.terraform.io/docs/cli/commands/plan.html#detailed-exitcode

- This is useful for adding custom steps in our GitHub action workflows.
- Not a Javascript developer so Im not sure how valid the OR condition is.
@jpogran jpogran requested a review from ksatirli April 27, 2022 14:47
@jpogran
Copy link
Collaborator

jpogran commented Apr 27, 2022

@dannyibishev I've rebased your PR with latest changes so it will pass linting.

I changed the condition to exit early if it is 0 or 2 instead of setting the exit code to be more explicit with our returns. We'll tes this out and get it merged in the 2.1.0 release.

@jpogran jpogran merged commit 62a66ee into hashicorp:main May 18, 2022
@bogaertg
Copy link

Hello,

With terraform-wrapper, we always have a response code 0 even if there is changes and option --detailled-exit-code.
Here is my testing workflow with a terraform plan and an echo $?.

We cannot return exitCode 2 with the wrapper because of setFailed function.

There is an opened issue for custom exit code.

Maybe, it will be better to add an environnement variable with status code for the wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants