Skip to content

Commit

Permalink
feat!: tg version file and input/output underscores to dashes (#345)
Browse files Browse the repository at this point in the history
* refactor!: change input/output vars underscores to dashes

* test: allow manual test execution
  • Loading branch information
autero1 authored Feb 18, 2024
1 parent 8c6b0f2 commit ea717b1
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 28 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: Test Action

on:
workflow_dispatch:
push:
branches:
- "*"
paths-ignore:
- "*.md"
# A pull request sent from a fork of the upstream repository could be manipulated to do harmful things.
# The upstream repository’s maintainer would have no protection against this attack, as pull requests
# can be sent by anyone who forks the repository on GitHub.
Expand Down Expand Up @@ -53,16 +52,34 @@ jobs:
- name: Install specific Terragrunt version
uses: ./
with:
terragrunt_version: 0.54.0
terragrunt-version: 0.54.0

- name: Validate specific version
run: terragrunt --version

- name: Install latest Terragrunt version
uses: ./
with:
terragrunt_version: latest
terragrunt-version: latest
token: ${{ secrets.GITHUB_TOKEN }}

- name: Validate latest
run: terragrunt --version

- name: Install Terragrunt with version file latest
uses: ./
with:
terragrunt-version-file: .terragrunt-version
token: ${{ secrets.GITHUB_TOKEN }}

- name: Validate version file latest
run: terragrunt --version

- name: Install Terragrunt with version file specified
uses: ./
with:
terragrunt-version-file: .terragrunt-version-latest
token: ${{ secrets.GITHUB_TOKEN }}

- name: Validate version file latest
run: terragrunt --version
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: "Test"

on:
workflow_dispatch:
push:
branches:
- "*"
paths-ignore:
- "*.md"

# A pull request sent from a fork of the upstream repository could be manipulated to do harmful things.
# The upstream repository’s maintainer would have no protection against this attack, as pull requests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/update-main-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
description: The major version to update
options:
- v2
- v3

jobs:
tag:
Expand Down
1 change: 1 addition & 0 deletions .terragrunt-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.51.0
1 change: 1 addition & 0 deletions .terragrunt-version-latest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
latest
48 changes: 39 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Set up your GitHub Actions workflow with a specific version of [Terragrunt](http

Because of [deprecation in the GitHub Actions environment](https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/), versions lower than v1.0.0 will no longer work properly.

## Special Notice

From version `v3.0.0`, the inputs and outputs are changed to dash-separated version (`terragrunt-version`, `terragrunt-version-file`, `terragrunt-path`).

This convention aligns with the YAML style guide and is more prevalent in the GitHub Actions community and documentation.

## Usage

The next example step will install Terragrunt 0.55.2.
Expand All @@ -27,26 +33,50 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terragrunt
uses: autero1/action-terragrunt@v2
uses: autero1/action-terragrunt@v3
with:
terragrunt-version: 0.55.2
token: ${{ secrets.GITHUB_TOKEN }}
- name: Interact with Terragrunt
run: terragrunt --version
```
If you want to use a version file, e.g. `.terragrunt-version`, you can use the following example:

```yaml
name: Example workflow with version file
on: [push]
jobs:
example:
name: Example Terragrunt interaction
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terragrunt
uses: autero1/action-terragrunt@v3
with:
terragrunt_version: 0.55.2
terragrunt-version-file: .terragrunt-version
token: ${{ secrets.GITHUB_TOKEN }}
- name: Interact with Terragrunt
run: terragrunt --version
```

### Inputs

| Parameter | Description | Required |
| --------- | ----------- | -------- |
| `terragrunt_version` | Terragrunt [version](https://github.com/gruntwork-io/terragrunt/releases) to deploy. Use `latest` for the most recent version. | true |
| `token` | Github API Token to avoid rate limiting while getting latest Terragrunt release | false |
| Parameter | Description | Required |
|---------------------------|--------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------|
| `terragrunt-version` | Terragrunt [version](https://github.com/gruntwork-io/terragrunt/releases) to deploy. Use `latest` for the most recent version. | either version or version file required |
| `terragrunt-version-file` | File containing the Terragrunt version to install. | either version or version file required |
| `token` | Github API Token to avoid rate limiting while getting latest Terragrunt release | false |

### Outputs

| Parameter | Description |
| --------- | ----------- |
| `terragrunt_path` | Cached tool path of Terragrunt |
| Parameter | Description |
|-------------------| ----------- |
| `terragrunt-path` | Cached tool path of Terragrunt |

### Supported platforms

Expand Down
1 change: 1 addition & 0 deletions __tests__/.terragrunt-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.50.0
7 changes: 7 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const theGot = got.default;

jest.mock('os');

afterEach(() => {
delete process.env['INPUT_TERRAGRUNT-VERSION'];
delete process.env['INPUT_TERRAGRUNT-VERSION-FILE'];
delete process.env['GITHUB_WORKSPACE'];
});

async function checkHead(url: string): Promise<number> {
// Due to funky redirects with GitHub, just making sure we get the 302 found and not follow
const response = await theGot.get(url, {followRedirect: false});
Expand Down Expand Up @@ -51,6 +57,7 @@ describe('getDownloadURL()', () => {
});

test('get latest url', async () => {
process.env['INPUT_TERRAGRUNT-VERSION'] = 'latest';
const latestVersion = (await getLatestVersion()) || '';
const osTypeSpy = jest.spyOn(os, 'type');
osTypeSpy.mockReturnValue('Linux');
Expand Down
44 changes: 39 additions & 5 deletions __tests__/get-inputs-and-outputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,54 @@ beforeEach(() => {
});

afterEach(() => {
delete process.env['INPUT_TERRAGRUNT_VERSION'];
delete process.env['INPUT_TERRAGRUNT-VERSION'];
delete process.env['INPUT_TERRAGRUNT-VERSION-FILE'];
delete process.env['GITHUB_WORKSPACE'];
});

describe('getInputs()', () => {
test('get spec inputs', () => {
process.env['INPUT_TERRAGRUNT_VERSION'] = 'v0.21.13';
test('get spec input version only', () => {
process.env['INPUT_TERRAGRUNT-VERSION'] = 'v0.21.13';

const inputs: Inputs = getInputs();

expect(inputs.TerragruntVersion).toMatch('v0.21.13');
});

test('get spec input version file only', () => {
process.env['GITHUB_WORKSPACE'] = './__tests__';
process.env['INPUT_TERRAGRUNT-VERSION-FILE'] = '.terragrunt-version';

const inputs: Inputs = getInputs();

expect(inputs.TerragruntVersion).toMatch('v0.50.0');
});

test('get spec input version latest', () => {
process.env['INPUT_TERRAGRUNT-VERSION'] = 'latest';

const inputs: Inputs = getInputs();

expect(inputs.TerragruntVersion).toMatch('latest');
});

test('get spec input version and version-file', () => {
process.env['INPUT_TERRAGRUNT-VERSION'] = 'v0.22.0';
process.env['INPUT_TERRAGRUNT-VERSION-FILE'] = '.terragrunt-version';

const inputs: Inputs = getInputs();

expect(inputs.TerragruntVersion).toMatch('v0.22.0');
});

test('get spec neither input version or version-file', () => {
const inputs: Inputs = getInputs();
expect(inputs.TerragruntVersion).toMatch('');
});


test('get spec input prefixes with v', () => {
process.env['INPUT_TERRAGRUNT_VERSION'] = '0.21.13';
process.env['INPUT_TERRAGRUNT-VERSION'] = '0.21.13';

const inputs: Inputs = getInputs();

Expand All @@ -29,6 +63,6 @@ describe('getInputs()', () => {
test('get spec outputs', () => {
const outputs: Outputs = getOutputs();

expect(outputs.TerragruntPath).toMatch('terragrunt_path');
expect(outputs.TerragruntPath).toMatch('terragrunt-path');
});
});
10 changes: 5 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ name: 'Terragrunt installer'
description: 'Install a specific version of terragrunt binary.'
author: 'Petri Autero'
inputs:
terragrunt_version:
description: 'Terragrunt version to install'
required: true
terragrunt-version:
description: 'Terragrunt version to install. Examples: 0.52.0, latest'
terragrunt-version-file:
description: 'File containing the Terragrunt version to install. Examples: .terragrunt-version'
token:
description: 'Github token to use for getting latest release'
required: false
outputs:
terragrunt_path:
terragrunt-path:
description: 'Path to the terragrunt binary'
branding:
icon: cloud
Expand Down
1 change: 1 addition & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export async function downloadTerragrunt(version: string): Promise<string> {

export async function run(): Promise<void> {
const terragruntVersion: string = getInputs().TerragruntVersion;

const cachedPath = await downloadTerragrunt(terragruntVersion);

// Add the cached tool to path
Expand Down
41 changes: 38 additions & 3 deletions src/get-inputs-and-outputs.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import * as core from '@actions/core';
import {Inputs, Outputs} from './interfaces';
import * as path from 'path';
import fs from 'fs';

function showInputs(inps: Inputs): void {
core.info(`[INFO] TerragruntVersion: ${inps.TerragruntVersion}`);
}

export function getInputs(): Inputs {
let tgVersion = core.getInput('terragrunt_version');
export function getTerragruntVersionFromFile(versionFilePath: string): string {
if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified Terragrunt version file at: ${versionFilePath} does not exist`
);
}

const contents = fs.readFileSync(versionFilePath, 'utf8');
return contents.trim().replace(/\r?\n|\r/g, '');
}

function prepareInputs(tgVersion: string): Inputs {
if (!tgVersion.startsWith('v') && tgVersion.toLowerCase() !== 'latest') {
tgVersion = `v${tgVersion}`;
}

const token = core.getInput('token') || undefined;

const inps: Inputs = {
Expand All @@ -22,9 +35,31 @@ export function getInputs(): Inputs {
return inps;
}

export function getInputs(): Inputs {
let tgVersion = core.getInput('terragrunt-version');
const tgVersionFile = core.getInput('terragrunt-version-file');

if (tgVersion && tgVersionFile) {
core.warning(
'[WARN] Both terragrunt-version and terragrunt-version-file inputs are specified, only terragrunt-version will be used'
);
return prepareInputs(tgVersion);
}

if (tgVersionFile) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
tgVersionFile
);

tgVersion = getTerragruntVersionFromFile(versionFilePath);
}
return prepareInputs(tgVersion);
}

export function getOutputs(): Outputs {
const outs: Outputs = {
TerragruntPath: 'terragrunt_path'
TerragruntPath: 'terragrunt-path'
};

return outs;
Expand Down

0 comments on commit ea717b1

Please sign in to comment.