Skip to content

Commit

Permalink
Dependencies and improvements (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evy Bongers authored Nov 11, 2021
1 parent b287e0b commit ce938af
Show file tree
Hide file tree
Showing 10 changed files with 1,886 additions and 30 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"node": true,
"commonjs": true,
"es2021": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"max-len": "off",
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
52 changes: 52 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
name: Continuous Integration

on:
pull_request:

jobs:
code-style:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set Node.js version
uses: actions/setup-node@v2
with:
node-version: 16

- name: Install dependencies
run: npm clean-install

- name: Run code style test
run: npm run lint

check-dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set Node.js version
uses: actions/setup-node@v2
with:
node-version: 16

- name: Install dependencies
run: npm clean-install

- name: Rebuild the dist/ directory
run: npm run build

- name: Compare the expected and actual dist/ directories
run: |
git diff --quiet --ignore-space-at-eol dist/ && exit 0
echo "Detected uncommitted changes after build. See status below:"
git diff --exit-code dist/
id: diff

# If index.js was different than expected, upload the expected version as an artifact
#- uses: actions/upload-artifact@v2
# if: ${{ failure() && steps.diff.conclusion == 'failure' }}
# with:
# name: dist
# path: dist/
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM node:16
COPY dist dist
ENTRYPOINT [ "node", "/dist/index.js" ]
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Terraform

[GitHub Action](https://github.com/features/actions) to automate Terraform workflows.

## Usage

```yaml
- uses: Pararius/action-terraform@0.0.9
name: Terraform
with:
# Google credentials, required for accessing state bucket
google_credentials: ''

# Location of terraform manifests, relative to the current working directory
# Default: ./
terraform_directory: ''

# Whether to run `terraform apply` as last step
# Default: 'false'
terraform_do_apply: ''

# Whether to (try to) lock the state during plan/apply. Reccommended when
# terraform_do_apply is set to 'true'
# Default: 'false'
terraform_lock: ''

# Limit the number of concurrent operations during plan/apply
# Default: '10'
terraform_parallelism: ''
```
## Example
### Only run terraform plan, without locking the workspace
```yaml
- uses: Pararius/action-terraform@0.0.9
name: Terraform
with:
google_credentials: ${{ secrets.YOUR_SECRET }}
terraform_directory: ./terraform/
terraform_do_apply: false
```
### Also run terraform apply, locking the workspace
```yaml
- uses: Pararius/action-terraform@0.0.9
name: Terraform
with:
google_credentials: ${{ secrets.YOUR_SECRET }}
terraform_directory: ./terraform/
terraform_do_apply: true
terraform_lock: true
```
6 changes: 4 additions & 2 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ inputs:
description: Limit the number of concurrent operations during plan/apply
default: '10'
runs:
using: node12
main: dist/index.js
# node16 not available
# https://github.com/actions/runner/issues/772
using: docker
image: Dockerfile
31 changes: 17 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5203,32 +5203,36 @@ const core = __nccwpck_require__(186);
const tc = __nccwpck_require__(784);
const exec = __nccwpck_require__(514);
const fs = __nccwpck_require__(747);
const path = __nccwpck_require__(622);

const status_skipped = '﹣';
const status_success = '✓'
const status_success = '✓';
const status_failed = '✕';

const tfswitchPath = `${process.env['HOME']}/tfswitch`;
const terraformPath = `${process.env['HOME']}/terraform`;

async function shell(command, args, options = {}) {
options.env = {
...process.env,
...options.env,
GOOGLE_APPLICATION_CREDENTIALS: `${process.env['HOME']}/gcloud.json`,
}
};
options.listeners = {
...options.listeners,
debug: (data) => { core.debug(data.toString()) }
}
debug: (data) => { core.debug(data.toString()); },
};

const result = await exec.getExecOutput(command, args, options);
return {
status: result.exitCode,
stderr: result.stderr,
stdout: result.stdout,
}
};
}

async function terraform(args) {
return await shell(`${process.env['HOME']}/terraform`, args, {
return await shell(terraformPath, args, {
cwd: core.getInput('terraform_directory'),
});
}
Expand Down Expand Up @@ -5258,17 +5262,16 @@ async function terraform(args) {
core.endGroup();

core.startGroup('Configure Google Cloud credentials');
fs.writeFileSync(`${process.env['HOME']}/gcloud.json`, core.getInput('google_credentials'))
fs.writeFileSync(`${process.env['HOME']}/gcloud.json`, core.getInput('google_credentials'));
core.endGroup();

core.startGroup('Setup Terraform CLI');
core.info(`Working directory: ${terraformDirectory}`);
core.info('Installing tfswitch:');
const tfsPath = await tc.downloadTool('https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh');
await shell('chmod', ['+x', tfsPath]);
await shell(tfsPath, ['-b', `${process.env['HOME']}/`]);
await shell('sh', [tfsPath, '-b', path.dirname(tfswitchPath)]);
core.info('Running tfswitch:');
const tfs = await shell(`${process.env['HOME']}/tfswitch`, ['-b', `${process.env['HOME']}/terraform`], {
const tfs = await shell(tfswitchPath, ['-b', terraformPath], {
cwd: terraformDirectory,
});
core.endGroup();
Expand Down Expand Up @@ -5340,10 +5343,10 @@ async function terraform(args) {
}
core.info('');
core.info(`Version: ${tf_version}`);
core.info(`Initialization: ${tf_init}`)
core.info(`Formatting: ${tf_fmt}`)
core.info(`Plan: ${tf_plan}`)
core.info(`Apply: ${tf_apply}`)
core.info(`Initialization: ${tf_init}`);
core.info(`Formatting: ${tf_fmt}`);
core.info(`Plan: ${tf_plan}`);
core.info(`Apply: ${tf_apply}`);
})().catch(error => {
core.setFailed(error.message);
});
Expand Down
31 changes: 17 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const exec = require('@actions/exec');
const fs = require('fs');
const path = require('path');

const status_skipped = '﹣';
const status_success = '✓'
const status_success = '✓';
const status_failed = '✕';

const tfswitchPath = `${process.env['HOME']}/tfswitch`;
const terraformPath = `${process.env['HOME']}/terraform`;

async function shell(command, args, options = {}) {
options.env = {
...process.env,
...options.env,
GOOGLE_APPLICATION_CREDENTIALS: `${process.env['HOME']}/gcloud.json`,
}
};
options.listeners = {
...options.listeners,
debug: (data) => { core.debug(data.toString()) }
}
debug: (data) => { core.debug(data.toString()); },
};

const result = await exec.getExecOutput(command, args, options);
return {
status: result.exitCode,
stderr: result.stderr,
stdout: result.stdout,
}
};
}

async function terraform(args) {
return await shell(`${process.env['HOME']}/terraform`, args, {
return await shell(terraformPath, args, {
cwd: core.getInput('terraform_directory'),
});
}
Expand Down Expand Up @@ -57,17 +61,16 @@ async function terraform(args) {
core.endGroup();

core.startGroup('Configure Google Cloud credentials');
fs.writeFileSync(`${process.env['HOME']}/gcloud.json`, core.getInput('google_credentials'))
fs.writeFileSync(`${process.env['HOME']}/gcloud.json`, core.getInput('google_credentials'));
core.endGroup();

core.startGroup('Setup Terraform CLI');
core.info(`Working directory: ${terraformDirectory}`);
core.info('Installing tfswitch:');
const tfsPath = await tc.downloadTool('https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh');
await shell('chmod', ['+x', tfsPath]);
await shell(tfsPath, ['-b', `${process.env['HOME']}/`]);
await shell('sh', [tfsPath, '-b', path.dirname(tfswitchPath)]);
core.info('Running tfswitch:');
const tfs = await shell(`${process.env['HOME']}/tfswitch`, ['-b', `${process.env['HOME']}/terraform`], {
const tfs = await shell(tfswitchPath, ['-b', terraformPath], {
cwd: terraformDirectory,
});
core.endGroup();
Expand Down Expand Up @@ -139,10 +142,10 @@ async function terraform(args) {
}
core.info('');
core.info(`Version: ${tf_version}`);
core.info(`Initialization: ${tf_init}`)
core.info(`Formatting: ${tf_fmt}`)
core.info(`Plan: ${tf_plan}`)
core.info(`Apply: ${tf_apply}`)
core.info(`Initialization: ${tf_init}`);
core.info(`Formatting: ${tf_fmt}`);
core.info(`Plan: ${tf_plan}`);
core.info(`Apply: ${tf_apply}`);
})().catch(error => {
core.setFailed(error.message);
});
Loading

0 comments on commit ce938af

Please sign in to comment.