Giter VIP home page Giter VIP logo

tfe-run's Introduction

tfe-run Action

CI Integration Go Report Card

This GitHub Action creates a new run on Terraform Cloud. Integrate Terraform Cloud into your GitHub Actions workflow.

This action creates runs using the Terraform Cloud API which provides more flexibility than using the CLI. Namely, you can:

  • define your own message (no more "Queued manually using Terraform")
  • provide as many variables as you want
  • access the outputs from the Terraform state

Internally, we leverage the official Go API client from Hashicorp.

How to use it

- uses: kvrhdn/tfe-run@v1
  with:
    token: ${{ secrets.TFE_TOKEN }}
    workspace: tfe-run
    message: |
      Run triggered using tfe-run (commit: ${{ github.SHA }})
  id: tfe-run

... next steps can access the run URL with ${{ steps.tfe-run.outputs.run-url }}

Full option list:

- uses: kvrhdn/tfe-run@v1
  with:
    # Token used to communicate with the Terraform Cloud API. Must be a user or
    # team api token.
    token: ${{ secrets.TFE_TOKEN }}

    # Name of the organization on Terraform Cloud. Defaults to the GitHub
    # organization name.
    organization: kvrhdn

    # Name of the workspace on Terraform Cloud.
    workspace: tfe-run

    # Optional message to use as name of the run.
    message: |
      Run triggered using tfe-run (commit: ${{ github.SHA }})

    # The directory that is uploaded to Terraform Cloud/Enterprise, defaults
    # to the repository root. Respsects .terraformignore. Note: this is
    # prefixed to the "Terraform Working Directory" in the remote workspace
    # settings.
    directory: integration/

    # The type of run, allowed options are 'plan', 'apply' and 'destroy'.
    type: apply

    # An optional list of resource addresses to target. Should be a list of
    # strings separated by new lines.
    #
    # For more information about resource targeting, check https://www.terraform.io/docs/commands/plan.html#resource-targeting
    targets: |
        resource.name

    # Whether we should wait for the plan or run to be applied. This will block
    # until the run is finished.
    wait-for-completion: true

    # The contents of a auto.tfvars file that will be uploaded to Terraform
    # Cloud. This can be used to set Terraform variables.
    tf-vars: |
      run_number = ${{ github.run_number }}
      service    = "example"

  # Optionally, assign this step an ID so you can refer to the outputs from the
  # action with ${{ steps.<id>.outputs.<output variable> }}
  id: tfe-run

Inputs

Name Required Description Type Default
token yes Token used to communicating with the Terraform Cloud API. Must be a user or team api token. string
organization Name of the organization on Terraform Cloud. string The repository owner
workspace yes Name of the workspace on Terraform Cloud. string
message Optional message to use as name of the run. string Queued by GitHub Actions (commit: $GITHUB_SHA)
directory The directory that is uploaded to Terraform Cloud/Enterprise, defaults to repository root. Respects .terraformignore. Note: this is prefixed to the "Terraform Working Directory" in the remote workspace settings. string ./
type The type of run, allowed options are 'plan', 'apply' and 'destroy'. string apply
targets An optional list of resource addresses to target. Should be a list of strings separated by new lines. string
wait-for-completion Whether we should wait for the plan or run to be applied. This will block until the run is finished. string false
tf-vars The contents of a auto.tfvars file that will be uploaded to Terraform Cloud. string

Outputs

Name Description Type
run-url URL of the run on Terraform Cloud string
has-changes Whether the run has changes. bool ('true' or 'false')
tf-** Outputs from the current Terraform state, prefixed with tf-. Only set for non-speculative runs. string

License

This Action is distributed under the terms of the MIT license, see LICENSE for details.

Development

For running tfe-run locally, see development.md.

For creating new release, see release-procedure.md.

tfe-run's People

Contributors

dependabot[bot] avatar kvrhdn avatar nicole-ashley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tfe-run's Issues

Setup failed: failed unpacking terraform config: lstat

TFC Run triggered using this GH Action fail with below issue.
Same error does not happen when I Queue plan manually from the Terraform Cloud UI.

Terraform v1.0.0
on linux_amd64

Setup failed: failed unpacking terraform config: lstat /tmp/terraform-build-worker01234676/slug: no such file or directory

I've tried configure the workspace with both UI/VCS-driven and API-driven workflows. Same issue in both cases.

Action step:

       - name: Terraform Cloud Run
         uses: kvrhdn/[email protected]
         with:
           token: ${{ secrets.TFC_ACCESS_TOKEN }}
           organization: MyOpg
           workspace: my-workspace
           type: apply
           wait-for-completion: true
           message: |
             Run triggered using tfe-run (commit: ${{ github.SHA }})
         id: tfe-run

Error: could not get current state: resource not found

Any ideas on this error?

For a Pull Request I'm using the action to run a Speculative Plan so type is set to plan

      - name: Terraform Cloud Speculative Plan 
        uses: kvrhdn/[email protected]
        with:
          token: ${{ secrets.TFC_ACCESS_TOKEN }}
          organization: MyOrg
          workspace: networking-workspace
          directory: networking/
          type: plan
          wait-for-completion: true
          message: |
            ${{ github.head_ref }} - ${{ github.event.commits[0].message }} #${{ github.run_number }} (commit: ${{ steps.slug.outputs.sha8 }})

From Terraform Cloud the speculative-plan finished successfully but the tfe-run action fails

Run status: pending
Run status: plan queued
Run status: planning
Run status: planned and finished
Run is planned and finished.
::set-output name=run-url::https://app.terraform.io/app/XXXX
##[debug]='https://app.terraform.io/app/XXXXX'
::set-output name=has-changes::true
##[debug]='true'
Error: could not get current state: resource not found
##[debug]Docker Action run completed with exit code 1

Output of the run shows sensitive outputs

Because the output of the run is being got directly from the state it prints sensitive output values to the github action log.

Would be good to suppress this like the CLI does or at least make it an option.

Don't fail the workflow if no statefile is available

Follow up from #22: in a new Terraform workspace when no statefile has been added yet, every speculative run will fail when attempting to fetch the outputs.

We should either:

  • fetch the outputs from the statefile that is created by the plan (if this is possible with the API?)
  • not fail when the statefile does not exist (i.e. log a warning and do not return any outputs)

support for enteprise

terraform enterprise makes use of the same API as native terraform cloud.

Therefore, It would be nice if we can pass a hostname for an org terraform enterprise server in GitHub actions so that it doesn't use the default "app.terraform.io" for all api calls.

Error: could not parse state: json: cannot unmarshal array into Go struct field terraformOutput.outputs.value of type string

I hit the following issue when I added a Terraform output of type array.

Terraform Cloud Plan & Apply passes successfully and I can see array output.
But kvrhdn/tfe-run action fails with

Run kvrhdn/[email protected]
...
Run has been applied!
Error: could not parse state: json: cannot unmarshal array into Go struct field terraformOutput.outputs.value of type string

My Terraform output

terraform output
name = [
  "my-name",
]

Command Line and Environment Argument Parsing

Would you be open to a pull request for optionally passing as arguments and environment variables? I'm new to go and found your project when I was working through how to create a run through the SDK. Your much better implementation has given me a lot of great example content on how to do this.

I'm using in azure devops pipelines right now and was considering enhancing to support not just json but also env/cli args. If that's within scope let me know and I'll see if I can make a contribution and get some feedback for you. Cheers and thanks for this project!

Unable to run on Terraform Cloud when specifying tf-vars

Terraform Cloud gives me an error regarding the variables that have been uploaded to the run. Apparently they're missing a trailing newline, and nothing I've tried in the yaml will force the variable definition to include one.

image

I would expect that updating tfe-run or go-tfe-run to ensure there's a newline at the end of the vars file would fix this.

Runs failing with TLS handshake timeout

EDIT 6 Sept.: original title was Segmentation violation during monitoring of the run


The past day two runs failed with the same error message. It's unclear what is causing this error (whether it's a bug in our code or weird responses coming from the API), but we should try to fix it anyway.

Error message:

...
Run status: apply queued
Run status: applying
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x6d00d3]

goroutine 1 [running]:
github.com/kvrhdn/go-tfe-run.Run(0x803d80, 0xc00001c078, 0xc00002400c, 0x5a, 0xc00001e053, 0x6, 0xc0000200a0, 0x13, 0xc000012e10, 0x0, ...)
	/go/pkg/mod/github.com/kvrhdn/[email protected]/tferun.go:188 +0x1483
main.main()
	/app/main.go:52 +0x31f

The runs on Terraform Cloud where not impact and completed successfully.

Occurences

Integration run 30 Aug. 10:21 UTC: https://github.com/kvrhdn/tfe-run/runs/1047358751?check_suite_focus=true#step:9:51
Integration run 30 Aug. 14:16 UTC: https://github.com/kvrhdn/tfe-run/runs/1047790479?check_suite_focus=true#step:9:51
Integration run 30 Aug, 23:15 UTC https://github.com/kvrhdn/tfe-run/runs/1048796083?check_suite_focus=true#step:9:51

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.