Skip to content

Commit b7ae89e

Browse files
Merge pull request #177 from aligent/feat/support-shell-vars-interpolation
Support shell variable interpolation in CDK context-values and extra-arguments
2 parents e84f051 + 1d463fe commit b7ae89e

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

.github/workflows/aws-cdk.yml

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ on:
4949

5050
# Advanced Configuration
5151
context-values:
52-
description: "CDK context values as JSON object"
52+
description: "CDK context as JSON. Supports $VAR/${VAR} interpolation (expanded after EXTRA_VARS/EXTRA_SECRETS)"
5353
type: string
5454
required: false
5555
default: "{}"
@@ -59,7 +59,7 @@ on:
5959
required: false
6060
default: ""
6161
extra-arguments:
62-
description: "Extra arguments"
62+
description: "Extra arguments. Supports $VAR/${VAR} interpolation (expanded after EXTRA_VARS/EXTRA_SECRETS)."
6363
type: string
6464
required: false
6565
debug:
@@ -234,10 +234,26 @@ jobs:
234234
done <<< "$EXTRA_SECRETS"
235235
fi
236236
237+
- name: Expand shell variables in inputs
238+
id: expand-inputs
239+
env:
240+
RAW_CONTEXT_VALUES: ${{ inputs.context-values }}
241+
RAW_EXTRA_ARGUMENTS: ${{ inputs.extra-arguments }}
242+
run: |
243+
echo "🔄 Expanding environment variables in inputs..."
244+
245+
expanded_context=$(echo "$RAW_CONTEXT_VALUES" | envsubst)
246+
expanded_args=$(echo "$RAW_EXTRA_ARGUMENTS" | envsubst)
247+
248+
echo "context-values=$expanded_context" >> $GITHUB_OUTPUT
249+
echo "extra-arguments=$expanded_args" >> $GITHUB_OUTPUT
250+
251+
echo "✅ Variable expansion complete"
252+
237253
- name: Validate required inputs
238254
id: validate-inputs
239255
env:
240-
CONTEXT_VALUES: ${{ inputs.context-values }}
256+
CONTEXT_VALUES: ${{ steps.expand-inputs.outputs.context-values }}
241257
ENVIRONMENT_TARGET: ${{ inputs.environment-target }}
242258
ENVIRONMENT: ${{ inputs.github-environment || 'Repository' }}
243259
INPUT_DEPLOY: ${{ inputs.deploy }}
@@ -336,7 +352,7 @@ jobs:
336352
- name: Configure CDK context
337353
id: context-config
338354
env:
339-
CONTEXT_VALUES: ${{ inputs.context-values }}
355+
CONTEXT_VALUES: ${{ steps.expand-inputs.outputs.context-values }}
340356
ENVIRONMENT_TARGET: ${{ inputs.environment-target }}
341357
run: |
342358
echo "⚙️ Configuring CDK context..."
@@ -384,7 +400,7 @@ jobs:
384400
AWS_REGION: ${{ inputs.aws-region }}
385401
BOOTSTRAP_CMD: ${{ inputs.bootstrap-command }}
386402
CFN_EXECUTION_ROLE: ${{ secrets.CFN_EXECUTION_ROLE }}
387-
EXTRA_ARGUMENTS: ${{ inputs.extra-arguments }}
403+
EXTRA_ARGUMENTS: ${{ steps.expand-inputs.outputs.extra-arguments }}
388404
FLAG_CFN_EXECUTION_POLICY: |-
389405
${{ case(
390406
secrets.CFN_EXECUTION_ROLE != '',
@@ -411,7 +427,7 @@ jobs:
411427
if: inputs.synth == true
412428
env:
413429
CONTEXT_ARGS: ${{ steps.context-config.outputs.args }}
414-
EXTRA_ARGUMENTS: ${{ inputs.extra-arguments }}
430+
EXTRA_ARGUMENTS: ${{ steps.expand-inputs.outputs.extra-arguments }}
415431
FLAG_VERBOSE: ${{ case(inputs.debug == true, '--verbose', '') }}
416432
STACK_NAME: ${{ inputs.stack-name || vars.STACK_NAME }}
417433
SYNTH_CMD: ${{ inputs.synth-command }}
@@ -439,7 +455,7 @@ jobs:
439455
env:
440456
CONTEXT_ARGS: ${{ steps.context-config.outputs.args }}
441457
DIFF_CMD: ${{ inputs.diff-command }}
442-
EXTRA_ARGUMENTS: ${{ inputs.extra-arguments }}
458+
EXTRA_ARGUMENTS: ${{ steps.expand-inputs.outputs.extra-arguments }}
443459
STACK_NAME: ${{ inputs.stack-name || vars.STACK_NAME }}
444460
run: |
445461
echo "📊 Analysing deployment changes..."
@@ -583,7 +599,7 @@ jobs:
583599
env:
584600
CONTEXT_ARGS: ${{ steps.context-config.outputs.args }}
585601
DEPLOY_CMD: ${{ inputs.deploy-command }}
586-
EXTRA_ARGUMENTS: ${{ inputs.extra-arguments }}
602+
EXTRA_ARGUMENTS: ${{ steps.expand-inputs.outputs.extra-arguments }}
587603
FLAG_VERBOSE: ${{ case(inputs.debug == true, '--verbose', '') }}
588604
STACK_NAME: ${{ inputs.stack-name || vars.STACK_NAME }}
589605
run: |

docs/aws-cdk.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ A streamlined AWS CDK workflow supporting multi-environment infrastructure synth
2828
| diff || boolean | false | Diff stack |
2929
| synth || boolean | false | Synth stack |
3030
| **Advanced Configuration** |
31-
| context-values || string | {} | CDK context values as JSON object |
31+
| context-values || string | {} | CDK context values as JSON object. Supports `$VAR` / `${VAR}` shell variable interpolation via `envsubst`. |
3232
| environment-target || string | | Target environment for CDK context (stg/prd/dev) - passed as `--context environment=<value>` |
33-
| extra-arguments || string | | Extra arguments as string |
33+
| extra-arguments || string | | Extra arguments as string. Supports `$VAR` / `${VAR}` shell variable interpolation via `envsubst`. |
3434
| debug || boolean | false | Enable verbose logging and debug output |
3535
| lfs || boolean | false | Enable Git LFS support for checkout |
3636
| runs-on || string | ubuntu-latest | GitHub runner (use `ubuntu-24.04-arm` for native ARM64 builds) |
@@ -218,6 +218,26 @@ jobs:
218218
secrets: inherit
219219
```
220220

221+
**Variable Interpolation in Context Values:**
222+
223+
`context-values` and `extra-arguments` support shell variable interpolation via `envsubst`. Variables are expanded after `EXTRA_VARS` and `EXTRA_SECRETS` are loaded into the environment, so you can reference any variable defined there.
224+
225+
> **Why is this needed?** GitHub Actions evaluates `${{ vars.* }}` expressions in the **caller's** context, which only has access to repository-level variables. Environment-scoped variables (configured via `github-environment`) are only available **inside** the reusable workflow at runtime. Variable interpolation bridges this gap, letting you reference environment-scoped values in `context-values` and `extra-arguments`.
226+
227+
```yaml
228+
jobs:
229+
deploy:
230+
uses: aligent/workflows/.github/workflows/aws-cdk.yml@main
231+
with:
232+
github-environment: Staging
233+
deploy: true
234+
context-values: '{"api-url": "${API_BASE_URL}", "version": "${BUILD_VERSION}"}'
235+
extra-arguments: --tags project=${PROJECT_NAME}
236+
secrets: inherit
237+
```
238+
239+
In this example, `API_BASE_URL`, `BUILD_VERSION`, and `PROJECT_NAME` would be set via `EXTRA_VARS` in the GitHub Environment.
240+
221241
**Deploy Production in NX Monorepo from Release:**
222242
```yaml
223243
on:

0 commit comments

Comments
 (0)