Skip to content

Commit 875f1da

Browse files
Show workspace in PR comments (hashicorp#146)
* Show workspace in PR comments * Load Terraform workspace before generating comment We don't want to pollute the comment with error output. This also moves workspace loading into common code and adds error handling. * Handle TF_CLI_ARGS when detecting workspace * Simplify workspace detection The only way it can be set is TF_WORKSPACE. * Simplify default fallback logic for tfWorkspace
1 parent ce1a957 commit 875f1da

File tree

8 files changed

+12
-7
lines changed

8 files changed

+12
-7
lines changed

src/main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ function parseInputs {
5151
if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}" != "" ]; then
5252
tfCLICredentialsToken=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}
5353
fi
54+
55+
tfWorkspace="default"
56+
if [ -n "${TF_WORKSPACE}" ]; then
57+
tfWorkspace="${TF_WORKSPACE}"
58+
fi
5459
}
5560

5661
function configureCLICredentials {

src/terraform_apply.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ${applyOutput}
3333
3434
</details>
3535
36-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
36+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
3737

3838
applyCommentWrapper=$(stripColors "${applyCommentWrapper}")
3939
echo "apply: info: creating JSON"

src/terraform_fmt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ${fmtFileDiff}
5656
fmtCommentWrapper="#### \`terraform fmt\` Failed
5757
${fmtComment}
5858
59-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
59+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
6060

6161
fmtCommentWrapper=$(stripColors "${fmtCommentWrapper}")
6262
echo "fmt: info: creating JSON"

src/terraform_import.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ${importOutput}
3333
3434
</details>
3535
36-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
36+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
3737

3838
importCommentWrapper=$(stripColors "${importCommentWrapper}")
3939
echo "import: info: creating JSON"

src/terraform_init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function terraformInit {
2727
${initOutput}
2828
\`\`\`
2929
30-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
30+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
3131

3232
initCommentWrapper=$(stripColors "${initCommentWrapper}")
3333
echo "init: info: creating JSON"

src/terraform_plan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ${planOutput}
5353
5454
</details>
5555
56-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
56+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
5757

5858
planCommentWrapper=$(stripColors "${planCommentWrapper}")
5959
echo "plan: info: creating JSON"

src/terraform_taint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ${taintOutput}
3535
3636
</details>
3737
38-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
38+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
3939

4040
taintCommentWrapper=$(stripColors "${taintCommentWrapper}")
4141
echo "taint: info: creating JSON"

src/terraform_validate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function terraformValidate {
2727
${validateOutput}
2828
\`\`\`
2929
30-
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
30+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*"
3131

3232
validateCommentWrapper=$(stripColors "${validateCommentWrapper}")
3333
echo "validate: info: creating JSON"

0 commit comments

Comments
 (0)