From 37a417e5ae86b0dff4f7d3f925f381e4b5be6497 Mon Sep 17 00:00:00 2001 From: somaz <112675579+somaz94@users.noreply.github.com> Date: Sat, 11 Jul 2026 00:37:53 +0900 Subject: [PATCH] feat: support .tool-versions (asdf format) in tofu_version_file (#131) Signed-off-by: somaz --- README.md | 5 +++++ dist/index.js | 11 ++++++++--- lib/setup-tofu.js | 11 ++++++++--- lib/test/setup-tofu.test.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f2445a7..51e1eb6 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,11 @@ steps: tofu_version_file: .opentofu-version ``` +`tofu_version_file` accepts two formats: + +- A single-line file containing just the version (e.g., `.opentofu-version` with `1.9.0`). +- An asdf-format `.tool-versions` file containing an `opentofu ` line. Other tool entries in the same file are ignored. + Supported version syntax is the same as for the `tofu_version` input. If both `tofu_version` and `tofu_version_file` are provided, the version number in the file takes precedence. Credentials for Terraform Cloud ([app.terraform.io](https://app.terraform.io/)) can be configured: diff --git a/dist/index.js b/dist/index.js index ade9443..045790b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -35234,9 +35234,14 @@ async function run () { try { core_debug(`Reading OpenTofu version from file: ${versionFile}`); const fileVersion = await external_fs_namespaceObject.promises.readFile(versionFile, 'utf8'); - const trimmedVersion = fileVersion.trim(); - if (trimmedVersion) { - version = trimmedVersion; + // .tool-versions (asdf format): use the version from the `opentofu ` + // line and ignore other tool entries. Otherwise (e.g. a single-line + // `.opentofu-version` file): use the whole trimmed file content. + // Spec: https://asdf-vm.com/manage/configuration.html#tool-versions + const asdfMatch = fileVersion.match(/^\s*opentofu\s+([^\s#]+)/m); + const parsedVersion = asdfMatch ? asdfMatch[1] : fileVersion.trim(); + if (parsedVersion) { + version = parsedVersion; core_debug(`Using version from file: ${version}`); } else { warning( diff --git a/lib/setup-tofu.js b/lib/setup-tofu.js index 47e8b01..c40680b 100644 --- a/lib/setup-tofu.js +++ b/lib/setup-tofu.js @@ -231,9 +231,14 @@ async function run () { try { debug(`Reading OpenTofu version from file: ${versionFile}`); const fileVersion = await fs.readFile(versionFile, 'utf8'); - const trimmedVersion = fileVersion.trim(); - if (trimmedVersion) { - version = trimmedVersion; + // .tool-versions (asdf format): use the version from the `opentofu ` + // line and ignore other tool entries. Otherwise (e.g. a single-line + // `.opentofu-version` file): use the whole trimmed file content. + // Spec: https://asdf-vm.com/manage/configuration.html#tool-versions + const asdfMatch = fileVersion.match(/^\s*opentofu\s+([^\s#]+)/m); + const parsedVersion = asdfMatch ? asdfMatch[1] : fileVersion.trim(); + if (parsedVersion) { + version = parsedVersion; debug(`Using version from file: ${version}`); } else { warning( diff --git a/lib/test/setup-tofu.test.js b/lib/test/setup-tofu.test.js index 652df86..9913b2c 100644 --- a/lib/test/setup-tofu.test.js +++ b/lib/test/setup-tofu.test.js @@ -225,6 +225,34 @@ describe('setup-tofu', () => { expect(fs.readFile).not.toHaveBeenCalled(); }); + + it('should parse the opentofu version from a .tool-versions (asdf) file', async () => { + jest.spyOn(fs, 'readFile'); + + const asdfVersion = '1.9.0'; + versionFileName = '.tool-versions'; + versionFile = join(tempDir, versionFileName); + await fs.writeFile(versionFile, `nodejs 24.14.0\nopentofu ${asdfVersion}\nterraform 1.5.0\n`); + + getInput.mockImplementation((name) => { + if (name === 'tofu_version_file') { + return versionFile; + } + if (name === 'tofu_version') { + return fallbackVersion; + } + return ''; + }); + + await setup(); + + expect(getRelease).toHaveBeenCalledWith( + asdfVersion, + process.env.GITHUB_TOKEN + ); + + expect(fs.readFile).toHaveBeenCalled(); + }); }); describe('caching functionality', () => {