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

Plan with -detailed-exitcode option returns error #152

Closed
aminzar-access opened this issue Mar 1, 2022 · 14 comments
Closed

Plan with -detailed-exitcode option returns error #152

aminzar-access opened this issue Mar 1, 2022 · 14 comments

Comments

@aminzar-access
Copy link

Hi,
Running plan with -detailed-exitcode reports error. Thanks.

image

      - name: Terraform Plan
        id: plan
        run: terraform -chdir=${{ env.working_directory}} plan -refresh=${{ github.event_name != 'pull_request' }} -out ${{ env.tf_plan_file }} -detailed-exitcode -no-color
        continue-on-error: true
@Michaelvsk
Copy link

That's not related to setup-terraform action. Github Actions handle all nonzero exit codes as errors and therefore let the script end immediately and prints an error.

If you do not need to process the detailed exit code, then just omit the parameter. If you want to do different things based on the exit code, then you are in the same situation as I am currently. So far I haven't found a workaround to retrieve the detailed exit code but do not let the script stop immedialty.

@Michaelvsk
Copy link

I were wrong. It is in indeed related as it seems that Error: Terraform exited with code 2. is printed when using the option terraform-wrapper: true.
My script is

- run: |
  terraform plan -out ${{ inputs.terraform-plan-artifact-name }} -detailed-exitcode || true

so bash counts it as exit code 0 and the step is also finishing successfully. It still prints the error to log and build summary page

@culpinnis
Copy link

Hi together,
We used a snippet like this before to determine if the plan contained any changes.

          set +e
          terraform plan ${{ inputs.TF_VARS }} -detailed-exitcode -out=deploy_${{ github.run_number }}.tfplan ${{ steps.SetTerraformDestroyFlag.outputs.destroyFlag }}
          exitcode=$?
          if [[ $exitcode -eq 2 ]]; then
            echo '::set-output name=planHasChanges::true'
            exit 0
          else
            exit $exitcode
          fi

This works fine when using an ubuntu runner without the TF setup action.
When using the provided Hashicorp action with the following code, I get the same issue.

      - uses: hashicorp/setup-terraform@v1
        name: Setup Terraform on private runner

Of course, I can use || true, but then I do not know if the plan has any changes (the error is reported to the log, but the exit code is 0 instead of 2).
Any ideas on how to deal with that? Is this a general GitHub action issue or related to this specific action?

@aereal
Copy link

aereal commented May 6, 2022

This is an issue that comes from @actions/core function.

hashicorp/setup-terraform provides the terraform(1) wrapper and it uses @actions/core.setFailed function.
refs.

core.setFailed(error.message);

@actions/core.setFailed always sets exit code as 1 and discards terraform plan's exit code such as 2.
refs. https://github.com/actions/toolkit/blob/main/packages/core/src/core.ts#L213-L217

We can obtain correct exit code from terraform(1) to specify terraform_wrapper: false and use bare terraform CLI.

@aminzar-access
Copy link
Author

This is an issue that comes from @actions/core function.

hashicorp/setup-terraform provides the terraform(1) wrapper and it uses @actions/core.setFailed function. refs.

core.setFailed(error.message);

@actions/core.setFailed always sets exit code as 1 and discards terraform plan's exit code such as 2. refs. https://github.com/actions/toolkit/blob/main/packages/core/src/core.ts#L213-L217

We can obtain correct exit code from terraform(1) to specify terraform_wrapper: false and use bare terraform CLI.

Hi @aereal, how do we get stdout, stderr and the exitcode with terraform_wrapper: false? I can't find any samples. Thanks

@aereal
Copy link

aereal commented May 11, 2022

@aminzar-access

You may be able to set those values using workflow commands.

steps:
  - id: terraform
    run: |
      set +e # allow the workflow to continue following steps even if the `terraform` execution failed
      terraform ... 1>stdout 2>stderr
      tf_exitcode=$?
      echo "::set-output name=exitcode::$tf_exitcode"
      echo "::set-output name=stdout::$(cat ./stdout)"
      echo "::set-output name=stderr::$(cat ./stderr)"

(I'm not sure using set-output with multi-line text works correctly)

@aminzar-access
Copy link
Author

Thanks for your help @aereal, really appreciate it.

@aminzar-access
Copy link
Author

@aereal unfortunately seems like exitcode is not being set and stdout doesn't have a value when you have multiple terraform commands in workflow.

@jpogran
Copy link
Collaborator

jpogran commented May 18, 2022

We recently merged #125, which handles both 0 and 2 as exitcodes. If you could test by using that git commit I would sincerely appreciate it. An official tag will be created when 2.1.0 is released

@aminzar-access
Copy link
Author

@jpogran looks like it has solved the issue, no more errors and I can still get the proper exitcode, thanks.

@ianhundere
Copy link

any word as to when 2.1.0 will be released?

@brettcurtis
Copy link

Hoping for 2.1.0 soon too, this is really confusing to our users.

@austinvalle
Copy link
Member

Closing this issue as it seems the fix was released in v2.0.1 👍🏻

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants