feat(versionfile): accept versionfile

This commit is contained in:
tush-tr 2023-05-27 04:13:39 +05:30
parent 1fdd4cd311
commit 6fac858356
3 changed files with 79 additions and 3 deletions

View file

@ -11,7 +11,9 @@ inputs:
required: false required: false
terraform_version: terraform_version:
description: 'The version of Terraform CLI to install. Instead of full version string you can also specify constraint string starting with "<" (for example `<1.13.0`) to install the latest version satisfying the constraint. A value of `latest` will install the latest version of Terraform CLI. Defaults to `latest`.' description: 'The version of Terraform CLI to install. Instead of full version string you can also specify constraint string starting with "<" (for example `<1.13.0`) to install the latest version satisfying the constraint. A value of `latest` will install the latest version of Terraform CLI. Defaults to `latest`.'
default: 'latest' required: false
terraform_version_file:
description: 'The version of Terraform CLI to install, specify the path of your terraform version file.'
required: false required: false
terraform_wrapper: terraform_wrapper:
description: 'Whether or not to install a wrapper to wrap subsequent calls of the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.' description: 'Whether or not to install a wrapper to wrap subsequent calls of the `terraform` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.'

39
dist/index.js vendored
View file

@ -120,11 +120,48 @@ credentials "${credentialsHostname}" {
core.debug(`Adding credentials to ${credsFile}`); core.debug(`Adding credentials to ${credsFile}`);
await fs.writeFile(credsFile, creds); await fs.writeFile(credsFile, creds);
} }
async function isVersionFileExist(versionFilePath) {
try {
await fs.access(versionFilePath);
return true;
} catch (err) {
return false;
}
}
async function resolveVersionInput() {
let version = core.getInput("terraform_version");
const versionFileInput = core.getInput("terraform_version_file");
if (version && versionFileInput) {
core.warning(
"Both terraform_version and terraform_version_file inputs are specified, only terraform_version will be used"
);
}
if (version) {
return version;
}
if (versionFileInput) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE,
versionFileInput
);
const fileExists = await isVersionFileExist(versionFilePath);
if (!fileExists) {
throw new Error(
`The specified terraform version file at: ${versionFilePath} does not exist`
);
}
version = await fs.readFile(versionFilePath, "utf-8");
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version || "latest";
}
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 resolveVersionInput();
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

@ -114,11 +114,48 @@ credentials "${credentialsHostname}" {
core.debug(`Adding credentials to ${credsFile}`); core.debug(`Adding credentials to ${credsFile}`);
await fs.writeFile(credsFile, creds); await fs.writeFile(credsFile, creds);
} }
async function isVersionFileExist(versionFilePath) {
try {
await fs.access(versionFilePath);
return true;
} catch (err) {
return false;
}
}
async function resolveVersionInput() {
let version = core.getInput("terraform_version");
const versionFileInput = core.getInput("terraform_version_file");
if (version && versionFileInput) {
core.warning(
"Both terraform_version and terraform_version_file inputs are specified, only terraform_version will be used"
);
}
if (version) {
return version;
}
if (versionFileInput) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE,
versionFileInput
);
const fileExists = await isVersionFileExist(versionFilePath);
if (!fileExists) {
throw new Error(
`The specified terraform version file at: ${versionFilePath} does not exist`
);
}
version = await fs.readFile(versionFilePath, "utf-8");
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version || "latest";
}
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 resolveVersionInput();
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';