Skip to content

A github action to run tfswitch as a composite action

License

Notifications You must be signed in to change notification settings

stv-io/action-tfswitch

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

action-tfswitch

A github action to run tfswitch as a composite action

Why

The official hashicorp setup-terraform provides the same ability to configure the version, with a key difference that the version needs to be specified in the pipeline file. For cases where tfswitch is used in the development process, and the version is already defined, and checked in, in:

  • .terraform-version
  • versions.tf

.. or any other way supported by tfswitch, this provides for a seamless experience working locally, or in Github Actions.

Usage

To use tfswitch github action, configure a YAML workflow file, e.g.

Using the required_version block

Recommended - https://developer.hashicorp.com/terraform/language/settings#specifying-a-required-terraform-version

Contents of versions.tf

terraform {
  ...
  required_version = "1.6.3"
  ...
}

Workflow

jobs:
  tfswitch-version-from-required_version:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup tfswitch
      uses: stv-io/action-tfswitch@v1
    - name: Install
      run: |
        tfswitch
        terraform --version
        ...

Using environment variables

jobs:
  tfswitch-version-as-env-var:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup tfswitch
      uses: stv-io/action-tfswitch@v1
    - name: Install
      env:
        TF_VERSION: 1.6.2
      run: |
        tfswitch
        terraform --version
        ...

Specify version on the command line

jobs:
  tfswitch-version-as-parameter:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup tfswitch
      uses: stv-io/action-tfswitch@v1
    - name: Install
      run: |
        tfswitch 1.6.4
        terraform --version
        ...

Use specified version in terraform setup

jobs:
  export-version-as-output:
    outputs:
      terraform_version: ${{ steps.tfswitch-install.outputs.terraform_version }}  
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup tfswitch
      uses: stv-io/action-tfswitch@v1
    - name: Install
      id: tfswitch-install
      run: |
        tfswitch 1.6.4
        terraform --version
        ...
    - name: display version and path
      run: |
        echo "Terraform ${{steps.tfswitch-install.outputs.terraform_version }} installed to $(which terraform) .."

Outputs

Output Type Description
terraform_version string The version of terraform installed

Note

This action unlinks the default installed terraform which is available in the runner.

Credit

https://github.com/warrensbox/terraform-switcher

About

A github action to run tfswitch as a composite action

Resources

License

Stars

Watchers

Forks

Packages

No packages published