Skip to content

Commit 1b64573

Browse files
authored
Merge pull request #378 from dflook/refresh-2
Add refresh action and input for plan/apply
2 parents 7020a18 + 7b65258 commit 1b64573

File tree

30 files changed

+1130
-28
lines changed

30 files changed

+1130
-28
lines changed

.github/workflows/test-refresh.yaml

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
name: Test terraform-refresh
2+
3+
on:
4+
- pull_request
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
refresh:
11+
runs-on: ubuntu-24.04
12+
name: Refresh
13+
permissions:
14+
contents: read
15+
pull-requests: write
16+
env:
17+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
with:
22+
persist-credentials: false
23+
24+
- name: Apply
25+
uses: ./terraform-apply
26+
with:
27+
path: tests/workflows/test-refresh
28+
auto_approve: true
29+
30+
- name: Check no changes
31+
uses: ./terraform-check
32+
with:
33+
path: tests/workflows/test-refresh
34+
35+
- name: Make untracked changes
36+
run: |
37+
echo "qewasdasd" > tests/workflows/test-refresh/test
38+
echo "cxvbbxcbb" > tests/workflows/test-refresh/test2
39+
echo "tyuityuiy" > tests/workflows/test-refresh/test3
40+
41+
- name: Create a normal plan
42+
uses: ./terraform-plan
43+
id: plan-with-refresh
44+
with:
45+
add_github_comment: false
46+
path: tests/workflows/test-refresh
47+
48+
- name: Check normal plan picks up changes
49+
env:
50+
CHANGES: ${{ steps.plan-with-refresh.outputs.changes }}
51+
TO_ADD: ${{ steps.plan-with-refresh.outputs.to_add }}
52+
run: |
53+
if [[ "$CHANGES" != "true" ]]; then
54+
echo "::error:: Plan did not have changes"
55+
exit 1
56+
fi
57+
58+
if [[ "$TO_ADD" != "3" ]]; then
59+
echo "::error:: Wrong number of resources to add"
60+
exit 1
61+
fi
62+
63+
- name: Create a non-refresh plan
64+
uses: ./terraform-plan
65+
id: plan-without-refresh
66+
with:
67+
add_github_comment: false
68+
label: test-refresh refresh non-refresh
69+
path: tests/workflows/test-refresh
70+
refresh: false
71+
72+
- name: Check non-refresh plan doesn't pick up changes
73+
env:
74+
CHANGES: ${{ steps.plan-without-refresh.outputs.changes }}
75+
TO_ADD: ${{ steps.plan-without-refresh.outputs.to_add }}
76+
run: |
77+
if [[ "$CHANGES" != "false" ]]; then
78+
echo "::error:: Plan has changes"
79+
exit 1
80+
fi
81+
82+
- name: Targeted refresh
83+
uses: ./terraform-refresh
84+
with:
85+
path: tests/workflows/test-refresh
86+
target: |
87+
local_file.one
88+
89+
- name: Plan after targeted refresh
90+
uses: ./terraform-plan
91+
id: plan-after-targeted-refresh
92+
with:
93+
path: tests/workflows/test-refresh
94+
refresh: false
95+
96+
- name: Check plan after targeted refresh
97+
env:
98+
CHANGES: ${{ steps.plan-after-targeted-refresh.outputs.changes }}
99+
TO_ADD: ${{ steps.plan-after-targeted-refresh.outputs.to_add }}
100+
run: |
101+
if [[ "$CHANGES" != "true" ]]; then
102+
echo "::error:: Plan did not have changes"
103+
exit 1
104+
fi
105+
106+
if [[ "$TO_ADD" != "1" ]]; then
107+
echo "::error:: Wrong number of resources to add"
108+
exit 1
109+
fi
110+
111+
- name: Apply plan after targeted refresh
112+
uses: ./terraform-apply
113+
id: apply
114+
continue-on-error: true
115+
with:
116+
path: tests/workflows/test-refresh
117+
118+
- name: Check failed to apply
119+
env:
120+
OUTCOME: ${{ steps.apply.outcome }}
121+
FAILURE_REASON: ${{ steps.apply.outputs.failure-reason }}
122+
run: |
123+
if [[ "$OUTCOME" != "failure" ]]; then
124+
echo "Apply did not fail correctly"
125+
exit 1
126+
fi
127+
128+
if [[ "$FAILURE_REASON" != "plan-changed" ]]; then
129+
echo "::error:: failure-reason not set correctly"
130+
exit 1
131+
fi
132+
133+
- name: Apply without refresh
134+
uses: ./terraform-apply
135+
with:
136+
path: tests/workflows/test-refresh
137+
refresh: false
138+
139+
- name: Create another normal plan
140+
uses: ./terraform-plan
141+
id: plan-with-refresh-after-apply
142+
with:
143+
add_github_comment: false
144+
path: tests/workflows/test-refresh
145+
146+
- name: Check normal plan picks up changes
147+
env:
148+
CHANGES: ${{ steps.plan-with-refresh-after-apply.outputs.changes }}
149+
TO_ADD: ${{ steps.plan-with-refresh-after-apply.outputs.to_add }}
150+
run: |
151+
if [[ "$CHANGES" != "true" ]]; then
152+
echo "::error:: Plan did not have changes"
153+
exit 1
154+
fi
155+
156+
if [[ "$TO_ADD" != "2" ]]; then
157+
echo "::error:: Wrong number of resources to add"
158+
exit 1
159+
fi
160+
161+
- name: Full refresh
162+
uses: ./terraform-refresh
163+
with:
164+
path: tests/workflows/test-refresh
165+
166+
- name: Plan after full refresh
167+
uses: ./terraform-plan
168+
id: plan-after-full-refresh
169+
with:
170+
add_github_comment: false
171+
path: tests/workflows/test-refresh
172+
refresh: false
173+
174+
- name: Check plan after full refresh
175+
env:
176+
CHANGES: ${{ steps.plan-after-full-refresh.outputs.changes }}
177+
TO_ADD: ${{ steps.plan-after-full-refresh.outputs.to_add }}
178+
run: |
179+
if [[ "$CHANGES" != "true" ]]; then
180+
echo "::error:: Plan did not have changes"
181+
exit 1
182+
fi
183+
184+
if [[ "$TO_ADD" != "2" ]]; then
185+
echo "::error:: Wrong number of resources to add"
186+
exit 1
187+
fi

CHANGELOG.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ The actions are versioned as a suite. Some actions may have no change in behavio
1111

1212
When using an action you can specify the version as:
1313

14-
- `@v1.47.0` to use an exact release
15-
- `@v1.47` to use the latest patch release for the specific minor version
14+
- `@v1.48.0` to use an exact release
15+
- `@v1.48` to use the latest patch release for the specific minor version
1616
- `@v1` to use the latest patch release for the specific major version
1717

18+
## [1.48.0] - 2025-03-24
19+
20+
### Added
21+
- A `refresh` input for [dflook/terraform-plan](https://github.com/dflook/terraform-github-actions/tree/main/terraform-plan)/[tofu-plan](https://github.com/dflook/terraform-github-actions/tree/main/tofu-plan)
22+
and [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/tree/main/terraform-apply)/[tofu-apply](https://github.com/dflook/terraform-github-actions/tree/main/tofu-apply)
23+
24+
This defaults to `true` with the current behaviour of refreshing the state before planning or applying.
25+
When set to `false` the state will not be refreshed, which can be a lot faster but may result in an outdated plan.
26+
27+
- New [dflook/terraform-refresh](https://github.com/dflook/terraform-github-actions/tree/main/terraform-refresh)/[tofu-refresh](https://github.com/dflook/terraform-github-actions/tree/main/tofu-refresh)
28+
actions to update the state file to match the current state of the infrastructure, but doesn't make any changes to the infrastructure.
29+
1830
## [1.47.0] - 2025-02-28
1931

2032
### Added
@@ -724,6 +736,7 @@ First release of the GitHub Actions:
724736
- [dflook/terraform-new-workspace](terraform-new-workspace)
725737
- [dflook/terraform-destroy-workspace](terraform-destroy-workspace)
726738

739+
[1.48.0]: https://github.com/dflook/terraform-github-actions/compare/v1.47.0...v1.48.0
727740
[1.47.0]: https://github.com/dflook/terraform-github-actions/compare/v1.46.1...v1.47.0
728741
[1.46.1]: https://github.com/dflook/terraform-github-actions/compare/v1.46.0...v1.46.1
729742
[1.46.0]: https://github.com/dflook/terraform-github-actions/compare/v1.45.0...v1.46.0

README.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ Currently, there is just experimental support for OpenTofu, see [here](https://g
1111

1212
See the documentation for the available actions:
1313

14-
| Terraform | OpenTofu |
15-
|--------------------------------------------------------------------|---------------------------------------------------------|
16-
| [dflook/terraform-plan](terraform-plan) | [dflook/tofu-plan](tofu-plan) |
17-
| [dflook/terraform-apply](terraform-apply) | [dflook/tofu-apply](tofu-apply) |
18-
| [dflook/terraform-output](terraform-output) | [dflook/tofu-output](tofu-output) |
19-
| [dflook/terraform-remote-state](terraform-remote-state) | [dflook/tofu-remote-state](tofu-remote-state) |
20-
| [dflook/terraform-validate](terraform-validate) | [dflook/tofu-validate](tofu-validate) |
21-
| [dflook/terraform-fmt-check](terraform-fmt-check) | [dflook/tofu-fmt-check](tofu-fmt-check) |
22-
| [dflook/terraform-fmt](terraform-fmt) | [dflook/tofu-fmt](tofu-fmt) |
23-
| [dflook/terraform-check](terraform-check) | [dflook/tofu-check](tofu-check) |
24-
| [dflook/terraform-new-workspace](terraform-new-workspace) | [dflook/tofu-new-workspace](tofu-new-workspace) |
25-
| [dflook/terraform-destroy-workspace](terraform-destroy-workspace) | [dflook/tofu-destroy-workspace](tofu-destroy-workspace) |
26-
| [dflook/terraform-destroy](terraform-destroy) | [dflook/tofu-destroy](tofu-destroy) |
27-
| [dflook/terraform-version](terraform-version) | [dflook/tofu-version](tofu-version) |
28-
| [dflook/terraform-unlock-state](terraform-unlock-state) | [dflook/tofu-unlock-state](tofu-unlock-state) |
29-
| [dflook/terraform-test](terraform-test) | [dflook/tofu-test](tofu-test) |
14+
| Terraform | OpenTofu |
15+
|-------------------------------------------------------------------|---------------------------------------------------------|
16+
| [dflook/terraform-plan](terraform-plan) | [dflook/tofu-plan](tofu-plan) |
17+
| [dflook/terraform-apply](terraform-apply) | [dflook/tofu-apply](tofu-apply) |
18+
| [dflook/terraform-output](terraform-output) | [dflook/tofu-output](tofu-output) |
19+
| [dflook/terraform-remote-state](terraform-remote-state) | [dflook/tofu-remote-state](tofu-remote-state) |
20+
| [dflook/terraform-validate](terraform-validate) | [dflook/tofu-validate](tofu-validate) |
21+
| [dflook/terraform-fmt-check](terraform-fmt-check) | [dflook/tofu-fmt-check](tofu-fmt-check) |
22+
| [dflook/terraform-fmt](terraform-fmt) | [dflook/tofu-fmt](tofu-fmt) |
23+
| [dflook/terraform-check](terraform-check) | [dflook/tofu-check](tofu-check) |
24+
| [dflook/terraform-new-workspace](terraform-new-workspace) | [dflook/tofu-new-workspace](tofu-new-workspace) |
25+
| [dflook/terraform-destroy-workspace](terraform-destroy-workspace) | [dflook/tofu-destroy-workspace](tofu-destroy-workspace) |
26+
| [dflook/terraform-destroy](terraform-destroy) | [dflook/tofu-destroy](tofu-destroy) |
27+
| [dflook/terraform-version](terraform-version) | [dflook/tofu-version](tofu-version) |
28+
| [dflook/terraform-unlock-state](terraform-unlock-state) | [dflook/tofu-unlock-state](tofu-unlock-state) |
29+
| [dflook/terraform-test](terraform-test) | [dflook/tofu-test](tofu-test) |
30+
| [dflook/terraform-refresh](terraform-refresh) | [dflook/tofu-refresh](tofu-refresh) |
3031

3132
## Example Usage
3233

docs-gen/action.py

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def assert_ordering(self):
172172
"replace",
173173
"target",
174174
"destroy",
175+
"refresh",
175176
"plan_path",
176177
"auto_approve",
177178
"add_github_comment",

docs-gen/actions/apply.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from inputs.parallelism import parallelism
1717
from inputs.path import path
1818
from inputs.plan_path import plan_path as plan_path_input
19+
from inputs.refresh import refresh
1920
from inputs.replace import replace
2021
from inputs.target import target
2122
from inputs.var_file import var_file
@@ -83,6 +84,7 @@
8384
Set to `true` to destroy all resources.
8485
8586
This generates and applies a plan in [destroy mode]($DestroyModeUrl).'''),
87+
refresh,
8688
plan_path_input,
8789
auto_approve,
8890
parallelism
@@ -339,4 +341,4 @@
339341
auto_approve: true
340342
```
341343
'''
342-
)
344+
)

docs-gen/actions/plan.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from inputs.label import label
1717
from inputs.parallelism import parallelism
1818
from inputs.path import path
19+
from inputs.refresh import refresh
1920
from inputs.replace import replace
2021
from inputs.target import target
2122
from inputs.var import var
@@ -57,6 +58,7 @@
5758
replace,
5859
target,
5960
destroy,
61+
refresh,
6062
add_github_comment,
6163
parallelism
6264
],
@@ -251,4 +253,4 @@
251253
path: my-$ToolName-config
252254
```
253255
'''
254-
)
256+
)

docs-gen/actions/refresh.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import dataclasses
2+
3+
from action import Action
4+
from environment_variables.GITHUB_DOT_COM_TOKEN import GITHUB_DOT_COM_TOKEN
5+
from environment_variables.TERRAFORM_CLOUD_TOKENS import TERRAFORM_CLOUD_TOKENS
6+
from environment_variables.TERRAFORM_HTTP_CREDENTIALS import TERRAFORM_HTTP_CREDENTIALS
7+
from environment_variables.TERRAFORM_PRE_RUN import TERRAFORM_PRE_RUN
8+
from environment_variables.TERRAFORM_SSH_KEY import TERRAFORM_SSH_KEY
9+
from inputs.backend_config import backend_config
10+
from inputs.backend_config_file import backend_config_file
11+
from inputs.parallelism import parallelism
12+
from inputs.path import path
13+
from inputs.var_file import var_file
14+
from inputs.variables import variables
15+
from inputs.workspace import workspace
16+
from inputs.target import target
17+
from outputs.failure_reason import failure_reason
18+
from outputs.lock_info import lock_info
19+
from outputs.run_id import run_id
20+
21+
refresh = Action(
22+
'refresh',
23+
'''
24+
This actions runs a $ProductName apply operation in refresh-only mode.
25+
This will synchronise the $ProductName state with the actual resources, but will not make any changes to the resources.
26+
''',
27+
meta_description='Refresh $ProductName state',
28+
inputs=[
29+
dataclasses.replace(path, description="Path to the $ProductName root module to refresh state for"),
30+
dataclasses.replace(workspace, description="$ProductName workspace to run the refresh state in"),
31+
variables,
32+
var_file,
33+
backend_config,
34+
backend_config_file,
35+
dataclasses.replace(target, description='''
36+
List of resources to target, one per line.
37+
The refresh will be limited to these resources and their dependencies.
38+
'''),
39+
parallelism
40+
],
41+
outputs=[
42+
dataclasses.replace(failure_reason, description='''
43+
When the job outcome is `failure`, this output may be set. The value may be one of:
44+
45+
- `refresh-failed` - The $ProductName apply operation failed.
46+
- `state-locked` - The Terraform state lock could not be obtained because it was already locked.
47+
48+
If the job fails for any other reason this will not be set.
49+
This can be used with the Actions expression syntax to conditionally run steps.
50+
'''
51+
),
52+
lock_info,
53+
run_id
54+
],
55+
environment_variables=[
56+
GITHUB_DOT_COM_TOKEN,
57+
TERRAFORM_CLOUD_TOKENS,
58+
TERRAFORM_SSH_KEY,
59+
TERRAFORM_HTTP_CREDENTIALS,
60+
TERRAFORM_PRE_RUN,
61+
]
62+
)

0 commit comments

Comments
 (0)