hashicorp / setup-terraform

Sets up Terraform CLI in your GitHub Actions workflow.
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions
Mozilla Public License 2.0
1.36k stars 236 forks source link

Read terraform version from a .terraformrc file #208

Open nitrocode opened 2 years ago

nitrocode commented 2 years ago

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 commented 2 years ago

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

RichiCoder1 commented 2 years ago

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

jgrumboe commented 2 years ago

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

marcofranssen commented 2 years ago

:+1: for the .terraform-version.

iniinikoski commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

@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 commented 1 year ago

/assign