Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 8 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
name: terraform-github-actions

inputs:
version:
version:
required: true
working_directory:
required: false
parallelism:
required: false
backend_config:
required: false
required: false
var_file:
required: false
required: false
plan:
required: false
required: false
apply:
required: false
required: false
destroy:
required: false

runs:
using: 'node12'
main: 'dist/index.js'

branding:
color: purple
icon: send
icon: send
28 changes: 21 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ function setVersion() {
}
function setWorkDir() {
return __awaiter(this, void 0, void 0, function* () {
var workDir = core.getInput('working_directory');
let workDir = core.getInput('working_directory');
if (shell.ls(workDir).code !== 0) {
throw new Error(`working directory ` + workDir + ` doesn't exist`);
}
Expand All @@ -1661,11 +1661,11 @@ function setWorkDir() {
}
function execTerraform() {
return __awaiter(this, void 0, void 0, function* () {
//format
// TF format
if (shell.exec('terraform fmt -check').code !== 0) {
throw new Error(`unable to format terraform`);
}
//init
// TF init
const backendConfig = core.getInput('backend_config');
if (backendConfig) {
if (shell.exec('terraform init -backend-config=' + backendConfig).code !== 0) {
Expand All @@ -1677,17 +1677,31 @@ function execTerraform() {
throw new Error(`unable to initilize terraform`);
}
}
//validate
// TF validation
if (shell.exec('terraform validate').code !== 0) {
throw new Error(`unable to validate terraform`);
}
//plan
const plan = core.getInput('plan');
const apply = core.getInput('apply');
const destroy = core.getInput('destroy');
const varFile = core.getInput('var_file');
// TF destroy
if (destroy) {
if (varFile) {
if (shell.exec(`terraform plan -var-file='${varFile}' -out=tfplan.out`).code !== 0) {
throw new Error(`unable to plan terraform`);
}
}
else {
if (shell.exec('terraform plan -out=tfplan.out').code !== 0) {
throw new Error(`unable to plan terraform`);
}
}
}
// TF plan
if (plan || apply) {
if (varFile) {
if (shell.exec('terraform plan -var-file=' + varFile + ' -out=tfplan.out').code !== 0) {
if (shell.exec(`terraform plan -var-file='${varFile}' -out=tfplan.out`).code !== 0) {
throw new Error(`unable to plan terraform`);
}
}
Expand All @@ -1697,7 +1711,7 @@ function execTerraform() {
}
}
}
//apply
// TF apply
if (apply) {
if (shell.exec('terraform apply tfplan.out').code !== 0) {
throw new Error(`unable to apply terraform`);
Expand Down
11 changes: 0 additions & 11 deletions index.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/main.ts -o dist",
"lint": "eslint src/"
"lint": "eslint ./src"
},
"devDependencies": {
"@actions/core": "^1.2.4",
Expand Down
47 changes: 32 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as core from '@actions/core'
import * as core from '@actions/core';
import * as shell from 'shelljs';

async function setVersion() {
const version = core.getInput('version')
if (version == "") {
throw new Error(`version input parmater was not set`)
}
}
if (shell.exec('tfenv install ' + version).code !== 0) {
throw new Error(`unable to install terraform ` + version)
}
Expand All @@ -14,15 +14,16 @@ async function setVersion() {
}
}


async function setWorkDir() {
var workDir = core.getInput('working_directory')
let workDir = core.getInput('working_directory')

if (shell.ls(workDir).code !== 0) {
throw new Error(`working directory ` + workDir + ` doesn't exist`)
}

//parallelism
const parallelism = core.getInput('parallelism')

if (parallelism !== "") {
if (shell.mkdir('-p', '_tf/' + parallelism).code !== 0) {
throw new Error(``)
Expand All @@ -37,37 +38,53 @@ async function setWorkDir() {
}

async function execTerraform() {
//format
// TF format
if (shell.exec('terraform fmt -check').code !== 0) {
throw new Error(`unable to format terraform`)
}

//init
// TF init
const backendConfig = core.getInput('backend_config')

if (backendConfig) {
if (shell.exec('terraform init -backend-config=' + backendConfig).code !== 0) {
throw new Error(`unable to initilize terraform`)
}

}
else {
} else {
if (shell.exec('terraform init').code !== 0) {
throw new Error(`unable to initilize terraform`)
}
}

//validate
// TF validation
if (shell.exec('terraform validate').code !== 0) {
throw new Error(`unable to validate terraform`)
}

//plan
const plan = core.getInput('plan')
const apply = core.getInput('apply')
const destroy = core.getInput('destroy')

const varFile = core.getInput('var_file')

// TF destroy
if (destroy) {
if (varFile) {
if (shell.exec(`terraform plan -var-file='${varFile}' -out=tfplan.out`).code !== 0) {

Choose a reason for hiding this comment

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

should be destroy

throw new Error(`unable to plan terraform`)
}
}
else {
if (shell.exec('terraform plan -out=tfplan.out').code !== 0) {
throw new Error(`unable to plan terraform`)
}
}
}

// TF plan
if (plan || apply) {
if (varFile) {
if (shell.exec('terraform plan -var-file=' + varFile + ' -out=tfplan.out').code !== 0) {
if (shell.exec(`terraform plan -var-file='${varFile}' -out=tfplan.out`).code !== 0) {
throw new Error(`unable to plan terraform`)
}
}
Expand All @@ -78,7 +95,7 @@ async function execTerraform() {
}
}

//apply
// TF apply
if (apply) {
if (shell.exec('terraform apply tfplan.out').code !== 0) {
throw new Error(`unable to apply terraform`)
Expand All @@ -88,7 +105,7 @@ async function execTerraform() {

async function run() {
try {
await setVersion()
await setVersion()
await setWorkDir()
await execTerraform()
}
Expand All @@ -97,4 +114,4 @@ async function run() {
}
}

run()
run()
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": ["node_modules", "**/*.test.ts"]
}
}