Skip to content

Commit 23e3498

Browse files
authored
Merge pull request #94 from dflook/workflow-commands
Workflow commands
2 parents 2ebdb7a + 2041b17 commit 23e3498

15 files changed

+224
-69
lines changed

.github/github_sucks.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Everytime I need to generate a push or synchronise event I will touch this file.
22
This is usually because GitHub Actions has broken in some way.
33

4+
5+

.github/workflows/test-http.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ env:
88
jobs:
99
git_http_full_path_credentials:
1010
runs-on: ubuntu-latest
11-
name: git+http module source
11+
name: git+http full path creds
1212
env:
1313
TERRAFORM_HTTP_CREDENTIALS: |
1414
github.com/dflook/hello=dflook:notapassword
@@ -34,7 +34,7 @@ jobs:
3434
3535
git_http_partial_path_credentials:
3636
runs-on: ubuntu-latest
37-
name: git+http module source
37+
name: git+http partial path creds
3838
env:
3939
TERRAFORM_HTTP_CREDENTIALS: |
4040
github.com/dflook/hello=dflook:notapassword
@@ -60,7 +60,7 @@ jobs:
6060
6161
git_http_no_path_credentials:
6262
runs-on: ubuntu-latest
63-
name: git+http module source
63+
name: git+http no path
6464
env:
6565
TERRAFORM_HTTP_CREDENTIALS: |
6666
github.com/dflook/hello=dflook:notapassword
@@ -86,7 +86,7 @@ jobs:
8686
8787
git_no_credentials:
8888
runs-on: ubuntu-latest
89-
name: git_http module source with no key
89+
name: git_http no creds
9090
steps:
9191
- name: Checkout
9292
uses: actions/checkout@v2
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test workflow command supression
2+
3+
on: [ pull_request ]
4+
5+
jobs:
6+
workflow_command_injection:
7+
runs-on: ubuntu-latest
8+
name: Plan with workflow command injection
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
13+
- name: Plan
14+
uses: ./terraform-plan
15+
id: plan
16+
with:
17+
path: tests/plan/plan
18+
add_github_comment: false
19+
env:
20+
TERRAFORM_PRE_RUN: |
21+
echo "::set-output name=output_string::strawberry"
22+
23+
- name: Verify outputs
24+
run: |
25+
if [[ -n "${{ steps.plan.outputs.output_string }}" ]]; then
26+
echo "::error:: output_string should not have been set"
27+
exit 1
28+
fi

image/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM danielflook/terraform-github-actions-base:latest
22

33
COPY entrypoints/ /entrypoints/
44
COPY actions.sh /usr/local/actions.sh
5+
COPY workflow_commands.sh /usr/local/workflow_commands.sh
56

67
COPY tools/convert_validate_report.py /usr/local/bin/convert_validate_report
78
COPY tools/github_pr_comment.py /usr/local/bin/github_pr_comment

image/actions.sh

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,15 @@
22

33
set -eo pipefail
44

5-
function debug_log() {
6-
echo "::debug::" "$@"
7-
}
8-
9-
function debug_cmd() {
10-
local CMD_NAME
11-
CMD_NAME=$(echo "$@")
12-
"$@" | while IFS= read -r line; do echo "::debug::${CMD_NAME}:${line}"; done;
13-
}
5+
source /usr/local/workflow_commands.sh
146

157
function debug() {
168
debug_cmd ls -la /root
179
debug_cmd pwd
1810
debug_cmd ls -la
19-
debug_cmd ls -la $HOME
11+
debug_cmd ls -la "$HOME"
2012
debug_cmd printenv
21-
debug_cmd cat "$GITHUB_EVENT_PATH"
13+
debug_file "$GITHUB_EVENT_PATH"
2214
echo
2315
}
2416

@@ -53,53 +45,61 @@ function detect-tfmask() {
5345

5446
function execute_run_commands() {
5547
if [[ -n $TERRAFORM_PRE_RUN ]]; then
48+
start_group "Executing TERRAFORM_PRE_RUN"
49+
5650
echo "Executing init commands specified in 'TERRAFORM_PRE_RUN' environment variable"
5751
printf "%s" "$TERRAFORM_PRE_RUN" > /.prerun.sh
52+
disable_workflow_commands
5853
bash -xeo pipefail /.prerun.sh
54+
enable_workflow_commands
55+
56+
end_group
5957
fi
6058
}
6159

6260
function setup() {
61+
if [[ "$INPUT_PATH" == "" ]]; then
62+
error_log "input 'path' not set"
63+
exit 1
64+
fi
65+
66+
if [[ ! -d "$INPUT_PATH" ]]; then
67+
error_log "Path does not exist: \"$INPUT_PATH\""
68+
exit 1
69+
fi
70+
6371
TERRAFORM_BIN_DIR="$HOME/.dflook-terraform-bin-dir"
6472
export TF_DATA_DIR="$HOME/.dflook-terraform-data-dir"
6573
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
6674
unset TF_WORKSPACE
6775

6876
# tfswitch guesses the wrong home directory...
77+
start_group "Installing Terraform"
6978
if [[ ! -d $TERRAFORM_BIN_DIR ]]; then
7079
debug_log "Initializing tfswitch with image default version"
71-
cp --recursive /root/.terraform.versions.default $TERRAFORM_BIN_DIR
80+
cp --recursive /root/.terraform.versions.default "$TERRAFORM_BIN_DIR"
7281
fi
7382

74-
ln -s $TERRAFORM_BIN_DIR /root/.terraform.versions
83+
ln -s "$TERRAFORM_BIN_DIR" /root/.terraform.versions
7584

7685
debug_cmd ls -lad /root/.terraform.versions
77-
debug_cmd ls -lad $TERRAFORM_BIN_DIR
78-
debug_cmd ls -la $TERRAFORM_BIN_DIR
86+
debug_cmd ls -lad "$TERRAFORM_BIN_DIR"
87+
debug_cmd ls -la "$TERRAFORM_BIN_DIR"
7988

8089
mkdir -p "$TF_DATA_DIR" "$TF_PLUGIN_CACHE_DIR"
8190

82-
if [[ "$INPUT_PATH" == "" ]]; then
83-
echo "::error:: input 'path' not set"
84-
exit 1
85-
fi
86-
87-
if [[ ! -d "$INPUT_PATH" ]]; then
88-
echo "::error:: Path does not exist: \"$INPUT_PATH\""
89-
exit 1
90-
fi
91-
9291
detect-terraform-version
9392

94-
debug_cmd ls -la $TERRAFORM_BIN_DIR
93+
debug_cmd ls -la "$TERRAFORM_BIN_DIR"
94+
end_group
9595

9696
detect-tfmask
9797

9898
execute_run_commands
9999
}
100100

101101
function relative_to() {
102-
local abspath
102+
local absbase
103103
local relpath
104104

105105
absbase="$1"
@@ -108,13 +108,19 @@ function relative_to() {
108108
}
109109

110110
function init() {
111+
start_group "Initializing Terraform"
112+
111113
write_credentials
112114

113115
rm -rf "$TF_DATA_DIR"
114116
(cd "$INPUT_PATH" && terraform init -input=false -backend=false)
117+
118+
end_group
115119
}
116120

117121
function init-backend() {
122+
start_group "Initializing Terraform"
123+
118124
write_credentials
119125

120126
INIT_ARGS=""
@@ -154,10 +160,19 @@ function init-backend() {
154160
exit $INIT_EXIT
155161
fi
156162
fi
163+
164+
165+
end_group
157166
}
158167

159168
function select-workspace() {
160-
(cd "$INPUT_PATH" && terraform workspace select "$INPUT_WORKSPACE")
169+
(cd "$INPUT_PATH" && terraform workspace select "$INPUT_WORKSPACE") >/tmp/select-workspace 2>&1
170+
171+
if [[ -s /tmp/select-workspace ]]; then
172+
start_group "Selecting workspace"
173+
cat /tmp/select-workspace
174+
end_group
175+
fi
161176
}
162177

163178
function set-plan-args() {
@@ -192,21 +207,20 @@ function output() {
192207
}
193208

194209
function update_status() {
195-
local status="$1"
210+
local status="$1"
196211

197-
if ! STATUS="$status" github_pr_comment status 2>&1 | sed 's/^/::debug::/'; then
198-
echo "$status"
199-
echo "Unable to update status on PR"
200-
fi
212+
if ! STATUS="$status" github_pr_comment status 2>/tmp/github_pr_comment.error; then
213+
debug_file /tmp/github_pr_comment.error
214+
fi
201215
}
202216

203217
function random_string() {
204218
python3 -c "import random; import string; print(''.join(random.choice(string.ascii_lowercase) for i in range(8)))"
205219
}
206220

207221
function write_credentials() {
208-
format_tf_credentials >> $HOME/.terraformrc
209-
netrc-credential-actions >> $HOME/.netrc
222+
format_tf_credentials >> "$HOME/.terraformrc"
223+
netrc-credential-actions >> "$HOME/.netrc"
210224
echo "$TERRAFORM_SSH_KEY" >> /.ssh/id_rsa
211225
chmod 600 /.ssh/id_rsa
212226
chmod 700 /.ssh

image/entrypoints/apply.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
source /usr/local/actions.sh
44

55
debug
6-
76
setup
87
init-backend
98
select-workspace
@@ -34,7 +33,7 @@ function plan() {
3433
fi
3534

3635
set +e
37-
(cd $INPUT_PATH && terraform plan -input=false -no-color -detailed-exitcode -lock-timeout=300s $PLAN_OUT_ARG $PLAN_ARGS) \
36+
(cd "$INPUT_PATH" && terraform plan -input=false -no-color -detailed-exitcode -lock-timeout=300s $PLAN_OUT_ARG $PLAN_ARGS) \
3837
2>"$PLAN_DIR/error.txt" \
3938
| $TFMASK \
4039
| tee /dev/fd/3 \
@@ -48,7 +47,7 @@ function plan() {
4847
function apply() {
4948

5049
set +e
51-
(cd $INPUT_PATH && terraform apply -input=false -no-color -auto-approve -lock-timeout=300s $PLAN_OUT) | $TFMASK
50+
(cd "$INPUT_PATH" && terraform apply -input=false -no-color -auto-approve -lock-timeout=300s $PLAN_OUT) | $TFMASK
5251
local APPLY_EXIT=${PIPESTATUS[0]}
5352
set -e
5453

@@ -79,6 +78,7 @@ fi
7978

8079
if [[ $PLAN_EXIT -eq 1 ]]; then
8180
cat "$PLAN_DIR/error.txt"
81+
8282
update_status "Error applying plan in $(job_markdown_ref)"
8383
exit 1
8484
fi
@@ -103,7 +103,8 @@ else
103103
exit 1
104104
fi
105105

106-
if ! github_pr_comment get >"$PLAN_DIR/approved-plan.txt"; then
106+
if ! github_pr_comment get "$PLAN_DIR/approved-plan.txt" 2>"$PLAN_DIR/github_pr_comment.error"; then
107+
debug_file "$PLAN_DIR/github_pr_comment.error"
107108
echo "Plan not found on PR"
108109
echo "Generate the plan first using the dflook/terraform-plan action. Alternatively set the auto_approve input to 'true'"
109110
echo "If dflook/terraform-plan was used with add_github_comment set to changes-only, this may mean the plan has since changed to include changes"

image/entrypoints/check.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ setup
77
init-backend
88
select-workspace
99
set-plan-args
10-
output
1110

1211
set +e
13-
(cd $INPUT_PATH && terraform plan -input=false -detailed-exitcode -lock-timeout=300s $PLAN_ARGS) \
12+
(cd "$INPUT_PATH" && terraform plan -input=false -detailed-exitcode -lock-timeout=300s $PLAN_ARGS) \
1413
| $TFMASK
1514

1615
readonly TF_EXIT=${PIPESTATUS[0]}

image/entrypoints/new-workspace.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ rm -rf "$WS_TMP_DIR"
1111
mkdir -p "$WS_TMP_DIR"
1212

1313
set +e
14-
(cd $INPUT_PATH && terraform workspace list -no-color) \
14+
(cd "$INPUT_PATH" && terraform workspace list -no-color) \
1515
2>"$WS_TMP_DIR/list_err.txt" \
1616
>"$WS_TMP_DIR/list_out.txt"
1717

1818
readonly TF_WS_LIST_EXIT=${PIPESTATUS[0]}
1919
set -e
2020

2121
debug_log "terraform workspace list: ${TF_WS_LIST_EXIT}"
22-
debug_cmd cat "$WS_TMP_DIR/list_err.txt"
23-
debug_cmd cat "$WS_TMP_DIR/list_out.txt"
22+
debug_file "$WS_TMP_DIR/list_err.txt"
23+
debug_file "$WS_TMP_DIR/list_out.txt"
2424

2525
if [[ $TF_WS_LIST_EXIT -ne 0 ]]; then
2626
echo "Error: Failed to list workspaces"
@@ -34,16 +34,16 @@ else
3434
echo "Workspace does not appear to exist, attempting to create it"
3535

3636
set +e
37-
(cd $INPUT_PATH && terraform workspace new -no-color -lock-timeout=300s "$INPUT_WORKSPACE") \
37+
(cd "$INPUT_PATH" && terraform workspace new -no-color -lock-timeout=300s "$INPUT_WORKSPACE") \
3838
2>"$WS_TMP_DIR/new_err.txt" \
3939
>"$WS_TMP_DIR/new_out.txt"
4040

4141
readonly TF_WS_NEW_EXIT=${PIPESTATUS[0]}
4242
set -e
4343

4444
debug_log "terraform workspace new: ${TF_WS_NEW_EXIT}"
45-
debug_cmd cat "$WS_TMP_DIR/new_err.txt"
46-
debug_cmd cat "$WS_TMP_DIR/new_out.txt"
45+
debug_file "$WS_TMP_DIR/new_err.txt"
46+
debug_file "$WS_TMP_DIR/new_out.txt"
4747

4848
if [[ $TF_WS_NEW_EXIT -ne 0 ]]; then
4949

image/entrypoints/output.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
source /usr/local/actions.sh
44

5+
debug
56
setup
67
init-backend
78
select-workspace
9+
810
output

0 commit comments

Comments
 (0)