mirror of
https://github.com/hashicorp/setup-terraform.git
synced 2025-12-17 08:52:37 +00:00
feat: implement support for the terraform_version_file
fix
This commit is contained in:
parent
c769acde7b
commit
409343f6e4
2 changed files with 168 additions and 1 deletions
|
|
@ -121,14 +121,70 @@ credentials "${credentialsHostname}" {
|
|||
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 () {
|
||||
try {
|
||||
// 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 credentialsToken = core.getInput('cli_config_credentials_token');
|
||||
const wrapper = core.getInput('terraform_wrapper') === 'true';
|
||||
|
||||
const version = await getTerraformVersion(versionInput, versionFile);
|
||||
|
||||
// Gather OS details
|
||||
const osPlatform = os.platform();
|
||||
const osArch = os.arch();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue