A github action to run tfswitch as a composite action
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.
To use tfswitch github action, configure a YAML workflow file, e.g.
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
...
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
...
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
...
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) .."
Output | Type | Description |
---|---|---|
terraform_version | string | The version of terraform installed |
This action unlinks the default installed terraform which is available in the runner.