Skip to content

Commit dc47399

Browse files
authored
change: defer to TF_WORKSPACE environment variable for arg-workspace selection input (#435)
* rely on `TF_WORKSPACE` environment variable input and drop support for `-or-create` argument Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * now to `TF_WORKSPACE` environment variable input instead of `arg-workspace` Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * bump terraform version in test Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * use semver ranges Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * use latest version Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * matrix input array conversion Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * env.TF_WORKSPACE ref Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * remove `-or-create` Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * remove show-args default value of `workspace` Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * some more echo Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * limit scope of test suite Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * echo `command` and `command_append` Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * echo steps.arg.outputs.arg-workspace Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * rearrange Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * clean up echo Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * consolidate `TF_WORKSPACE` Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * oops Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * consistent quotations in comments Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * test empty arg-refresh Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * bump input and output parameter headings Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * docs Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * d Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * d Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * inputs Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * spacing Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * wording Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * order Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * spacing Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * wording Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * wording Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * footer Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * rearrange Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * reminder Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> * ready to merge Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com> --------- Signed-off-by: Rishav Dhar <19497993+rdhar@users.noreply.github.com>
1 parent 8edfc59 commit dc47399

File tree

4 files changed

+87
-86
lines changed

4 files changed

+87
-86
lines changed

.github/assets/workflow.png

-38.6 KB
Binary file not shown.

.github/workflows/test_suite.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ jobs:
4444
if: matrix.tool == 'tofu'
4545
uses: opentofu/setup-opentofu@12f4debbf681675350b6cd1f0ff8ecfbda62027b # v1.0.4
4646
with:
47-
tofu_version: v1.9.0
47+
tofu_version: latest
4848
tofu_wrapper: false
4949

5050
- name: Setup Terraform
5151
if: matrix.tool == 'terraform'
5252
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
5353
with:
54-
terraform_version: v1.10.5
54+
terraform_version: latest
5555
terraform_wrapper: false
5656

5757
- name: Provision TF
@@ -60,12 +60,14 @@ jobs:
6060
uses: ./
6161
with:
6262
working-directory: tests/${{ matrix.test }}
63-
command: ${{ github.event.pull_request.merged && 'apply' || 'plan' }}
63+
arg-workspace: dev
6464
arg-lock: ${{ github.event.pull_request.merged }}
65+
arg-refresh: ${{ github.event.pull_request.merged && 'false' }}
66+
command: ${{ github.event.pull_request.merged && 'apply' || 'plan' }}
67+
comment-pr: ${{ matrix.tool == 'tofu' && 'always' || 'never' }}
6568
format: true
6669
validate: true
6770
tag-actor: never
68-
comment-pr: ${{ matrix.tool == 'tofu' && 'always' || 'never' }}
6971
tool: ${{ matrix.tool }}
7072

7173
- name: Echo TF

README.md

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<td>
2222
<ul>
2323
<li>Plan and apply changes with CLI arguments and <strong>encrypted plan file</strong> to avoid configuration drift.</li>
24-
<li>Outline diff changes within updated <strong>PR comment</strong> and matrix-friendly workflow summary, complete with log.</li>
24+
<li>Outline diff within up-to-date <strong>PR comment</strong> and matrix-friendly workflow summary, complete with log.</li>
2525
</ul>
2626
</td>
2727
<td>
@@ -35,7 +35,7 @@
3535

3636
</br>
3737

38-
### View: [Usage Examples](#usage-examples) · [In/Output Parameters](#parameters) · [Security](#security) · [Changelog](#changelog) · [License](#license)
38+
### View: [Usage Examples](#usage-examples) · [Inputs](#inputs) · [Outputs](#outputs) · [Security](#security) · [Changelog](#changelog) · [License](#license)
3939

4040
[![PR comment of plan output with "Diff of changes" section expanded.](/.github/assets/comment.png)](https://raw.githubusercontent.com/op5dev/tf-via-pr/refs/heads/main/.github/assets/comment.png "View full-size image.")
4141

@@ -68,28 +68,29 @@ jobs:
6868
with:
6969
terraform_wrapper: false
7070

71+
# Run plan by default, or apply on merge.
7172
- uses: op5dev/tf-via-pr@v13
7273
with:
73-
# Run plan by default, or apply on push to main.
7474
working-directory: path/to/directory
7575
command: ${{ github.event_name == 'push' && 'apply' || 'plan' }}
7676
arg-lock: ${{ github.event_name == 'push' }}
77+
arg-backend-config: env/dev.tfbackend
7778
arg-var-file: env/dev.tfvars
7879
arg-workspace: dev-use1
7980
plan-encrypt: ${{ secrets.PASSPHRASE }}
8081
```
8182
8283
> [!TIP]
8384
>
84-
> - All supported arguments (e.g., `-backend-config`, `-destroy`, `-parallelism`, etc.) are [listed below](#inputs---arguments).
85-
> - Environment variables can be passed in for cloud platform authentication (e.g., [configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials "Configuring AWS credentials for use in GitHub Actions.") for short-lived credentials).
85+
> - All supported arguments (e.g., `-backend-config`, `-destroy`, `-parallelism`, etc.) are [listed below](#arguments).
86+
> - Environment variables can be passed in for cloud platform authentication (e.g., [configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials "Configuring AWS credentials for use in GitHub Actions.") for short-lived credentials via OIDC).
8687
> - Recommend setting `terraform_wrapper`/`tofu_wrapper` to `false` in order to output the [detailed exit code](https://developer.hashicorp.com/terraform/cli/commands/plan#detailed-exitcode) for better error handling.
8788

8889
</br>
8990

9091
### Where to find more examples?
9192

92-
The following workflows showcase common use cases, while a comprehensive list of inputs is [documented](#parameters) below.
93+
The following workflows showcase common use cases, while a comprehensive list of inputs is [documented](#inputs) below.
9394

9495
<table>
9596
<tr>
@@ -134,7 +135,7 @@ The following workflows showcase common use cases, while a comprehensive list of
134135

135136
### How does encryption work?
136137

137-
Before the workflow uploads the plan file as an artifact, it can be encrypted with a passphrase (e.g., `${{ secrets.PASSPHRASE }}`) to prevent exposure of sensitive data using `plan-encrypt` input. This is done with [OpenSSL](https://docs.openssl.org/master/man1/openssl-enc/ "OpenSSL encryption documentation.")'s symmetric stream counter mode encryption with salt and pbkdf2.
138+
Before the workflow uploads the plan file as an artifact, it can be encrypted-at-rest with a passphrase using `plan-encrypt` input to prevent exposure of sensitive data (e.g., `${{ secrets.PASSPHRASE }}`). This is done with [OpenSSL](https://docs.openssl.org/master/man1/openssl-enc/ "OpenSSL encryption documentation.")'s symmetric stream counter mode ([256 bit AES in CTR](https://docs.openssl.org/3.3/man1/openssl-enc/#supported-ciphers:~:text=192/256%20bit-,AES%20in%20CTR%20mode,-aes%2D%5B128%7C192)) encryption with salt and pbkdf2.
138139

139140
In order to decrypt the plan file locally, use the following commands after downloading the artifact (adding a whitespace before `openssl` to prevent recording the command in shell history):
140141

@@ -149,102 +150,111 @@ openssl enc -d -aes-256-ctr -pbkdf2 -salt \
149150

150151
</br>
151152

152-
For each workflow run, a matrix-friendly job summary with logs is added as a fallback to the PR comment. Below this, you'll find a list of plan file artifacts generated during runtime.</br>
153-
154-
[![Workflow job summary with plan file artifact.](/.github/assets/workflow.png)](https://raw.githubusercontent.com/op5dev/tf-via-pr/refs/heads/main/.github/assets/workflow.png "View full-size image.")
155-
156-
</br>
157-
158-
## Parameters
159-
160-
### Inputs - Configuration
161-
162-
| Type | Name | Description |
163-
| -------- | ------------------- | ----------------------------------------------------------------------------------------------------------------- |
164-
| CLI | `working-directory` | Specify the working directory of TF code, alias of `arg-chdir`.</br>Example: `path/to/directory` |
165-
| CLI | `command` | Command to run between: `plan` or `apply`.<sup>1</sup></br>Example: `plan` |
166-
| CLI | `tool` | Provisioning tool to use between: `terraform` or `tofu`.</br>Default: `terraform` |
167-
| Check | `format` | Check format of TF code.</br>Default: `false` |
168-
| Check | `validate` | Check validation of TF code.</br>Default: `false` |
169-
| Check | `plan-parity` | Replace plan file if it matches a newly-generated one to prevent stale apply.<sup>2</sup></br>Default: `false` |
170-
| Security | `plan-encrypt` | Encrypt plan file artifact with the given input.<sup>3</sup></br>Example: `${{ secrets.PASSPHRASE }}` |
171-
| Security | `retention-days` | Duration after which plan file artifact will expire in days.</br>Example: `90` |
172-
| Security | `token` | Specify a GitHub token.</br>Default: `${{ github.token }}` |
173-
| UI | `label-pr` | Add a PR label with the command input (e.g., `tf:plan`).</br>Default: `true` |
174-
| UI | `comment-pr` | Add a PR comment: `always`, `on-change`, or `never`.<sup>4</sup></br>Default: `always` |
175-
| UI | `comment-method` | PR comment by: `update` existing comment or `recreate` and delete previous one.<sup>5</sup></br>Default: `update` |
176-
| UI | `tag-actor` | Tag the workflow triggering actor: `always`, `on-change`, or `never`.<sup>4</sup></br>Default: `always` |
177-
| UI | `hide-args` | Hide comma-separated list of CLI arguments from the command input.</br>Default: `detailed-exitcode,lock,out,var=` |
178-
| UI | `show-args` | Show comma-separated list of CLI arguments in the command input.</br>Default: `workspace` |
153+
## Inputs
154+
155+
All supported CLI argument inputs are [listed below](#arguments) with accompanying options, while workflow configuration inputs are listed here.
156+
157+
### Configuration
158+
159+
| Type | Name | Description |
160+
| -------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
161+
| CLI | `working-directory` | Specify the working directory of TF code, alias of `arg-chdir`.</br>Example: `path/to/directory` |
162+
| CLI | `command` | Command to run between: `plan` or `apply`.<sup>1</sup></br>Example: `plan` |
163+
| CLI | `tool` | Provisioning tool to use between: `terraform` or `tofu`.</br>Default: `terraform` |
164+
| Check | `format` | Check format of TF code.</br>Default: `false` |
165+
| Check | `validate` | Check validation of TF code.</br>Default: `false` |
166+
| Check | `plan-parity` | Replace plan file if it matches a newly-generated one to prevent stale apply.<sup>2</sup></br>Default: `false` |
167+
| Security | `plan-encrypt` | Encrypt plan file artifact with the given input.<sup>3</sup></br>Example: `${{ secrets.PASSPHRASE }}` |
168+
| Security | `retention-days` | Duration after which plan file artifact will expire in days.</br>Example: `90` |
169+
| Security | `token` | Specify a GitHub token.</br>Default: `${{ github.token }}` |
170+
| UI | `label-pr` | Add a PR label with the command input (e.g., `tf:plan`).</br>Default: `true` |
171+
| UI | `comment-pr` | Add a PR comment: `always`, `on-change`, or `never`.<sup>4</sup></br>Default: `always` |
172+
| UI | `comment-method` | PR comment by: `update` existing comment or `recreate` and delete previous one.<sup>5</sup></br>Default: `update` |
173+
| UI | `tag-actor` | Tag the workflow triggering actor: `always`, `on-change`, or `never`.<sup>4</sup></br>Default: `always` |
174+
| UI | `hide-args` | Hide comma-separated list of CLI arguments from the command input.<sup>6</sup></br>Default: `detailed-exitcode,parallelism,lock,out,var=` |
175+
| UI | `show-args` | Show comma-separated list of CLI arguments in the command input.<sup>6</sup></br>Default: `workspace` |
179176

180177
</br>
181178

182179
1. Both `command: plan` and `command: apply` include: `init`, `fmt` (with `format: true`), `validate` (with `validate: true`), and `workspace` (with `arg-workspace`) commands rolled into it automatically.</br>
183180
To separately run checks and/or generate outputs only, `command: init` can be used.</br></br>
184-
1. Originally intended for `merge_group` event trigger, `plan-parity: true` input helps prevent stale apply within a series of workflow runs when merging multiple PRs.</br></br>
181+
1. Originally intended for `merge_group` event trigger, `plan-parity: true` input helps to prevent stale apply within a series of workflow runs when merging multiple PRs.</br></br>
185182
1. The secret string input for `plan-encrypt` can be of any length, as long as it's consistent between encryption (plan) and decryption (apply).</br></br>
186-
1. The `on-change` option is true when the exit code of the last TF command is non-zero.</br></br>
183+
1. The `on-change` option is true when the exit code of the last TF command is non-zero (ensure `terraform_wrapper`/`tofu_wrapper` is set to `false`).</br></br>
187184
1. The default behavior of `comment-method` is to update the existing PR comment with the latest plan/apply output, making it easy to track changes over time through the comment's revision history.</br></br>
188-
[![PR comment revision history comparing plan and apply outputs.](/.github/assets/revisions.png)](https://raw.githubusercontent.com/op5dev/tf-via-pr/refs/heads/main/.github/assets/revisions.png "View full-size image.")
185+
[![PR comment revision history comparing plan and apply outputs.](/.github/assets/revisions.png)](https://raw.githubusercontent.com/op5dev/tf-via-pr/refs/heads/main/.github/assets/revisions.png "View full-size image.")</br></br>
186+
1. It can be desirable to hide certain arguments from the last run command input to prevent exposure in the PR comment (e.g., sensitive `arg-var` values). Conversely, it can be desirable to show other arguments even if they are not in last run command input (e.g., `arg-workspace` or `arg-backend-config` selection).
189187

190188
</br>
191189

192-
### Inputs - Arguments
190+
### Arguments
193191

194192
> [!NOTE]
195193
>
196-
> - Arguments are passed to the appropriate TF command(s) automatically, whether that's `init`, `workspace`, `validate`, `plan`, or `apply`.</br>
194+
> - Arguments are passed to the appropriate TF command(s) automatically, whether that's `fmt`, `init`, `validate`, `plan`, or `apply`.</br>
197195
> - For repeated arguments like `arg-var`, `arg-var-file`, `arg-backend-config`, `arg-replace` and `arg-target`, use commas to separate multiple values (e.g., `arg-var: key1=value1,key2=value2`).
198196

199-
<details><summary>Toggle view of all available CLI arguments.</summary>
200-
201197
</br>
202198

199+
Applicable to respective "plan" and "apply" `command` inputs ("init" included).
200+
203201
| Name | CLI Argument |
204202
| ------------------------- | ---------------------------------------- |
205203
| `arg-auto-approve` | `-auto-approve` |
206204
| `arg-backend-config` | `-backend-config` |
207205
| `arg-backend` | `-backend` |
208206
| `arg-backup` | `-backup` |
209-
| `arg-chdir` | `-chdir` |
210-
| `arg-check` | `-check`</br>Default: `true` |
207+
| `arg-chdir` | `-chdir`</br>Alias: `working-directory` |
211208
| `arg-compact-warnings` | `-compact-warnings` |
212209
| `arg-concise` | `-concise` |
213210
| `arg-destroy` | `-destroy` |
214211
| `arg-detailed-exitcode` | `-detailed-exitcode`</br>Default: `true` |
215-
| `arg-diff` | `-diff`</br>Default: `true` |
216212
| `arg-force-copy` | `-force-copy` |
217213
| `arg-from-module` | `-from-module` |
218214
| `arg-generate-config-out` | `-generate-config-out` |
219215
| `arg-get` | `-get` |
220-
| `arg-list` | `-list` |
221216
| `arg-lock-timeout` | `-lock-timeout` |
222217
| `arg-lock` | `-lock` |
223218
| `arg-lockfile` | `-lockfile` |
224219
| `arg-migrate-state` | `-migrate-state` |
225-
| `arg-no-tests` | `-no-tests` |
226-
| `arg-or-create` | `-or-create`</br>Default: `true` |
227220
| `arg-parallelism` | `-parallelism` |
228221
| `arg-plugin-dir` | `-plugin-dir` |
229222
| `arg-reconfigure` | `-reconfigure` |
230-
| `arg-recursive` | `-recursive`</br>Default: `true` |
231223
| `arg-refresh-only` | `-refresh-only` |
232224
| `arg-refresh` | `-refresh` |
233225
| `arg-replace` | `-replace` |
234226
| `arg-state-out` | `-state-out` |
235227
| `arg-state` | `-state` |
236228
| `arg-target` | `-target` |
237-
| `arg-test-directory` | `-test-directory` |
238229
| `arg-upgrade` | `-upgrade` |
239230
| `arg-var-file` | `-var-file` |
240231
| `arg-var` | `-var` |
241-
| `arg-workspace` | `-workspace` |
242-
| `arg-write` | `-write` |
243-
</details>
232+
| `arg-workspace` | `-workspace`</br>Alias: `TF_WORKSPACE` |
233+
234+
</br>
235+
236+
Applicable only when `format: true`.
237+
238+
| Name | CLI Argument |
239+
| --------------- | -------------------------------- |
240+
| `arg-check` | `-check`</br>Default: `true` |
241+
| `arg-diff` | `-diff`</br>Default: `true` |
242+
| `arg-list` | `-list` |
243+
| `arg-recursive` | `-recursive`</br>Default: `true` |
244+
| `arg-write` | `-write` |
245+
246+
</br>
247+
248+
Applicable only when `validate: true`.
249+
250+
| Name | CLI Argument |
251+
| -------------------- | ----------------- |
252+
| `arg-no-tests` | `-no-tests` |
253+
| `arg-test-directory` | `-test-directory` |
244254

245255
</br>
246256

247-
### Outputs
257+
## Outputs
248258

249259
| Type | Name | Description |
250260
| -------- | -------------- | --------------------------------------------- |
@@ -300,4 +310,4 @@ View [all notable changes](https://github.com/op5dev/tf-via-pr/releases "Release
300310

301311
- This project is licensed under the permissive [Apache License 2.0](LICENSE "Apache License 2.0.").
302312
- All works herein are my own, shared of my own volition, and [contributors](https://github.com/op5dev/tf-via-pr/graphs/contributors "Contributors.").
303-
- Copyright 2016-2025 [Rishav Dhar](https://github.com/rdhar "Rishav Dhar's GitHub profile.") — All wrongs reserved.
313+
- Copyright 2016-present [Rishav Dhar](https://github.com/rdhar "Rishav Dhar's GitHub profile.") — All wrongs reserved.

0 commit comments

Comments
 (0)