Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat(versionfile): accept versionfile #324

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ inputs:
required: false
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`.'
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
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`.'
Expand Down
39 changes: 38 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,48 @@ credentials "${credentialsHostname}" {
core.debug(`Adding credentials to ${credsFile}`);
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 () {
try {
// Gather GitHub Actions inputs
const version = core.getInput('terraform_version');
const version = await resolveVersionInput();
const credentialsHostname = core.getInput('cli_config_credentials_hostname');
const credentialsToken = core.getInput('cli_config_credentials_token');
const wrapper = core.getInput('terraform_wrapper') === 'true';
Expand Down
39 changes: 38 additions & 1 deletion lib/setup-terraform.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,48 @@ credentials "${credentialsHostname}" {
core.debug(`Adding credentials to ${credsFile}`);
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this won't work if tfutils/tfenv#347 (allow whole line comments) is merged, and assuming of course that the user has the version file due to using tfenv.

I say this mainly because I think it's an important consideration for the (Hashicorp) team in making a product decision about it as mentioned above: terraform itself doesn't have a version file concept, so either:

  1. setup-terraform would support a simple 'just the version' format, and Hashicorp owns that format;
  2. it tries to support all formats seen in the wild, on the basis of user reports that certain things (like comments) break it or something (and, er, hopes they never conflict?);
  3. it declares support for a specific format (e.g. tfenv's) and aims to support that (and inherently then breaking changes in that format necessitate breaking changes in setup-terraform)


core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version || "latest";
}

async function run () {
try {
// Gather GitHub Actions inputs
const version = core.getInput('terraform_version');
const version = await resolveVersionInput();
const credentialsHostname = core.getInput('cli_config_credentials_hostname');
const credentialsToken = core.getInput('cli_config_credentials_token');
const wrapper = core.getInput('terraform_wrapper') === 'true';
Expand Down