use .terraform-version

This commit is contained in:
Victor Martinez 2024-12-11 21:32:18 +01:00
parent c7f7145902
commit 149638b372
No known key found for this signature in database
GPG key ID: 3EF267DA33D65715
3 changed files with 89 additions and 6 deletions

View file

@ -260,7 +260,7 @@ The action supports the following inputs:
for available range specifications). Examples are: `"<1.2.0"`, `"~1.1.0"`, `"1.1.7"` (all three installing for available range specifications). Examples are: `"<1.2.0"`, `"~1.1.0"`, `"1.1.7"` (all three installing
the latest available `1.1` version). Prerelease versions can be specified and a range will stay within the the latest available `1.1` version). Prerelease versions can be specified and a range will stay within the
given tag such as `beta` or `rc`. If no version is given, it will default to `latest`. given tag such as `beta` or `rc`. If no version is given, it will default to `latest`.
- `terraform_version_file` - (optional) The path to a file containing terraform version. Supported file types are `.tool-versions` or anything else. See more details in [about version-file](#Terraform-version-file). - `terraform_version_file` - (optional) The path to a file containing terraform version. Supported file types are `.tool-versions` or `.terraform-version`. See more details in [about version-file](#Terraform-version-file).
- `terraform_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of - `terraform_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of
the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs
named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`. named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.
@ -269,7 +269,7 @@ The action supports the following inputs:
If the `terraform_version_file` input is specified, the action will extract the version from the file and install it. If the `terraform_version_file` input is specified, the action will extract the version from the file and install it.
Supported files names are `.tool-versions` or anything else. Supported files names are `.tool-versions` or `.terraform-version`.
In `.tool-versions` file, terraform version should be preceded by the terraform keyword (e.g., `terraform 1.13.0`). In `.tool-versions` file, terraform version should be preceded by the terraform keyword (e.g., `terraform 1.13.0`).
The `.tool-versions` file supports version specifications in accordance with Semantic Versioning ([semver](https://semver.org/)) and [Semver Ranges](https://www.npmjs.com/package/semver#ranges). The `.tool-versions` file supports version specifications in accordance with Semantic Versioning ([semver](https://semver.org/)) and [Semver Ranges](https://www.npmjs.com/package/semver#ranges).
In other files, the version should be specified as explainied in the `terraform_version` input. In other files, the version should be specified as explainied in the `terraform_version` input.

View file

@ -130,7 +130,7 @@ async function getVersionFromFileContent (versionFile) {
const versionFileName = path.basename(versionFile); const versionFileName = path.basename(versionFile);
if (versionFileName === '.tool-versions') { if (versionFileName === '.tool-versions') {
versionRegExp = /^(terraform\s+)(?<version>[^\s]+)$/m; versionRegExp = /^(terraform\s+)(?<version>[^\s]+)$/m;
} else if (versionFileName) { } else if (versionFileName === '.terraform-version') {
versionRegExp = /(?<version>[^\s]+)/; versionRegExp = /(?<version>[^\s]+)/;
} else { } else {
return; return;

View file

@ -663,7 +663,7 @@ describe('Setup Terraform', () => {
expect(ioCp).toHaveBeenCalledWith(wrapperPath, `file${path.sep}terraform`); expect(ioCp).toHaveBeenCalledWith(wrapperPath, `file${path.sep}terraform`);
}); });
test('gets version from .tool-versions file and adds token and hostname on linux, amd64', async () => { test('gets version from .tool-versions file', async () => {
const version = ''; const version = '';
const versionFile = '.tool-versions'; const versionFile = '.tool-versions';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
@ -697,10 +697,10 @@ describe('Setup Terraform', () => {
fs.readFileSync = jest fs.readFileSync = jest
.fn() .fn()
.mockReturnValueOnce('terraform 0.10.0'); .mockReturnValueOnce('terraform 0.1.1');
const versionObj = await setup(); const versionObj = await setup();
expect(versionObj.version).toEqual('0.10.0'); expect(versionObj.version).toEqual('0.1.1');
}); });
test('gets version from version if both (version and .tool-versions file) are set', async () => { test('gets version from version if both (version and .tool-versions file) are set', async () => {
@ -780,4 +780,87 @@ describe('Setup Terraform', () => {
const versionObj = await setup(); const versionObj = await setup();
expect(versionObj.version).toEqual('0.10.0'); expect(versionObj.version).toEqual('0.10.0');
}); });
test('gets version from .terraform-version file', async () => {
const version = '';
const versionFile = '.terraform-version';
const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl';
core.getInput = jest
.fn()
.mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken);
tc.downloadTool = jest
.fn()
.mockReturnValueOnce('file.zip');
tc.extractZip = jest
.fn()
.mockReturnValueOnce('file');
os.platform = jest
.fn()
.mockReturnValue('linux');
os.arch = jest
.fn()
.mockReturnValue('amd64');
nock('https://releases.hashicorp.com')
.get('/terraform/index.json')
.reply(200, json);
fs.readFileSync = jest
.fn()
.mockReturnValueOnce('0.1.1');
const versionObj = await setup();
expect(versionObj.version).toEqual('0.1.1');
});
test('fails when unsupported terraform version file', async () => {
const version = '';
const versionFile = 'unsupported-file';
const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl';
core.getInput = jest
.fn()
.mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken);
tc.downloadTool = jest
.fn()
.mockReturnValueOnce('file.zip');
tc.extractZip = jest
.fn()
.mockReturnValueOnce('file');
os.platform = jest
.fn()
.mockReturnValue('linux');
os.arch = jest
.fn()
.mockReturnValue('amd64');
nock('https://releases.hashicorp.com')
.get('/terraform/index.json')
.reply(200, json);
fs.readFileSync = jest
.fn()
.mockReturnValueOnce('0.10.0');
try {
await setup();
} catch (e) {
expect(core.error).toHaveBeenCalled();
}
});
}); });