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

Support resolving terraform version from config #407

Open
dkirrane opened this issue Apr 4, 2024 · 4 comments
Open

Support resolving terraform version from config #407

dkirrane opened this issue Apr 4, 2024 · 4 comments

Comments

@dkirrane
Copy link

dkirrane commented Apr 4, 2024

I'm looking for a feature similar to actions/setup-go@v5 https://github.com/actions/setup-go?tab=readme-ov-file#getting-go-version-from-the-gomod-file

If we could pass a terraform-version-file that takes a path to a .tf file containing the terraform block with required_version e.g.

versions.tf

terraform {
  required_version = "1.3.6"
}

action

uses: hashicorp/setup-terraform@v3
with:
    terraform-version-file: 'path/to/versions.tf'

Or better still just automatically find the .tf file with required_version config

@vsamofal
Copy link

I do use this action to get a version dflook/terraform-version@v1 (cool to have it built in, but at least some workaround)

@guitarrapc
Copy link

FYI, I've written bash 1 liner to retrieve required_version and use it with setup-terraform. If you are using simple version string, "= 1.8.4" and ~> 1.8.0, it works fine. Complex versioning is out of scope.

https://gist.github.com/guitarrapc/371018c583e409990daba76df7f062f5

@antm-pp
Copy link

antm-pp commented Jul 15, 2024

I don't think this does work as the true terraform required_version block is not semver (which setup_terraform uses)

required_version = "~> 1.8"

In terraform init that's read as only allow changes to rightmost value, so in current builds 1.9.3 etc would be valid. Things like tfswitch read it the same way as terraform and pull the correct version.

In setup_terraform this gets read as semver allow any patch value not exceeding 1.8 eg 1.8.5 and not 1.9.x. Therefore you will get inconsistent results.

Not sure if that's what was meant about the 'complex versioning' limitation in the above comment. But really these things should all work consistently.

@viktor-aura
Copy link

Here is my solution based on @guitarrapc :

run: |
    test -f versions.tf || { echo ERROR: versions.tf not found ; exit 1 ; }
    version=$(\
        grep -E 'required_version\s*=\s*"([^"]+)"' versions.tf \
        | sed -n 's/\s*required_version[[:space:]]*=[[:space:]]*\(".*"\)/\1/p' \
        | sed -e 's/v//g;s/[[:space:]]//g' \
        | sed -e '/~/s/"~>[[:space:]]*\(.*\)"/~\1/')
    echo "value=${version}" | tee -a "${GITHUB_OUTPUT}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants