Skip to content

Commit d08c2dc

Browse files
committed
Pass variables to workspace new/delete
1 parent f81a5e4 commit d08c2dc

File tree

8 files changed

+96
-10
lines changed

8 files changed

+96
-10
lines changed

.github/workflows/test-early-eval.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,27 @@ jobs:
2626
add_github_comment: false
2727
variables: |
2828
passphrase = "tofuqwertyuiopasdfgh"
29+
30+
- name: Create workspace
31+
uses: ./terraform-new-workspace
32+
with:
33+
path: tests/workflows/test-new-workspace
34+
workspace: test-workspace
35+
variables: |
36+
passphrase = "tofuqwertyuiopasdfgh"
37+
38+
- name: Create workspace again
39+
uses: ./terraform-new-workspace
40+
with:
41+
path: tests/workflows/test-new-workspace
42+
workspace: test-workspace
43+
variables: |
44+
passphrase = "tofuqwertyuiopasdfgh"
45+
46+
- name: Destroy workspace
47+
uses: ./terraform-destroy-workspace
48+
with:
49+
path: tests/workflows/test-new-workspace
50+
workspace: test-workspace
51+
variables: |
52+
passphrase = "tofuqwertyuiopasdfgh"

docs-gen/action.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Input:
4545
deprecation_message: str = None
4646
show_in_docs: bool = True
4747
example: str = None
48+
available_in: list[Type[Terraform] | Type[OpenTofu]] = dataclasses.field(default_factory=lambda: [Terraform, OpenTofu])
4849

4950
def markdown(self, tool: Tool) -> str:
5051
if self.deprecation_message is None:
@@ -226,6 +227,8 @@ def markdown(self, tool: Tool) -> str:
226227
for input in self.inputs:
227228
if not input.show_in_docs:
228229
continue
230+
if tool not in input.available_in:
231+
continue
229232
s += text_chunk(input.markdown(tool))
230233

231234
if self.outputs:
@@ -264,7 +267,7 @@ def action_yaml(self, tool: Tool) -> str:
264267
if self.inputs:
265268
s += 'inputs:\n'
266269

267-
for input in self.inputs:
270+
for input in (input for input in self.inputs if tool in input.available_in):
268271
s += f' {input.name}:\n'
269272

270273
description = input.meta_description or input.description

docs-gen/actions/destroy_workspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@
114114
workspace: ${{ github.head_ref }}
115115
```
116116
'''
117-
)
117+
)

docs-gen/actions/new_workspace.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22

3-
from action import Action
3+
from action import Action, OpenTofu
44
from environment_variables.GITHUB_DOT_COM_TOKEN import GITHUB_DOT_COM_TOKEN
55
from environment_variables.TERRAFORM_CLOUD_TOKENS import TERRAFORM_CLOUD_TOKENS
66
from environment_variables.TERRAFORM_HTTP_CREDENTIALS import TERRAFORM_HTTP_CREDENTIALS
@@ -9,6 +9,8 @@
99
from inputs.backend_config import backend_config
1010
from inputs.backend_config_file import backend_config_file
1111
from inputs.path import path
12+
from inputs.var_file import var_file
13+
from inputs.variables import variables
1214
from inputs.workspace import workspace
1315

1416
new_workspace = Action(
@@ -19,6 +21,12 @@
1921
inputs=[
2022
path,
2123
dataclasses.replace(workspace, description='The name of the $ProductName workspace to create.', required=True, default=None),
24+
dataclasses.replace(variables, description='''
25+
Variables to set when initializing $ProductName. This should be valid $ProductName syntax - like a [variable definition file]($VariableDefinitionUrl).
26+
27+
Variables set here override any given in `var_file`s.
28+
''', available_in=[OpenTofu]),
29+
dataclasses.replace(var_file, available_in=[OpenTofu]),
2230
backend_config,
2331
backend_config_file,
2432
],
@@ -62,4 +70,4 @@
6270
auto_approve: true
6371
```
6472
'''
65-
)
73+
)

image/entrypoints/destroy-workspace.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ else
3535
# We can't delete an active workspace, so re-initialize with a 'default' workspace (which may not exist)
3636
init-backend-default-workspace
3737

38-
debug_log terraform workspace delete -no-color -lock-timeout=300s "$INPUT_WORKSPACE"
39-
(cd "$INPUT_PATH" && terraform workspace delete -no-color -lock-timeout=300s "$INPUT_WORKSPACE")
38+
# shellcheck disable=SC2086
39+
debug_log $TOOL_COMMAND_NAME workspace delete $VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE"
40+
# shellcheck disable=SC2086
41+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace delete $VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE")
4042
fi

image/entrypoints/new-workspace.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ fi
1414
init-backend-default-workspace
1515

1616
set +e
17-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace list -no-color) \
17+
# shellcheck disable=SC2086
18+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace list $VARIABLE_ARGS -no-color) \
1819
2>"$STEP_TMP_DIR/terraform_workspace_list.stderr" \
1920
>"$STEP_TMP_DIR/terraform_workspace_list.stdout"
2021

@@ -32,12 +33,14 @@ fi
3233

3334
if workspace_exists "$INPUT_WORKSPACE" <"$STEP_TMP_DIR/terraform_workspace_list.stdout"; then
3435
echo "Workspace appears to exist, selecting it"
35-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select -no-color "$INPUT_WORKSPACE")
36+
# shellcheck disable=SC2086
37+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select $VARIABLE_ARGS -no-color "$INPUT_WORKSPACE")
3638
else
3739
echo "Workspace does not appear to exist, attempting to create it"
3840

3941
set +e
40-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace new -no-color -lock-timeout=300s "$INPUT_WORKSPACE") \
42+
# shellcheck disable=SC2086
43+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace new $VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE") \
4144
2>"$STEP_TMP_DIR/terraform_workspace_new.stderr" \
4245
>"$STEP_TMP_DIR/terraform_workspace_new.stdout"
4346

@@ -52,7 +55,8 @@ else
5255

5356
if grep -Fq "already exists" "$STEP_TMP_DIR/terraform_workspace_new.stderr"; then
5457
echo "Workspace does exist, selecting it"
55-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select -no-color "$INPUT_WORKSPACE")
58+
# shellcheck disable=SC2086
59+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select $VARIABLE_ARGS -no-color "$INPUT_WORKSPACE")
5660
else
5761
cat "$STEP_TMP_DIR/terraform_workspace_new.stderr"
5862
cat "$STEP_TMP_DIR/terraform_workspace_new.stdout"

tofu-new-workspace/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,40 @@ Creates a new OpenTofu workspace. If the workspace already exists, succeeds with
2121
- Type: string
2222
- Required
2323

24+
* `variables`
25+
26+
Variables to set when initializing OpenTofu. This should be valid OpenTofu syntax - like a [variable definition file](https://opentofu.org/docs/language/values/variables/#variable-definitions-tfvars-files).
27+
28+
Variables set here override any given in `var_file`s.
29+
30+
```yaml
31+
with:
32+
variables: |
33+
image_id = "${{ secrets.AMI_ID }}"
34+
availability_zone_names = [
35+
"us-east-1a",
36+
"us-west-1c",
37+
]
38+
```
39+
40+
- Type: string
41+
- Optional
42+
43+
* `var_file`
44+
45+
List of tfvars files to use, one per line.
46+
Paths should be relative to the GitHub Actions workspace
47+
48+
```yaml
49+
with:
50+
var_file: |
51+
common.tfvars
52+
prod.tfvars
53+
```
54+
55+
- Type: string
56+
- Optional
57+
2458
* `backend_config`
2559

2660
List of OpenTofu backend config values, one per line.

tofu-new-workspace/action.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ inputs:
1010
workspace:
1111
description: The name of the OpenTofu workspace to create.
1212
required: true
13+
variables:
14+
description: |
15+
Variables to set when initializing OpenTofu. This should be valid OpenTofu syntax - like a [variable definition file](https://opentofu.org/docs/language/values/variables/#variable-definitions-tfvars-files).
16+
17+
Variables set here override any given in `var_file`s.
18+
required: false
19+
var_file:
20+
description: |
21+
List of tfvars files to use, one per line.
22+
Paths should be relative to the GitHub Actions workspace
23+
required: false
1324
backend_config:
1425
description: List of OpenTofu backend config values, one per line.
1526
required: false

0 commit comments

Comments
 (0)