utilize .tool-versions file to resolve terraform version

This commit is contained in:
plukevdh 2023-05-23 15:50:27 -04:00
parent 1fdd4cd311
commit 5de63cedb5
No known key found for this signature in database
GPG key ID: 4CDF66F8A7057826
4 changed files with 158 additions and 5 deletions

View file

@ -28,6 +28,36 @@ function mapOS (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) {
core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url);
@ -38,7 +68,7 @@ async function downloadCLI (url) {
if (os.platform().startsWith('win')) {
core.debug(`Terraform CLI Download Path is ${pathToCLIZip}`);
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
io.mv(pathToCLIZip, fixedPathToCLIZip);
await io.mv(pathToCLIZip, fixedPathToCLIZip);
core.debug(`Moved download to ${fixedPathToCLIZip}`);
pathToCLI = await tc.extractZip(fixedPathToCLIZip);
} else {
@ -118,7 +148,7 @@ credentials "${credentialsHostname}" {
async function run () {
try {
// Gather GitHub Actions inputs
const version = core.getInput('terraform_version');
const version = await resolveVersion();
const credentialsHostname = core.getInput('cli_config_credentials_hostname');
const credentialsToken = core.getInput('cli_config_credentials_token');
const wrapper = core.getInput('terraform_wrapper') === 'true';