Skip to content
box

GitHub Action

tfswitch

v1.0.1 Latest version

tfswitch

box

tfswitch

Setup tfswitch to install a specific version of terraform

Installation

Copy and paste the following snippet into your .yml file.

              

- name: tfswitch

uses: stv-io/[email protected]

Learn more about this action in stv-io/action-tfswitch

Choose a version

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