diff --git a/.github/workflows/test-version.yaml b/.github/workflows/test-version.yaml index 61f8dec2..bf869c82 100644 --- a/.github/workflows/test-version.yaml +++ b/.github/workflows/test-version.yaml @@ -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 tofu-version + uses: ./tofu-version + id: tofu-version + with: + path: tests/workflows/test-version/tofuenv + + - name: Check the version + env: + DETECTED_TERRAFORM_VERSION: ${{ steps.tofu-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 diff --git a/image/src/terraform_version/__main__.py b/image/src/terraform_version/__main__.py index 492ff55a..d6bbf0dc 100644 --- a/image/src/terraform_version/__main__.py +++ b/image/src/terraform_version/__main__.py @@ -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 diff --git a/image/src/terraform_version/tfenv.py b/image/src/terraform_version/tfenv.py index 3842faeb..4b2f11ef 100644 --- a/image/src/terraform_version/tfenv.py +++ b/image/src/terraform_version/tfenv.py @@ -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 diff --git a/terraform-version/README.md b/terraform-version/README.md index ecddf9e2..c81e88bb 100644 --- a/terraform-version/README.md +++ b/terraform-version/README.md @@ -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. diff --git a/tests/workflows/test-version/tofuenv/.opentofu-version b/tests/workflows/test-version/tofuenv/.opentofu-version new file mode 100644 index 00000000..ff2fd4fb --- /dev/null +++ b/tests/workflows/test-version/tofuenv/.opentofu-version @@ -0,0 +1 @@ +1.8.5 \ No newline at end of file diff --git a/tests/workflows/test-version/tofuenv/main.tf b/tests/workflows/test-version/tofuenv/main.tf new file mode 100644 index 00000000..e69de29b diff --git a/tofu-version/README.md b/tofu-version/README.md index 3908a500..0f34dd95 100644 --- a/tofu-version/README.md +++ b/tofu-version/README.md @@ -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.