Skip to content

Commit 47e0634

Browse files
committed
Add support for detecting OpenTofu version from .opentofu-version files.
1 parent 7f1907d commit 47e0634

File tree

7 files changed

+50
-15
lines changed

7 files changed

+50
-15
lines changed

.github/workflows/test-version.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ jobs:
137137
exit 1
138138
fi
139139

140+
tofuenv:
141+
runs-on: ubuntu-24.04
142+
name: tofuenv
143+
steps:
144+
- name: Checkout
145+
uses: actions/checkout@v4
146+
with:
147+
persist-credentials: false
148+
149+
- name: Test tofu-version
150+
uses: ./tofu-version
151+
id: tofu-version
152+
with:
153+
path: tests/workflows/test-version/tofuenv
154+
155+
- name: Check the version
156+
env:
157+
DETECTED_TERRAFORM_VERSION: ${{ steps.tofu-version.outputs.terraform }}
158+
run: |
159+
echo "The terraform version was $DETECTED_TERRAFORM_VERSION"
160+
161+
if [[ "$DETECTED_TERRAFORM_VERSION" != "1.8.5" ]]; then
162+
echo "::error:: Terraform version not set from .opentofu-version"
163+
exit 1
164+
fi
165+
140166
asdf:
141167
runs-on: ubuntu-24.04
142168
name: asdf

image/src/terraform_version/__main__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ def determine_version(inputs: InitInputs, cli_config_path: Path, actions_env: Ac
5151
sys.stdout.write(f'Using {version.product} version specified in .tfswitchrc file\n')
5252
return version
5353

54-
if version := try_read_tfenv(inputs, versions):
54+
if 'OPENTOFU' in os.environ:
55+
if version := try_read_tfenv('.opentofu-version', inputs, versions):
56+
sys.stdout.write(f'Using {version.product} version specified in .opentofu-version file\n')
57+
return version
58+
59+
if version := try_read_tfenv('.terraform-version', inputs, versions):
5560
sys.stdout.write(f'Using {version.product} version specified in .terraform-version file\n')
5661
return version
5762

image/src/terraform_version/tfenv.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ def parse_tfenv(terraform_version_file: str, versions: Iterable[Version]) -> Ver
3838
return Version(version)
3939

4040

41-
def try_read_tfenv(inputs: InitInputs, versions: Iterable[Version]) -> Optional[Version]:
41+
def try_read_tfenv(filename: str, inputs: InitInputs, versions: Iterable[Version]) -> Optional[Version]:
4242
"""
4343
Return the terraform version specified by any .terraform-version file.
4444

45+
:param filename: The name of the version file
4546
:param inputs: The action inputs
4647
:param versions: The available terraform versions
4748
:returns: The terraform version specified by any .terraform-version file, which may be None.
4849
"""
4950

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

5253
if not os.path.exists(tfenv_path):
5354
return None

terraform-version/README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ The version to use is discovered from the first of:
1111
2. A [`required_version`](https://www.terraform.io/docs/configuration/terraform.html#specifying-a-required-terraform-version)
1212
constraint in the Terraform configuration. If the constraint is range, the latest matching version is used.
1313
3. A [tfswitch](https://warrensbox.github.io/terraform-switcher/) `.tfswitchrc` file in the module path
14-
4. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
15-
5. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
16-
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.
17-
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.
18-
8. The Terraform version that created the current state file (best effort).
19-
9. The latest Terraform version
14+
4. When using a `dflook/tofu-*` action, a [tofuenv](https://github.com/tofuutils/tofuenv) `.opentofu-version` file in the module path
15+
5. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
16+
6. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
17+
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.
18+
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.
19+
9. The Terraform version that created the current state file (best effort).
20+
10. The latest Terraform version
2021

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.8.5

tests/workflows/test-version/tofuenv/main.tf

Whitespace-only changes.

tofu-version/README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ The version to use is discovered from the first of:
1111
2. A [`required_version`](https://www.terraform.io/docs/configuration/terraform.html#specifying-a-required-terraform-version)
1212
constraint in the OpenTofu configuration. If the constraint is range, the latest matching version is used.
1313
3. A [tfswitch](https://warrensbox.github.io/terraform-switcher/) `.tfswitchrc` file in the module path
14-
4. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
15-
5. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
16-
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.
17-
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.
18-
8. The OpenTofu version that created the current state file (best effort).
19-
9. The latest OpenTofu version
14+
4. When using a `dflook/tofu-*` action, a [tofuenv](https://github.com/tofuutils/tofuenv) `.opentofu-version` file in the module path
15+
5. A [tfenv](https://github.com/tfutils/tfenv) `.terraform-version` file in the module path
16+
6. An [asdf](https://asdf-vm.com/) `.tool-versions` file in the module path or any parent path
17+
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.
18+
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.
19+
9. The OpenTofu version that created the current state file (best effort).
20+
10. The latest OpenTofu version
2021

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

0 commit comments

Comments
 (0)