Skip to content

Commit 3db5a43

Browse files
Allow speculative run
1 parent 7a9d257 commit 3db5a43

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

image/entrypoints/plan.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ set-plan-args
1111
exec 3>&1
1212

1313
### Generate a plan
14-
PLAN_OUT="$STEP_TMP_DIR/plan.out"
14+
if [[ "$INPUT_SPECULATIVE" == "true" ]]; then
15+
# Force speculative plan - no -out flag
16+
PLAN_OUT=""
17+
else
18+
# Normal behavior - try to save plan file
19+
PLAN_OUT="$STEP_TMP_DIR/plan.out"
20+
fi
1521
PLAN_ARGS="$PLAN_ARGS -lock=false"
1622
plan
1723

18-
if [[ $PLAN_EXIT -eq 1 ]]; then
24+
# If plan failed because remote backend doesn't support -out flag, retry without it
25+
# Skip this retry if we're already running speculative (PLAN_OUT is already empty)
26+
if [[ $PLAN_EXIT -eq 1 && -n "$PLAN_OUT" ]]; then
1927
if grep -q "Saving a generated plan is currently not supported" "$STEP_TMP_DIR/terraform_plan.stderr"; then
2028
# This terraform module is using the remote backend, which is deficient.
2129
PLAN_OUT=""
22-
PLAN_ARGS="$PLAN_ARGS -lock=false"
2330
plan
2431
fi
2532
fi

terraform-plan/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ The [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/
170170
- Optional
171171
- Default: The Terraform default (10).
172172

173+
* `speculative`
174+
175+
Set to `true` to force a speculative plan that cannot be applied.
176+
177+
This creates a "Planned and finished" run in Terraform Cloud instead of "Planned and saved".
178+
Speculative plans don't lock state and can run in parallel with other operations.
179+
180+
This is useful for PR workflows where you want to preview changes without blocking other runs.
181+
182+
```yaml
183+
with:
184+
speculative: true
185+
```
186+
187+
- Type: boolean
188+
- Optional
189+
- Default: `false`
190+
173191
## Outputs
174192

175193
* `changes`
@@ -185,6 +203,8 @@ The [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/
185203

186204
The plan can be used as the `plan_file` input to the [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/tree/main/terraform-apply) action.
187205

206+
This won't be set if `speculative` is `true` or if the backend type is `remote`/`cloud` in remote execution mode.
207+
188208
Terraform plans often contain sensitive information, so this output should be treated with care.
189209

190210
- Type: string

terraform-plan/action.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ inputs:
7878
description: Limit the number of concurrent operations
7979
required: false
8080
default: "0"
81+
speculative:
82+
description: |
83+
Set to `true` to force a speculative plan that cannot be applied.
84+
85+
This creates a "Planned and finished" run in Terraform Cloud instead of "Planned and saved".
86+
Speculative plans don't lock state and can run in parallel with other operations.
87+
88+
This is useful for PR workflows where you want to preview changes without blocking other runs.
89+
required: false
90+
default: "false"
8191

8292
outputs:
8393
changes:
@@ -89,6 +99,8 @@ outputs:
8999
90100
The plan can be used as the `plan_file` input to the [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/tree/main/terraform-apply) action.
91101
102+
This won't be set if `speculative` is `true` or if the backend type is `remote`/`cloud` in remote execution mode.
103+
92104
Terraform plans often contain sensitive information, so this output should be treated with care.
93105
json_plan_path:
94106
description: |

0 commit comments

Comments
 (0)