Skip to content

Commit 1655d20

Browse files
authored
Merge pull request #239 from dflook/test-destroy-lock-info
Test destroy lock info
2 parents e4a43bf + 6a73940 commit 1655d20

File tree

5 files changed

+130
-79
lines changed

5 files changed

+130
-79
lines changed

.github/workflows/test-unlock-state.yaml

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,59 @@ jobs:
4646
fi
4747
4848
# Check state-locked
49-
- name: Try using locked state
49+
- name: Try using locked state using terraform-apply
5050
uses: ./terraform-apply
51-
id: locked-state
51+
id: locked-state-apply
5252
continue-on-error: true
5353
with:
5454
path: tests/workflows/test-unlock-state
5555
auto_approve: true
5656

57-
- name: Check state locked failure-reason
57+
- name: Check terraform-apply state locked failure-reason
58+
run: |
59+
if [[ "${{ steps.locked-state-apply.outcome }}" != "failure" ]]; then
60+
echo "Apply did not fail correctly"
61+
exit 1
62+
fi
63+
64+
if [[ "${{ steps.locked-state-apply.outputs.failure-reason }}" != "state-locked" ]]; then
65+
echo "::error:: failure-reason not set correctly"
66+
exit 1
67+
fi
68+
69+
echo '"${{ steps.locked-state-apply.outputs.lock-info }}"'
70+
71+
echo 'Lock id is ${{ fromJson(steps.locked-state-apply.outputs.lock-info).ID }}'
72+
73+
- name: Try using locked state using terraform-destroy
74+
uses: ./terraform-destroy
75+
id: locked-state-destroy
76+
continue-on-error: true
77+
with:
78+
path: tests/workflows/test-unlock-state
79+
80+
- name: Check terraform-destroy state locked failure-reason
5881
run: |
59-
if [[ "${{ steps.locked-state.outcome }}" != "failure" ]]; then
82+
if [[ "${{ steps.locked-state-destroy.outcome }}" != "failure" ]]; then
6083
echo "Apply did not fail correctly"
6184
exit 1
6285
fi
6386
64-
if [[ "${{ steps.locked-state.outputs.failure-reason }}" != "state-locked" ]]; then
87+
if [[ "${{ steps.locked-state-destroy.outputs.failure-reason }}" != "state-locked" ]]; then
6588
echo "::error:: failure-reason not set correctly"
6689
exit 1
6790
fi
6891
69-
echo '"${{ steps.locked-state.outputs.lock-info }}"'
92+
echo '"${{ steps.locked-state-destroy.outputs.lock-info }}"'
7093
71-
echo 'Lock id is ${{ fromJson(steps.locked-state.outputs.lock-info).ID }}'
94+
echo 'Lock id is ${{ fromJson(steps.locked-state-destroy.outputs.lock-info).ID }}'
7295
7396
- name: Unlock the state
7497
uses: ./terraform-unlock-state
7598
continue-on-error: true
7699
with:
77100
path: tests/workflows/test-unlock-state
78-
lock_id: ${{ fromJson(steps.locked-state.outputs.lock-info).ID }}
101+
lock_id: ${{ fromJson(steps.locked-state-apply.outputs.lock-info).ID }}
79102

80103
- name: Check state is not locked
81104
uses: ./terraform-apply
@@ -154,6 +177,30 @@ jobs:
154177
155178
echo 'Lock id is ${{ fromJson(steps.locked-state-workspace.outputs.lock-info).ID }}'
156179
180+
- name: Try using locked state using terraform-destroy-workspace
181+
uses: ./terraform-destroy-workspace
182+
id: locked-state-destroy-workspace
183+
continue-on-error: true
184+
with:
185+
path: tests/workflows/test-unlock-state
186+
workspace: hello
187+
188+
- name: Check terraform-destroy-workspace state locked failure-reason
189+
run: |
190+
if [[ "${{ steps.locked-state-destroy-workspace.outcome }}" != "failure" ]]; then
191+
echo "Apply did not fail correctly"
192+
exit 1
193+
fi
194+
195+
if [[ "${{ steps.locked-state-destroy-workspace.outputs.failure-reason }}" != "state-locked" ]]; then
196+
echo "::error:: failure-reason not set correctly"
197+
exit 1
198+
fi
199+
200+
echo '"${{ steps.locked-state-destroy-workspace.outputs.lock-info }}"'
201+
202+
echo 'Lock id is ${{ fromJson(steps.locked-state-destroy-workspace.outputs.lock-info).ID }}'
203+
157204
- name: Unlock the state
158205
uses: ./terraform-unlock-state
159206
continue-on-error: true

terraform-apply/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,28 @@ These input values must be the same as any `terraform-plan` for the same configu
222222

223223
- `apply-failed` - The Terraform apply operation failed.
224224
- `plan-changed` - The approved plan is no longer accurate, so the apply will not be attempted.
225+
- `state-locked` - The Terraform state lock could not be obtained because it was already locked.
225226

226227
If the job fails for any other reason this will not be set.
227228
This can be used with the Actions expression syntax to conditionally run steps.
228229

230+
* `lock-info`
231+
232+
When the job outcome is `failure` and the failure-reason is `state-locked`, this output will be set.
233+
234+
It is a json object containing any available state lock information and typically has the form:
235+
```json
236+
{
237+
"ID": "838fbfde-c5cd-297f-84a4-d7578b4a4880",
238+
"Path": "terraform-github-actions/test-unlock-state",
239+
"Operation": "OperationTypeApply",
240+
"Who": "root@e9d43b0c6478",
241+
"Version": "1.3.7",
242+
"Created": "2023-01-28 00:16:41.560904373 +0000 UTC",
243+
"Info": ""
244+
}
245+
```
246+
229247
* `run_id`
230248

231249
If the root module uses the `remote` or `cloud` backend in remote execution mode, this output will be set to the remote run id.

terraform-destroy-workspace/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,30 @@ This action uses the `terraform destroy` command to destroy all resources in a t
101101

102102
* `failure-reason`
103103

104-
When the job outcome is `failure` because the terraform destroy operation failed, this is set to `destroy-failed`.
104+
When the job outcome is `failure`, this output may be set. The value may be one of:
105+
106+
- `destroy-failed` - The Terraform destroy operation failed.
107+
- `state-locked` - The Terraform state lock could not be obtained because it was already locked.
108+
105109
If the job fails for any other reason this will not be set.
106-
This can be used with the Actions expression syntax to conditionally run a step when the destroy fails.
110+
This can be used with the Actions expression syntax to conditionally run a steps.
111+
112+
* `lock-info`
113+
114+
When the job outcome is `failure` and the failure-reason is `state-locked`, this output will be set.
115+
116+
It is a json object containing any available state lock information and typically has the form:
117+
```json
118+
{
119+
"ID": "838fbfde-c5cd-297f-84a4-d7578b4a4880",
120+
"Path": "terraform-github-actions/test-unlock-state",
121+
"Operation": "OperationTypeApply",
122+
"Who": "root@e9d43b0c6478",
123+
"Version": "1.3.7",
124+
"Created": "2023-01-28 00:16:41.560904373 +0000 UTC",
125+
"Info": ""
126+
}
127+
```
107128

108129
## Environment Variables
109130

terraform-destroy/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,30 @@ This action uses the `terraform destroy` command to destroy all resources in a t
108108

109109
* `failure-reason`
110110

111-
When the job outcome is `failure` because the terraform destroy operation failed, this is set to `destroy-failed`.
111+
When the job outcome is `failure`, this output may be set. The value may be one of:
112+
113+
- `destroy-failed` - The Terraform destroy operation failed.
114+
- `state-locked` - The Terraform state lock could not be obtained because it was already locked.
115+
112116
If the job fails for any other reason this will not be set.
113-
This can be used with the Actions expression syntax to conditionally run a step when the destroy fails.
117+
This can be used with the Actions expression syntax to conditionally run a steps.
118+
119+
* `lock-info`
120+
121+
When the job outcome is `failure` and the failure-reason is `state-locked`, this output will be set.
122+
123+
It is a json object containing any available state lock information and typically has the form:
124+
```json
125+
{
126+
"ID": "838fbfde-c5cd-297f-84a4-d7578b4a4880",
127+
"Path": "terraform-github-actions/test-unlock-state",
128+
"Operation": "OperationTypeApply",
129+
"Who": "root@e9d43b0c6478",
130+
"Version": "1.3.7",
131+
"Created": "2023-01-28 00:16:41.560904373 +0000 UTC",
132+
"Info": ""
133+
}
134+
```
114135

115136
## Environment Variables
116137

terraform-unlock-state/README.md

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,35 @@
22

33
This is one of a suite of terraform related actions - find them at [dflook/terraform-github-actions](https://github.com/dflook/terraform-github-actions).
44

5-
This actions is intially planned to manually unlock the state for the defined configuration.
6-
7-
The `GITHUB_TOKEN` environment variable must be set for the PR comment to be added.
8-
The action can be run on other events, which prints the plan to the workflow log.
5+
Force unlocks a Terraform remote state.
96

107
## Inputs
118

129
* `path`
1310

14-
Path to the terraform root module to apply
11+
Path to the terraform root module that defines the remote state to unlock
1512

1613
- Type: string
1714
- Optional
1815
- Default: The action workspace
1916

2017
* `workspace`
2118

22-
Terraform workspace to run
19+
Terraform workspace to unlock the remote state for
2320

2421
- Type: string
2522
- Optional
2623
- Default: `default`
2724

2825
* `lock_id`
2926

30-
Lock id from the defined configuration
27+
The ID of the state lock to release
3128

3229
- Type: string
3330
- Required
3431

3532
## Environment Variables
3633

37-
* `GITHUB_TOKEN`
38-
39-
The GitHub authorization token to use to create comments on a PR.
40-
The token provided by GitHub Actions can be used - it can be passed by
41-
using the `${{ secrets.GITHUB_TOKEN }}` expression, e.g.
42-
43-
```yaml
44-
env:
45-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46-
```
47-
48-
The token provided by GitHub Actions will work with the default permissions.
49-
The minimum permissions are `pull-requests: write`.
50-
It will also likely need `contents: read` so the job can checkout the repo.
51-
52-
You can also use any other App token that has `pull-requests: write` permission.
53-
54-
You can use a fine-grained Personal Access Token which has repository permissions:
55-
- Read access to metadata
56-
- Read and Write access to pull requests
57-
58-
You can also use a classic Personal Access Token which has the `repo` scope.
59-
60-
The GitHub user or app that owns the token will be the PR comment author.
61-
62-
- Type: string
63-
- Optional
64-
65-
* `TERRAFORM_ACTIONS_GITHUB_TOKEN`
66-
67-
When this is set it is used instead of `GITHUB_TOKEN`, with the same behaviour.
68-
The GitHub terraform provider also uses the `GITHUB_TOKEN` environment variable,
69-
so this can be used to make the github actions and the terraform provider use different tokens.
70-
71-
- Type: string
72-
- Optional
73-
7434
* `TERRAFORM_CLOUD_TOKENS`
7535

7636
API tokens for terraform cloud hosts, of the form `<host>=<token>`. Multiple tokens may be specified, one per line.
@@ -133,22 +93,6 @@ The action can be run on other events, which prints the plan to the workflow log
13393
- Type: string
13494
- Optional
13595

136-
* `TF_PLAN_COLLAPSE_LENGTH`
137-
138-
When PR comments are enabled, the terraform output is included in a collapsable pane.
139-
140-
If a terraform plan has fewer lines than this value, the pane is expanded
141-
by default when the comment is displayed.
142-
143-
```yaml
144-
env:
145-
TF_PLAN_COLLAPSE_LENGTH: 30
146-
```
147-
148-
- Type: integer
149-
- Optional
150-
- Default: 10
151-
15296
* `TERRAFORM_PRE_RUN`
15397

15498
A set of commands that will be ran prior to `terraform init`. This can be used to customise the environment before running terraform.
@@ -181,26 +125,26 @@ on:
181125
workflow_dispatch:
182126
inputs:
183127
path:
184-
description: "Path to the terraform files configuration"
128+
description: "Path to the terraform root module"
185129
required: true
186130
lock_id:
187131
description: "Lock ID to be unlocked"
188132
required: true
189133
134+
env:
135+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
136+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
137+
190138
jobs:
191139
unlock:
192-
name: Setup and unlock
140+
name: Unlock
193141
runs-on: ubuntu-latest
194-
env:
195-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
196142
steps:
197143
- name: Checkout current branch
198144
uses: actions/checkout@v3
199-
with:
200-
fetch-depth: 0
201145
202146
- name: Terraform Unlock
203-
uses: patricktalmeida/terraform-github-actions/terraform-unlock-state@add-unlock-state
147+
uses: dflook/terraform-unlock-state@v1
204148
with:
205149
path: ${{ github.event.inputs.path }}
206150
lock_id: ${{ github.event.inputs.lock_id }}

0 commit comments

Comments
 (0)