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

Use $RUNNER_TEMP when TF_CLI_CONFIG_FILE not in use #270

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
36 changes: 8 additions & 28 deletions .github/workflows/setup-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,10 @@ jobs:
with:
cli_config_credentials_token: ${{ env.TF_CLOUD_API_TOKEN }}

- name: Validate Terraform Credentials (Windows)
if: runner.os == 'Windows'
- name: Validate Terraform Credentials
run: |
cat ${APPDATA}/terraform.rc | grep 'credentials "app.terraform.io"'
cat ${APPDATA}/terraform.rc | grep 'token = "${{ env.TF_CLOUD_API_TOKEN }}"'

- name: Validate Teraform Credentials (Linux & macOS)
if: runner.os != 'Windows'
run: |
cat ${HOME}/.terraformrc | grep 'credentials "app.terraform.io"'
cat ${HOME}/.terraformrc | grep 'token = "${{ env.TF_CLOUD_API_TOKEN }}"'
cat ${RUNNER_TEMP}/setup-terraform.tfrc | grep 'credentials "app.terraform.io"'
cat ${RUNNER_TEMP}/setup-terraform.tfrc | grep 'token = "${{ env.TF_CLOUD_API_TOKEN }

terraform-credentials-enterprise:
name: 'Terraform Enterprise Credentials'
Expand All @@ -146,17 +139,10 @@ jobs:
cli_config_credentials_hostname: 'terraform.example.com'
cli_config_credentials_token: ${{ env.TF_CLOUD_API_TOKEN }}

- name: Validate Terraform Credentials (Windows)
if: runner.os == 'Windows'
run: |
cat ${APPDATA}/terraform.rc | grep 'credentials "terraform.example.com"'
cat ${APPDATA}/terraform.rc | grep 'token = "${{ env.TF_CLOUD_API_TOKEN }}"'

- name: Validate Teraform Credentials (Linux & macOS)
if: runner.os != 'Windows'
- name: Validate Terraform Credentials
run: |
cat ${HOME}/.terraformrc | grep 'credentials "terraform.example.com"'
cat ${HOME}/.terraformrc | grep 'token = "${{ env.TF_CLOUD_API_TOKEN }}"'
cat ${RUNNER_TEMP}/setup-terraform.tfrc | grep 'credentials "terraform.example.com"'
cat ${RUNNER_TEMP}/setup-terraform.tfrc | grep 'token = "${{ env.TF_CLOUD_API_TOKEN }}"'

terraform-credentials-none:
name: 'Terraform No Credentials'
Expand All @@ -171,15 +157,9 @@ jobs:
- name: Setup Terraform
uses: ./

- name: Validate Terraform Credentials (Windows)
if: runner.os == 'Windows'
run: |
[[ -f ${APPDATA}/terraform.rc ]] || exit 0

- name: Validate Teraform Credentials (Linux & macOS)
if: runner.os != 'Windows'
- name: Validate Teraform Credentials
run: |
[[ -f ${HOME}/.terraformrc ]] || exit 0
[[ -f ${RUNNER_TEMP}/setup-terraform.tfrc ]] || exit 0

terraform-arguments:
name: 'Terraform Arguments'
Expand Down
16 changes: 8 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ credentials "${credentialsHostname}" {
}`.trim();
// eslint-enable

// default to OS-specific path
let credsFile = osPlat === 'win32'
? `${process.env.APPDATA}/terraform.rc`
: `${process.env.HOME}/.terraformrc`;

// override with TF_CLI_CONFIG_FILE environment variable
credsFile = process.env.TF_CLI_CONFIG_FILE ? process.env.TF_CLI_CONFIG_FILE : credsFile;
// set or use the TF_CLI_CONFIG_FILE environment variable
let credsFile = process.env.TF_CLI_CONFIG_FILE;
if (!credsFile) {
credsFile = path.join(process.env.RUNNER_TEMP, 'setup-terraform.tfrc');
core.debug(`Default CLI config created as ${credsFile}`);
core.exportVariable('TF_CLI_CONFIG_FILE', credsFile);
}

// get containing folder
// create containing folder in case it doesn't exist
const credsFolder = path.dirname(credsFile);

core.debug(`Creating ${credsFolder}`);
Expand Down
16 changes: 8 additions & 8 deletions lib/setup-terraform.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ credentials "${credentialsHostname}" {
}`.trim();
// eslint-enable

// default to OS-specific path
let credsFile = osPlat === 'win32'
? `${process.env.APPDATA}/terraform.rc`
: `${process.env.HOME}/.terraformrc`;

// override with TF_CLI_CONFIG_FILE environment variable
credsFile = process.env.TF_CLI_CONFIG_FILE ? process.env.TF_CLI_CONFIG_FILE : credsFile;
// set or use the TF_CLI_CONFIG_FILE environment variable
let credsFile = process.env.TF_CLI_CONFIG_FILE;
if (!credsFile) {
credsFile = path.join(process.env.RUNNER_TEMP, 'setup-terraform.tfrc');
core.debug(`Default CLI config created as ${credsFile}`);
core.exportVariable('TF_CLI_CONFIG_FILE', credsFile);
}

// get containing folder
// create containing folder in case it doesn't exist
const credsFolder = path.dirname(credsFile);

core.debug(`Creating ${credsFolder}`);
Expand Down
46 changes: 20 additions & 26 deletions test/setup-terraform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ const setup = require('../lib/setup-terraform');
// .fn(console.error);

describe('Setup Terraform', () => {
const HOME = process.env.HOME;
const APPDATA = process.env.APPDATA;

beforeEach(() => {
process.env.HOME = '/tmp/asdf';
process.env.APPDATA = '/tmp/asdf';
process.env.RUNNER_TEMP = '/tmp/asdf';
});

afterEach(async () => {
await io.rmRF(process.env.HOME);
process.env.HOME = HOME;
process.env.APPDATA = APPDATA;
await io.rmRF(process.env.RUNNER_TEMP);
});

test('gets specific version and adds token and hostname on linux, amd64', async () => {
Expand Down Expand Up @@ -73,8 +67,8 @@ describe('Setup Terraform', () => {

// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -118,8 +112,8 @@ describe('Setup Terraform', () => {
// 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}/terraform.rc`, { encoding: 'utf8' });
// expect credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -161,8 +155,8 @@ describe('Setup Terraform', () => {
// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -204,8 +198,8 @@ describe('Setup Terraform', () => {
// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -246,8 +240,8 @@ describe('Setup Terraform', () => {

// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -288,8 +282,8 @@ describe('Setup Terraform', () => {

// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -330,8 +324,8 @@ describe('Setup Terraform', () => {

// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -372,8 +366,8 @@ describe('Setup Terraform', () => {

// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down Expand Up @@ -414,8 +408,8 @@ describe('Setup Terraform', () => {

// 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 credentials are in ${RUNNER_TEMP}/setup-terraform.tfrc
const creds = await fs.readFile(`${process.env.RUNNER_TEMP}/setup-terraform.tfrc`, { encoding: 'utf8' });
expect(creds.indexOf(credentialsHostname)).toBeGreaterThan(-1);
expect(creds.indexOf(credentialsToken)).toBeGreaterThan(-1);
});
Expand Down