This commit is contained in:
Luke van der Hoeven 2025-12-16 15:57:33 +00:00 committed by GitHub
commit 52f6782136
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 158 additions and 5 deletions

34
dist/index.js vendored
View file

@ -40,6 +40,36 @@ function mapOS (os) {
return mappings[os] || os; return mappings[os] || os;
} }
async function resolveVersion () {
let version = core.getInput('terraform_version');
if (!version) {
version = await getToolVersions();
}
return version;
}
async function getToolVersions () {
let version = '';
core.debug('Attempting to infer Terraform version from .tool-versions');
if (await fs.existsSync('.tool-versions')) {
const contents = await fs.readFileSync('.tool-versions').toString();
const match = contents.match(/^terraform (\d+(\.\d+)*)/m);
if (!match) {
core.debug('Could not find a valid terraform version from .tool-versions');
} else {
version = match[1];
}
} else {
core.debug('.tool-versions does not exist');
}
return version;
}
async function downloadCLI (url) { async function downloadCLI (url) {
core.debug(`Downloading Terraform CLI from ${url}`); core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url); const pathToCLIZip = await tc.downloadTool(url);
@ -50,7 +80,7 @@ async function downloadCLI (url) {
if (os.platform().startsWith('win')) { if (os.platform().startsWith('win')) {
core.debug(`Terraform CLI Download Path is ${pathToCLIZip}`); core.debug(`Terraform CLI Download Path is ${pathToCLIZip}`);
const fixedPathToCLIZip = `${pathToCLIZip}.zip`; const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
io.mv(pathToCLIZip, fixedPathToCLIZip); await io.mv(pathToCLIZip, fixedPathToCLIZip);
core.debug(`Moved download to ${fixedPathToCLIZip}`); core.debug(`Moved download to ${fixedPathToCLIZip}`);
pathToCLI = await tc.extractZip(fixedPathToCLIZip); pathToCLI = await tc.extractZip(fixedPathToCLIZip);
} else { } else {
@ -130,7 +160,7 @@ credentials "${credentialsHostname}" {
async function run () { async function run () {
try { try {
// Gather GitHub Actions inputs // Gather GitHub Actions inputs
const version = core.getInput('terraform_version'); const version = await resolveVersion();
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';

View file

@ -34,6 +34,36 @@ function mapOS (os) {
return mappings[os] || os; return mappings[os] || os;
} }
async function resolveVersion () {
let version = core.getInput('terraform_version');
if (!version) {
version = await getToolVersions();
}
return version;
}
async function getToolVersions () {
let version = '';
core.debug('Attempting to infer Terraform version from .tool-versions');
if (await fs.existsSync('.tool-versions')) {
const contents = await fs.readFileSync('.tool-versions').toString();
const match = contents.match(/^terraform (\d+(\.\d+)*)/m);
if (!match) {
core.debug('Could not find a valid terraform version from .tool-versions');
} else {
version = match[1];
}
} else {
core.debug('.tool-versions does not exist');
}
return version;
}
async function downloadCLI (url) { async function downloadCLI (url) {
core.debug(`Downloading Terraform CLI from ${url}`); core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url); const pathToCLIZip = await tc.downloadTool(url);
@ -44,7 +74,7 @@ async function downloadCLI (url) {
if (os.platform().startsWith('win')) { if (os.platform().startsWith('win')) {
core.debug(`Terraform CLI Download Path is ${pathToCLIZip}`); core.debug(`Terraform CLI Download Path is ${pathToCLIZip}`);
const fixedPathToCLIZip = `${pathToCLIZip}.zip`; const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
io.mv(pathToCLIZip, fixedPathToCLIZip); await io.mv(pathToCLIZip, fixedPathToCLIZip);
core.debug(`Moved download to ${fixedPathToCLIZip}`); core.debug(`Moved download to ${fixedPathToCLIZip}`);
pathToCLI = await tc.extractZip(fixedPathToCLIZip); pathToCLI = await tc.extractZip(fixedPathToCLIZip);
} else { } else {
@ -124,7 +154,7 @@ credentials "${credentialsHostname}" {
async function run () { async function run () {
try { try {
// Gather GitHub Actions inputs // Gather GitHub Actions inputs
const version = core.getInput('terraform_version'); const version = await resolveVersion();
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';

View file

@ -176,6 +176,46 @@
"url": "https://releases.hashicorp.com/terraform/0.10.0/terraform_0.10.0_windows_386.zip" "url": "https://releases.hashicorp.com/terraform/0.10.0/terraform_0.10.0_windows_386.zip"
} }
] ]
},
"1.3.7": {
"name": "terraform",
"version": "1.3.7",
"shasums": "terraform_1.3.7_SHA256SUMS",
"shasums_signature": "terraform_1.3.7_SHA256SUMS.sig",
"builds": [
{
"name": "terraform",
"version": "1.3.7",
"os": "darwin",
"arch": "amd64",
"filename": "terraform_1.3.7_darwin_amd64.zip",
"url": "https://releases.hashicorp.com/terraform/1.3.7/terraform_1.3.7_darwin_amd64.zip"
},
{
"name": "terraform",
"version": "1.3.7",
"os": "linux",
"arch": "386",
"filename": "terraform_1.3.7_linux_386.zip",
"url": "https://releases.hashicorp.com/terraform/1.3.7/terraform_1.3.7_linux_386.zip"
},
{
"name": "terraform",
"version": "1.3.7",
"os": "linux",
"arch": "amd64",
"filename": "terraform_1.3.7_linux_amd64.zip",
"url": "https://releases.hashicorp.com/terraform/1.3.7/terraform_1.3.7_linux_amd64.zip"
},
{
"name": "terraform",
"version": "1.3.7",
"os": "windows",
"arch": "386",
"filename": "terraform_1.3.7_windows_386.zip",
"url": "https://releases.hashicorp.com/terraform/1.3.7/terraform_1.3.7_windows_386.zip"
}
]
} }
} }
} }

View file

@ -161,7 +161,7 @@ describe('Setup Terraform', () => {
.reply(200, json); .reply(200, json);
const versionObj = await setup(); const versionObj = await setup();
expect(versionObj.version).toEqual('0.10.0'); expect(versionObj.version).toEqual('1.3.7');
// downloaded CLI has been added to path // downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled(); expect(core.addPath).toHaveBeenCalled();
@ -425,6 +425,59 @@ describe('Setup Terraform', () => {
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1); expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
}); });
test('gets version from .tool-versions when version not provided', async () => {
const credentialsHostname = 'app.terraform.io';
const credentialsToken = 'asdfjkl';
fs.existsSync = jest
.fn()
.mockReturnValueOnce(true);
fs.readFileSync = jest
.fn()
.mockReturnValueOnce(`terraform 1.3.7
golang 1.16.3
python 3.10.11
ruby 3.2.1
`);
core.getInput = jest
.fn()
.mockReturnValueOnce(null)
.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);
const versionObj = await setup();
expect(versionObj.version).toEqual('1.3.7');
// downloaded CLI has been added to path
expect(core.addPath).toHaveBeenCalled();
// expect credentials are in ${HOME}.terraformrc
const creds = await fs.readFile(`${process.env.HOME}/.terraformrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
test('fails when metadata cannot be downloaded', async () => { test('fails when metadata cannot be downloaded', async () => {
const version = 'latest'; const version = 'latest';
const credentialsHostname = 'app.terraform.io'; const credentialsHostname = 'app.terraform.io';