Git Product home page Git Product logo

terraform-cloud-action's Introduction

terraform-cloud-action

standard-readme compliant Build Status codecov

An action to run Terraform Cloud workspaces

This action submits a run to a Terraform Cloud workspace which performs a plan and apply. The action can be configured to return a success once the run is succesfully submitted, or it can wait for the plan to be applied by periodically polling Terraform Cloud for run status updates.

Table of Contents

Usage

Terraform Cloud requires a .tar.gz archive containing the Terraform configuration, and build artifacts if required. The example shows a GitHub workflow archiving Lambda functions (in the build directory) alongside a Terraform configuration (in the infrastructure directory) for deployment. The archive is then passed to the action for deployment by Terraform Cloud.

- name: Create tar gz file
  run: tar --exclude *.terraform* -zcvf build.tar.gz build infrastructure

- name: Terraform Cloud
  uses: addresscloud/[email protected]
  with:
    tfToken: ${{ secrets.TERRAFORM_TOKEN }}
    tfOrg: '<ORGANISATION>'
    tfWorkspace: 'my-lambda-service'
    filePath: './build.tar.gz'
    identifier: ${{ github.sha }}

If the awaitApply variable is set then once a run is confirmed the Action will poll Terraform Cloud and return success when the run status is either planned_and_finished or applied. In this example the Action will poll for updates every 60 seconds, for a maximum of 5 times. If the limit is reached before the run is complete the Action will return an error.

- name: Create tar gz file
  run: tar --exclude *.terraform* -zcvf build.tar.gz build infrastructure

- name: Terraform Cloud
  uses: addresscloud/[email protected]
  with:
    tfToken: ${{ secrets.TERRAFORM_TOKEN }}
    tfOrg: '<ORGANISATION>'
    tfWorkspace: 'my-lambda-service'
    filePath: './build.tar.gz'
    identifier: ${{ github.sha }}
    awaitApply: true
    awaitInterval: 60
    retryLimit: 5

Inputs

The inputs below are required by the action to submit the run to Terraform Cloud. Additional workspace variables and settings should be configured using the Terraform Cloud UI.

tfToken

Required - Terraform Cloud access token.

tfOrganization

Required - Terraform Cloud Organization.

tfWorkspace

Required - Name of existing Terraform Cloud workspace.

filePath

Required - Path to .tar.gz archive with Terraform configuration.

identifier

Required - Unique identifier for the run (e.g. git commit sha). Reduced to 7 characters for brevity.

awaitApply

Wait for run to apply before completing.

Default: false

awaitInterval

Duration to wait (seconds) between sunsequent run status requests.

Default: 60

retryLimit

Number of times to retry Terraform Cloud API requests.

Default: 5

Outputs

runId

The identfier of the run in Terraform Cloud.

status

The status of the run in Terraform Cloud.

Note on Terraform Modules

If your repository contains multiple modules, upload the top-level directory and configure the root workspace path in the Terraform Cloud UI. For example, to deploy infrastructure/dev/services/lambda/main.tf which has references to modules in infrastructure/modules/services/lambda/module.tf upload the entire infrastructure directory and configure infrastructure/dev/services/lambda/ as the root of the workspace in the Terraform Cloud UI.

Maintainers

@tomasholderness

Contributing

PRs accepted with unit tests.

To run tests:

npm run test

To check code lint:

npm run lint

Small note: If editing the README, please conform to the standard-readme specification.

This Action was based on the example Terraform Enterprise script at: https://github.com/hashicorp/terraform-guides/tree/master/operations/automation-script

License

MPL-2.0 © 2020 Addresscloud Limited

terraform-cloud-action's People

Contributors

dependabot[bot] avatar tomasholderness avatar

Stargazers

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

Watchers

 avatar  avatar

terraform-cloud-action's Issues

unable to use v2 with awaitApply and additional inputs

When trying ot use the following

      - name: Terraform Cloud
        uses: addresscloud/[email protected]
        with:
          tfToken: ${{ secrets.TERRAFORM_TOKEN }}
          tfOrg: '<ORG>'
          tfWorkspace: '<WORKSPACE>'
          filePath: './build.tar.gz'
          identifier: ${{ github.sha }}
          awaitApply: true
          awaitInterval: 60
          retryLimit: 5

I get the following error
Unexpected input(s) 'awaitApply', 'awaitInterval', 'retryLimit', valid inputs are ['tfToken', 'tfOrg', 'tfWorkspace', 'filePath', 'identifier']

When attempting to use

      - name: Terraform Cloud
        uses: addresscloud/[email protected]
        with:
          tfToken: ${{ secrets.TERRAFORM_TOKEN }}
          tfOrg: '<ORG>'
          tfWorkspace: '<WORKSPACE>'
          filePath: './build.tar.gz'
          identifier: ${{ github.sha }}
          awaitApply: true
          awaitInterval: 60
          retryLimit: 5

I get the following error

Error checking the workspace: Request failed with status code 401

I'm assuming this is because the version 2 release has not been created and uploaded to the github action marketplace.

@tomasholderness is this possible to be done, this looks like a really useful action

Dots in `identifier` param terminate string

Hi,

Thanks for creating this action! It seems that if the identifier parameter contains a dot, the portion of the string after the dot will not get displayed in Terraform Cloud.

For example, if I send dev-v1.0 under the identifier, in terraform cloud it will show up as (dev-v1.)

Not sure if this is a Terraform Cloud API issue or something with this action but thought I'd mention it!

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.