feat: implement support for the terraform_version_file

fix
This commit is contained in:
Victor Martinez 2024-12-09 15:25:13 +01:00
parent c769acde7b
commit 409343f6e4
No known key found for this signature in database
GPG key ID: 3EF267DA33D65715
2 changed files with 168 additions and 1 deletions

View file

@ -121,14 +121,70 @@ credentials "${credentialsHostname}" {
await fs.writeFile(credsFile, creds); await fs.writeFile(credsFile, creds);
} }
async function getVersionFromFileContent (versionFile) {
if (!versionFile) {
return;
}
let versionRegExp;
const versionFileName = path.basename(versionFile);
if (versionFileName === '.tool-versions') {
versionRegExp = /^(terraform\s+)(?<version>[^\s]+)$/m;
} else if (versionFileName) {
versionRegExp = /(?<version>[^\s]+)/;
} else {
return;
}
try {
const content = fs.readFileSync(versionFile).toString().trim();
let fileContent = '';
if (content.match(versionRegExp)?.groups?.version) {
fileContent = content.match(versionRegExp)?.groups?.version;
}
if (!fileContent) {
return;
}
core.debug(`Version from file '${fileContent}'`);
return fileContent;
} catch (error) {
if (error.code === 'ENOENT') {
return;
}
throw error;
}
}
// get the Terraform version from the action inputs
async function getTerraformVersion (versionInput, versionFile) {
const DEFAULT_VERSION = 'latest';
let version = versionInput;
if (!versionInput && !versionFile) {
core.info(`Set default value for version to ${DEFAULT_VERSION}`);
version = DEFAULT_VERSION;
}
if (!version && versionFile) {
version = await getVersionFromFileContent(versionFile);
if (!version) {
throw new Error(`No supported version was found in file ${versionFile}`);
}
}
return version;
}
async function run () { async function run () {
try { try {
// Gather GitHub Actions inputs // Gather GitHub Actions inputs
const version = core.getInput('terraform_version'); const versionInput = core.getInput('terraform_version', { required: false });
const versionFile = core.getInput('terraform_version_file', { required: false });
const credentialsHostname = core.getInput('cli_config_credentials_hostname'); const credentialsHostname = core.getInput('cli_config_credentials_hostname');
const credentialsToken = core.getInput('cli_config_credentials_token'); const credentialsToken = core.getInput('cli_config_credentials_token');
const wrapper = core.getInput('terraform_wrapper') === 'true'; const wrapper = core.getInput('terraform_wrapper') === 'true';
const version = await getTerraformVersion(versionInput, versionFile);
// Gather OS details // Gather OS details
const osPlatform = os.platform(); const osPlatform = os.platform();
const osArch = os.arch(); const osArch = os.arch();

View file

@ -44,12 +44,14 @@ describe('Setup Terraform', () => {
test('gets specific version and adds token and hostname on linux, amd64', async () => { test('gets specific version and adds token and hostname on linux, amd64', async () => {
const version = '0.1.1'; const version = '0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -86,12 +88,14 @@ describe('Setup Terraform', () => {
test('gets specific version and adds token and hostname on windows, 386', async () => { test('gets specific version and adds token and hostname on windows, 386', async () => {
const version = '0.1.1'; const version = '0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -131,12 +135,14 @@ describe('Setup Terraform', () => {
test('gets latest version and adds token and hostname on linux, amd64', async () => { test('gets latest version and adds token and hostname on linux, amd64', async () => {
const version = 'latest'; const version = 'latest';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -174,12 +180,14 @@ describe('Setup Terraform', () => {
test('gets latest version matching specification adds token and hostname on linux, amd64', async () => { test('gets latest version matching specification adds token and hostname on linux, amd64', async () => {
const version = '<0.10.0'; const version = '<0.10.0';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -217,12 +225,14 @@ describe('Setup Terraform', () => {
test('gets latest version matching tilde range patch', async () => { test('gets latest version matching tilde range patch', async () => {
const version = '~0.1.0'; const version = '~0.1.0';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -259,12 +269,14 @@ describe('Setup Terraform', () => {
test('gets latest version matching tilde range minor', async () => { test('gets latest version matching tilde range minor', async () => {
const version = '~0.1'; const version = '~0.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -301,12 +313,14 @@ describe('Setup Terraform', () => {
test('gets latest version matching tilde range minor', async () => { test('gets latest version matching tilde range minor', async () => {
const version = '~0'; const version = '~0';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -343,12 +357,14 @@ describe('Setup Terraform', () => {
test('gets latest version matching .X range ', async () => { test('gets latest version matching .X range ', async () => {
const version = '0.1.x'; const version = '0.1.x';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -385,12 +401,14 @@ describe('Setup Terraform', () => {
test('gets latest version matching - range ', async () => { test('gets latest version matching - range ', async () => {
const version = '0.1.0 - 0.1.1'; const version = '0.1.0 - 0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -427,12 +445,14 @@ describe('Setup Terraform', () => {
test('fails when metadata cannot be downloaded', async () => { test('fails when metadata cannot be downloaded', async () => {
const version = 'latest'; const version = 'latest';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -449,12 +469,14 @@ describe('Setup Terraform', () => {
test('fails when specific version cannot be found', async () => { test('fails when specific version cannot be found', async () => {
const version = '0.9.9'; const version = '0.9.9';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -471,12 +493,14 @@ describe('Setup Terraform', () => {
test('fails when CLI for os and architecture cannot be found', async () => { test('fails when CLI for os and architecture cannot be found', async () => {
const version = '0.1.1'; const version = '0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -509,12 +533,14 @@ describe('Setup Terraform', () => {
test('fails when CLI cannot be downloaded', async () => { test('fails when CLI cannot be downloaded', async () => {
const version = '0.1.1'; const version = '0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken); .mockReturnValueOnce(credentialsToken);
@ -547,6 +573,7 @@ describe('Setup Terraform', () => {
test('installs wrapper on linux', async () => { test('installs wrapper on linux', async () => {
const version = '0.1.1'; const version = '0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
const wrapperPath = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep)); const wrapperPath = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep));
@ -559,6 +586,7 @@ describe('Setup Terraform', () => {
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken) .mockReturnValueOnce(credentialsToken)
.mockReturnValueOnce('true'); .mockReturnValueOnce('true');
@ -591,6 +619,7 @@ describe('Setup Terraform', () => {
test('installs wrapper on windows', async () => { test('installs wrapper on windows', async () => {
const version = '0.1.1'; const version = '0.1.1';
const versionFile = '';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl'; const credentialsToken = 'asdfjkl';
const wrapperPath = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep)); const wrapperPath = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep));
@ -603,6 +632,7 @@ describe('Setup Terraform', () => {
core.getInput = jest core.getInput = jest
.fn() .fn()
.mockReturnValueOnce(version) .mockReturnValueOnce(version)
.mockReturnValueOnce(versionFile)
.mockReturnValueOnce(credentialsHostname) .mockReturnValueOnce(credentialsHostname)
.mockReturnValueOnce(credentialsToken) .mockReturnValueOnce(credentialsToken)
.mockReturnValueOnce('true'); .mockReturnValueOnce('true');
@ -632,4 +662,85 @@ describe('Setup Terraform', () => {
expect(ioMv).toHaveBeenCalledWith(`file${path.sep}terraform.exe`, `file${path.sep}terraform-bin.exe`); expect(ioMv).toHaveBeenCalledWith(`file${path.sep}terraform.exe`, `file${path.sep}terraform-bin.exe`);
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 () => {
const version = '';
const versionFile = '.tool-versions';
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('terraform 0.10.0');
const versionObj = await setup();
expect(versionObj.version).toEqual('0.10.0');
});
test('gets version from version if both (version and .tool-versions file) are set', async () => {
const version = '0.1.0';
const versionFile = '.tool-versions';
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('terraform 0.10.0');
const versionObj = await setup();
expect(versionObj.version).toEqual('0.1.0');
});
}); });