Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read terraform version from a .terraformrc file #208

Open
nitrocode opened this issue May 25, 2022 · 9 comments
Open

Read terraform version from a .terraformrc file #208

nitrocode opened this issue May 25, 2022 · 9 comments
Assignees
Labels
tf-devex-triage Terraform DevEx project tracking

Comments

@nitrocode
Copy link

nitrocode commented May 25, 2022

Instead of hard coding the version in the workflow, it would be nice to have it read it from a file similar to how actions/setup-node@v3 reads from .nvmrc

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'

Something like this perhaps ?

      - uses: actions/checkout@v3
      - uses: hashicorp/setup-terraform@v2
        with:
          terraform_version_file: '.terraformrc'
@jasonhuling
Copy link

I second this, but would prefer pulling from .terraform-version or from required_version in the terraform block.

@RichiCoder1
Copy link

+1 specifically for .terraform-version. Would be willing to PR that too.

@jgrumboe
Copy link

jgrumboe commented Jun 7, 2022

I also believe .terraform-version is better, because tfenv also respects it - so "two birds with one stone" 😁

@marcofranssen
Copy link

👍 for the .terraform-version.

@iniinikoski
Copy link

iniinikoski commented Aug 17, 2022

Really good idea, to use .terraform-version. Mostly because, .terraformrc is reserved for this use: https://www.terraform.io/cli/config/config-file - and this could create confusion

@karelbemelmans
Copy link

It would be nice to have it in the setup action, but for now you can just do it this way:

      - name: Set variables
        run: |
          TF_VERSION=$(cat .terraform-version)
          echo "TF_VERSION=$TF_VERSION" >> $GITHUB_ENV

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: ${{ env.TF_VERSION }}

@nitrocode
Copy link
Author

@karelbemelmans that's a good workaround.

I would propose a minor tweak to the version retrieval to allow comments in the file

      - name: Set variables
        run: |
          TF_VERSION=$(grep -vP '^[\s]?#' .terraform-version)
          echo "TF_VERSION=$TF_VERSION" >> $GITHUB_ENV
✗ cat .terraform-version
# this is a good version
1.2.3
✗ grep -vP '^[\s]?#' .terraform-version
1.2.3

@stevehipwell
Copy link

@karelbemelmans the spec for .terraform-version supports using a regex to calculate the correct version which isn't compatible with this action or the library used to download Terraform; these both need Terraform compatible version ranges. To support .terraform-version spec regex patterns you'd need an additional step to download the available versions and grep through them (grep -e in the docs).

@tush-tr
Copy link

tush-tr commented May 26, 2023

/assign

@bflad bflad added the tf-devex-triage Terraform DevEx project tracking label Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tf-devex-triage Terraform DevEx project tracking
Projects
None yet
Development

No branches or pull requests

10 participants