Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit 7881bd7

Browse files
jordanfinnerssudomateo
authored andcommitted
Create an Output action (#108)
* Gather the terraform outputs and provide them as a step output to be used in further github actions
1 parent 2608642 commit 7881bd7

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Inputs configure Terraform GitHub Actions to perform different actions.
7979
Outputs are used to pass information to subsequent GitHub Actions steps.
8080

8181
* `tf_actions_plan_has_changes` - Whether or not the Terraform plan contained changes.
82+
* `tf_actions_output` - The Terraform outputs in JSON format.
8283

8384
## Secrets
8485

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ inputs:
2020
outputs:
2121
tf_actions_plan_has_changes:
2222
description: 'Whether or not the Terraform plan contained changes.'
23+
tf_actions_output:
24+
description: 'The Terraform outputs in JSON format.'
2325
runs:
2426
using: 'docker'
2527
image: './Dockerfile'

examples/output.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Terraform Output
2+
3+
If you need the outputs of your Terraform configuration later in your GitHub Actions workflow, you can use the `output` subcommand.
4+
5+
```yaml
6+
name: 'Terraform GitHub Actions'
7+
on:
8+
- push
9+
jobs:
10+
terraform:
11+
name: 'Terraform'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: 'Checkout'
15+
uses: actions/checkout@master
16+
17+
- name: 'Terraform Outputs'
18+
id: terraform
19+
uses: hashicorp/terraform-github-actions@master
20+
with:
21+
tf_actions_version: 0.12.13
22+
tf_actions_subcommand: 'outputs'
23+
tf_actions_working_dir: '.'
24+
25+
- name: 'Use Terraform Output'
26+
run: echo ${{ steps.terraform.outputs.tf_actions_output }}
27+
28+
- name: 'Pull specific database name from outputs'
29+
run: |
30+
# Install jq in this shell
31+
apt-get install jq
32+
33+
# Parse the outputs from the 'Terraform Outputs' step and grab the database name
34+
DBNAME=$(echo ${{ steps.terraform.outputs.tf_actions_output }} | jq -r '.database.value.name')
35+
36+
# Will echo out 'test-database'
37+
echo $DBNAME
38+
```
39+
40+
In this example the `tf_actions_output` would look like:
41+
```json
42+
{
43+
"database": {
44+
"value": {
45+
"name": "test-database"
46+
}
47+
}
48+
}
49+
```

src/main.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ function parseInputs {
3030
echo "Input terraform_subcommand cannot be empty"
3131
exit 1
3232
fi
33-
33+
3434
# Optional inputs
3535
tfWorkingDir="."
3636
if [ "${INPUT_TF_ACTIONS_WORKING_DIR}" != "" ] || [ "${INPUT_TF_ACTIONS_WORKING_DIR}" != "." ]; then
3737
tfWorkingDir=${INPUT_TF_ACTIONS_WORKING_DIR}
3838
fi
39-
39+
4040
tfComment=0
4141
if [ "${INPUT_TF_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_TF_ACTIONS_COMMENT}" == "true" ]; then
4242
tfComment=1
@@ -71,6 +71,7 @@ function main {
7171
source ${scriptDir}/terraform_validate.sh
7272
source ${scriptDir}/terraform_plan.sh
7373
source ${scriptDir}/terraform_apply.sh
74+
source ${scriptDir}/terraform_output.sh
7475

7576
parseInputs
7677
cd ${GITHUB_WORKSPACE}/${tfWorkingDir}
@@ -96,6 +97,10 @@ function main {
9697
installTerraform
9798
terraformApply "${*}"
9899
;;
100+
output)
101+
installTerraform
102+
terraformOutput ${*}
103+
;;
99104
*)
100105
echo "Error: Must provide a valid value for terraform_subcommand"
101106
exit 1

src/terraform_output.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
function terraformOutput {
4+
# Gather the output of `terraform output`.
5+
echo "output: info: gathering all the outputs for the Terraform configuration in ${tfWorkingDir}"
6+
outputOutput=$(terraform output -json ${*} 2>&1)
7+
outputExitCode=${?}
8+
9+
# Exit code of 0 indicates success. Print the output and exit.
10+
if [ ${outputExitCode} -eq 0 ]; then
11+
echo "output: info: successfully gathered all the outputs for the Terraform configuration in ${tfWorkingDir}"
12+
echo "${outputOutput}"
13+
echo
14+
echo ::set-output name=tf_actions_output::${outputOutput}
15+
exit ${outputExitCode}
16+
fi
17+
18+
# Exit code of !0 indicates failure.
19+
echo "output: error: failed to gather all the outputs for the Terraform configuration in ${tfWorkingDir}"
20+
echo "${outputOutput}"
21+
echo
22+
exit ${outputExitCode}
23+
}

0 commit comments

Comments
 (0)