Skip to content

Commit

Permalink
Add support for detecting OpenTofu version from .opentofu-version files.
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Dec 21, 2024
1 parent 7f1907d commit 5a055c3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ jobs:
exit 1
fi
tofuenv:
runs-on: ubuntu-24.04
name: tofuenv
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Test terraform-version
uses: ./terraform-version
id: terraform-version
with:
path: tests/workflows/test-version/tofuenv

- name: Check the version
env:
DETECTED_TERRAFORM_VERSION: ${{ steps.terraform-version.outputs.terraform }}
run: |
echo "The terraform version was $DETECTED_TERRAFORM_VERSION"
if [[ "$DETECTED_TERRAFORM_VERSION" != "1.8.5" ]]; then
echo "::error:: Terraform version not set from .opentofu-version"
exit 1
fi
asdf:
runs-on: ubuntu-24.04
name: asdf
Expand Down
7 changes: 6 additions & 1 deletion image/src/terraform_version/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def determine_version(inputs: InitInputs, cli_config_path: Path, actions_env: Ac
sys.stdout.write(f'Using {version.product} version specified in .tfswitchrc file\n')
return version

if version := try_read_tfenv(inputs, versions):
if 'OPENTOFU' in os.environ:
if version := try_read_tfenv('.opentofu-version', inputs, versions):
sys.stdout.write(f'Using {version.product} version specified in .opentofu-version file\n')
return version

if version := try_read_tfenv('.terraform-version', inputs, versions):
sys.stdout.write(f'Using {version.product} version specified in .terraform-version file\n')
return version

Expand Down
5 changes: 3 additions & 2 deletions image/src/terraform_version/tfenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ def parse_tfenv(terraform_version_file: str, versions: Iterable[Version]) -> Ver
return Version(version)


def try_read_tfenv(inputs: InitInputs, versions: Iterable[Version]) -> Optional[Version]:
def try_read_tfenv(filename: str, inputs: InitInputs, versions: Iterable[Version]) -> Optional[Version]:
"""
Return the terraform version specified by any .terraform-version file.
:param filename: The name of the version file
:param inputs: The action inputs
:param versions: The available terraform versions
:returns: The terraform version specified by any .terraform-version file, which may be None.
"""

tfenv_path = os.path.join(inputs.get('INPUT_PATH', '.'), '.terraform-version')
tfenv_path = os.path.join(inputs.get('INPUT_PATH', '.'), filename)

if not os.path.exists(tfenv_path):
return None
Expand Down
13 changes: 7 additions & 6 deletions terraform-version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ The version to use is discovered from the first of:
2. A [`required_version`](https://www.terraform.io/docs/configuration/terraform.html#specifying-a-required-terraform-version)
constraint in the Terraform configuration. If the constraint is range, the latest matching version is used.
3. A [tfswitch](https://warrensbox.github.io/terraform-switcher/) `.tfswitchrc` file in the module path
4. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
5. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
6. An `OPENTOFU_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
7. A `TERRAFORM_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
8. The Terraform version that created the current state file (best effort).
9. The latest Terraform version
4. When using a `dflook/tofu-*` action, a [tofuenv](https://github.com/tofuutils/tofuenv) `.opentofu-version` file in the module path
5. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
6. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
7. An `OPENTOFU_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
8. A `TERRAFORM_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
9. The Terraform version that created the current state file (best effort).
10. The latest Terraform version

The version of Terraform and all required providers will be output to the workflow log.

Expand Down
1 change: 1 addition & 0 deletions tests/workflows/test-version/tofuenv/.opentofu-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.8.5
Empty file.
13 changes: 7 additions & 6 deletions tofu-version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ The version to use is discovered from the first of:
2. A [`required_version`](https://www.terraform.io/docs/configuration/terraform.html#specifying-a-required-terraform-version)
constraint in the OpenTofu configuration. If the constraint is range, the latest matching version is used.
3. A [tfswitch](https://warrensbox.github.io/terraform-switcher/) `.tfswitchrc` file in the module path
4. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
5. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
6. An `OPENTOFU_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
7. A `TERRAFORM_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
8. The OpenTofu version that created the current state file (best effort).
9. The latest OpenTofu version
4. When using a `dflook/tofu-*` action, a [tofuenv](https://github.com/tofuutils/tofuenv) `.opentofu-version` file in the module path
5. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
6. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
7. An `OPENTOFU_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
8. A `TERRAFORM_VERSION` environment variable containing a [version constraint](https://www.terraform.io/language/expressions/version-constraints). If the constraint allows multiple versions, the latest matching version is used.
9. The OpenTofu version that created the current state file (best effort).
10. The latest OpenTofu version

The version of OpenTofu and all required providers will be output to the workflow log.

Expand Down

0 comments on commit 5a055c3

Please sign in to comment.