Skip to content

Commit 5ad3902

Browse files
authored
feat: add spacelift stack's after_run config, FINALLY hook (#52)
## what Allow the `after_run` (FINALLY hook) to be set for Spacelift stacks. ## why For use cases when a script is needed to be ran even if the apply/deployment doesn't go through. Similar to the try catch, finally. ## references https://registry.terraform.io/providers/spacelift-io/spacelift/latest/docs/resources/stack#after_run-1 ![image](https://github.com/user-attachments/assets/f31a4fef-4b9c-479f-899f-cc1335bc283f) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced an "after_run" hook that lets users define commands to be executed after a stack run, enhancing lifecycle control and customization. - Added support for the new `after_run` variable in the configuration options. - **Documentation** - Updated documentation to include the new `after_run` input variable and its usage details. - **Tests** - Added assertions to validate the `after_run` configuration in test cases. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a967f89 commit 5ad3902

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ This is to support easy local and outside-spacelift operations. Keeping variable
269269
| <a name="input_after_init"></a> [after\_init](#input\_after\_init) | List of after-init scripts | `list(string)` | `[]` | no |
270270
| <a name="input_after_perform"></a> [after\_perform](#input\_after\_perform) | List of after-perform scripts | `list(string)` | `[]` | no |
271271
| <a name="input_after_plan"></a> [after\_plan](#input\_after\_plan) | List of after-plan scripts | `list(string)` | `[]` | no |
272+
| <a name="input_after_run"></a> [after\_run](#input\_after\_run) | List of after-run (aka `finally` hook) scripts | `list(string)` | `[]` | no |
272273
| <a name="input_all_root_modules_enabled"></a> [all\_root\_modules\_enabled](#input\_all\_root\_modules\_enabled) | When set to true, all subdirectories in root\_modules\_path will be treated as root modules. | `bool` | `false` | no |
273274
| <a name="input_autodeploy"></a> [autodeploy](#input\_autodeploy) | Flag to enable/disable automatic deployment of the stack | `bool` | `true` | no |
274275
| <a name="input_autoretry"></a> [autoretry](#input\_autoretry) | Flag to enable/disable automatic retry of the stack | `bool` | `false` | no |

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ resource "spacelift_stack" "default" {
335335
after_init = compact(concat(try(local.stack_configs[each.key].after_init, []), var.after_init))
336336
after_perform = compact(concat(try(local.stack_configs[each.key].after_perform, []), var.after_perform))
337337
after_plan = compact(concat(try(local.stack_configs[each.key].after_plan, []), var.after_plan))
338+
after_run = compact(concat(try(local.stack_configs[each.key].after_run, []), var.after_run))
338339
autodeploy = coalesce(try(local.stack_configs[each.key].autodeploy, null), var.autodeploy)
339340
autoretry = try(local.stack_configs[each.key].autoretry, var.autoretry)
340341
before_apply = compact(coalesce(try(local.stack_configs[each.key].before_apply, []), var.before_apply))

stack-config.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@
8080
},
8181
"description": "Commands to run after plan"
8282
},
83+
"after_run": {
84+
"type": "array",
85+
"items": {
86+
"type": "string"
87+
},
88+
"description": "Commands to run after run (aka `finally` hook)."
89+
},
8390
"autodeploy": {
8491
"type": "boolean",
8592
"description": "Whether to automatically deploy changes"

tests/fixtures/multi-instance/root-module-a/stacks/default-example.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ stack_settings:
77
after_init: [echo 'after_init']
88
after_perform: [echo 'after_perform']
99
after_plan: [echo 'after_plan']
10+
after_run: [echo 'after_run']
1011
autodeploy: false
1112
autoretry: true
1213
before_apply: [echo 'before_apply']

tests/main.tftest.hcl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ run "test_default_example_stack_final_values" {
6262
error_message = "after_plan was not correct on the default-example stack: ${jsonencode(spacelift_stack.default["root-module-a-default-example"])}"
6363
}
6464

65+
# after_run
66+
assert {
67+
condition = contains(spacelift_stack.default["root-module-a-default-example"].after_run, "echo 'after_run'")
68+
error_message = "after_run was not correct on the default-example stack: ${jsonencode(spacelift_stack.default["root-module-a-default-example"])}"
69+
}
70+
6571
# autodeploy
6672
assert {
6773
condition = spacelift_stack.default["root-module-a-default-example"].autodeploy == false
@@ -228,6 +234,7 @@ run "test_default_example_stack_runtime_overrides" {
228234
after_init = ["echo 'changed_after_init'"]
229235
after_perform = ["echo 'changed_after_perform'"]
230236
after_plan = ["echo 'changed_after_plan'"]
237+
after_run = ["echo 'changed_after_run'"]
231238
autodeploy = true
232239
autoretry = false
233240
before_apply = ["echo 'changed_before_apply'"]
@@ -305,6 +312,12 @@ run "test_default_example_stack_runtime_overrides" {
305312
error_message = "after_plan override was not applied correctly: ${jsonencode(spacelift_stack.default["root-module-a-default-example"])}"
306313
}
307314

315+
# after_run
316+
assert {
317+
condition = contains(spacelift_stack.default["root-module-a-default-example"].after_run, "echo 'changed_after_run'")
318+
error_message = "after_run override was not applied correctly: ${jsonencode(spacelift_stack.default["root-module-a-default-example"])}"
319+
}
320+
308321
# autodeploy
309322
assert {
310323
condition = spacelift_stack.default["root-module-a-default-example"].autodeploy == true

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ variable "after_plan" {
164164
default = []
165165
}
166166

167+
variable "after_run" {
168+
type = list(string)
169+
description = "List of after-run (aka `finally` hook) scripts"
170+
default = []
171+
}
172+
167173
variable "autodeploy" {
168174
type = bool
169175
description = "Flag to enable/disable automatic deployment of the stack"

0 commit comments

Comments
 (0)