Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions .github/actions/resolve-aws-deploy-role/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Resolve AWS Deployment Role
description: >
Validates the deployment role and region inputs and selects the role for this run, before any
AWS credentials are requested. Tag runs deploy production and require productionRoleArn; all other
runs deploy development and use roleArn. There is no fallback between the two.
inputs:
roleArn:
description: Development deployment role ARN (non-tag runs).
required: false
default: ""
productionRoleArn:
description: Production deployment role ARN (tag runs).
required: false
default: ""
awsRegion:
description: AWS region.
required: true
ref:
description: The run's git ref (github.ref); refs/tags/* selects production.
required: true
outputs:
role-arn:
description: The role ARN to assume.
value: ${{ steps.resolve.outputs.role-arn }}
environment:
description: "`production` for tag runs, otherwise `development`."
value: ${{ steps.resolve.outputs.environment }}
runs:
using: composite
steps:
- id: resolve
shell: bash
env:
ROLE_ARN: ${{ inputs.roleArn }}
PRODUCTION_ROLE_ARN: ${{ inputs.productionRoleArn }}
AWS_REGION_INPUT: ${{ inputs.awsRegion }}
RUN_REF: ${{ inputs.ref }}
run: |
set -euo pipefail
# IAM role ARN: arn:<partition>:iam::<account>:role/<optional path segments/><name>. The path
# (with its leading and trailing slash) is at most 512 printable non-space ASCII characters;
# the name is 1-64 characters of [A-Za-z0-9+=,.@_-].
is_role_arn() {
local LC_ALL=C rest name path
[[ "$1" =~ ^arn:aws(-cn|-us-gov)?:iam::[0-9]{12}:role/(.+)$ ]] || return 1
rest="${BASH_REMATCH[2]}"
name="${rest##*/}"
path="/"
[[ "$rest" == */* ]] && path="/${rest%/*}/"
[[ "$name" =~ ^[A-Za-z0-9+=,.@_-]{1,64}$ ]] || return 1
(( ${#path} <= 512 )) || return 1
[[ "$path" =~ ^[[:graph:]]+$ ]]
}

[[ "$AWS_REGION_INPUT" =~ ^[a-z]{2}(-[a-z]+)+-[0-9]$ ]] \
|| { echo "::error::awsRegion '$AWS_REGION_INPUT' is not an AWS region (for example us-east-1)."; exit 1; }

if [[ "$RUN_REF" == refs/tags/* ]]; then
environment=production
input=productionRoleArn
selected="$PRODUCTION_ROLE_ARN"
if [[ -z "$selected" ]]; then
echo "::error::This tag run deploys production, but productionRoleArn is not set. build-deploy never falls back to roleArn for production. Set productionRoleArn (to keep the previous single-role behavior, pass the same ARN as roleArn)."
exit 1
fi
else
environment=development
input=roleArn
selected="$ROLE_ARN"
if [[ -z "$selected" ]]; then
echo "::error::runCdk is true, but roleArn is not set. Set roleArn to the development deployment role, or set runCdk: false."
exit 1
fi
fi

is_role_arn "$selected" \
|| { echo "::error::$input is not an IAM role ARN (arn:aws:iam::<12-digit account>:role/[<path>/]<name>)."; exit 1; }

echo "Deploying $environment with the role from $input."
echo "role-arn=$selected" >> "$GITHUB_OUTPUT"
echo "environment=$environment" >> "$GITHUB_OUTPUT"
52 changes: 52 additions & 0 deletions .github/scripts/check-msbuild-env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""Fails when a workflow or composite action exports an MSBuild output-path property as an env var.

MSBuild reads environment variables as properties (case-insensitively), so an environment variable
such as `outputPath` silently becomes `OutputPath` for every project built in that job and redirects
all build output into one folder (#45). Checks every `env:` mapping and every `NAME=... >> $GITHUB_ENV`
write under .github/workflows and .github/actions.
"""
import pathlib
import re
import sys

import yaml

RESERVED = {
"outputpath", "outdir", "baseoutputpath", "intermediateoutputpath",
"baseintermediateoutputpath", "publishdir", "packageoutputpath",
}
GITHUB_ENV_WRITE = re.compile(r'''^\s*echo\s+["']?([A-Za-z_][A-Za-z0-9_]*)=.*>>\s*"?\$\{?GITHUB_ENV''', re.MULTILINE)


def walk(node, path, problems, source):
if isinstance(node, dict):
for key, value in node.items():
if key == "env" and isinstance(value, dict):
for name in value:
if str(name).lower() in RESERVED:
problems.append(f"{source}: {'/'.join(path)}/env/{name}")
if key == "run" and isinstance(value, str):
for name in GITHUB_ENV_WRITE.findall(value):
if name.lower() in RESERVED:
problems.append(f"{source}: {'/'.join(path)} writes {name} to $GITHUB_ENV")
walk(value, path + [str(key)], problems, source)
elif isinstance(node, list):
for index, item in enumerate(node):
walk(item, path + [str(index)], problems, source)


def main(root: str) -> int:
base = pathlib.Path(root)
files = sorted(base.glob(".github/workflows/*.y*ml")) + sorted(base.glob(".github/actions/**/action.y*ml"))
problems: list[str] = []
for file in files:
walk(yaml.safe_load(file.read_text()), [], problems, str(file.relative_to(base)))
for problem in problems:
print(f"::error::MSBuild reads environment variables as properties; rename this variable: {problem}")
print(f"Checked {len(files)} files; {len(problems)} MSBuild property collision(s).")
return 1 if problems else 0


if __name__ == "__main__":
sys.exit(main(sys.argv[1] if len(sys.argv) > 1 else "."))
49 changes: 39 additions & 10 deletions .github/workflows/build-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ on:
description: "Name of the AWS CDK stack"
required: false
type: string
roleArn:
description: "IAM role ARN assumed through GitHub OIDC for development (non-tag) CDK deployments. Required when runCdk is true."
required: false
default: ""
type: string
productionRoleArn:
description: "IAM role ARN assumed through GitHub OIDC for production (tag) CDK deployments. Required for tag runs when runCdk is true; there is no fallback to roleArn."
required: false
default: ""
type: string
awsRegion:
description: "AWS region for CDK deployments"
required: false
default: "us-east-1"
type: string
functions:
description: "JSON array of Lambda functions to build"
required: false
Expand All @@ -100,15 +115,28 @@ jobs:
if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
runs-on: ${{ inputs.lambdaRuntimeVersion == 'net11.0' && 'ubuntu-24.04-arm' || 'ubuntu-latest' }}
env:
outputPath: ${{ github.workspace }}/${{ inputs.outputPath }}
# Not named outputPath: MSBuild reads environment variables as properties, and an `outputPath`
# variable would redirect every project's OutputPath into this one folder (#45).
TEMPLATE_OUTPUT_PATH: ${{ github.workspace }}/${{ inputs.outputPath }}

steps:
# Validate the deployment role before building anything, and before requesting credentials.
- name: Resolve AWS deployment role
id: aws-role
if: inputs.runCdk == true
uses: $/.github/actions/resolve-aws-deploy-role
with:
roleArn: ${{ inputs.roleArn }}
productionRoleArn: ${{ inputs.productionRoleArn }}
awsRegion: ${{ inputs.awsRegion }}
ref: ${{ github.ref }}

- name: Set Default Output Path If Not Provided
run: |
if [ -z "${{ inputs.outputPath }}" ]; then
echo "outputPath=${{ github.workspace }}/publish" >> $GITHUB_ENV
echo "TEMPLATE_OUTPUT_PATH=${{ github.workspace }}/publish" >> $GITHUB_ENV
else
echo "outputPath=${{ github.workspace}}/${{ inputs.outputPath }}" >> $GITHUB_ENV
echo "TEMPLATE_OUTPUT_PATH=${{ github.workspace}}/${{ inputs.outputPath }}" >> $GITHUB_ENV
fi

- name: Compute Build Flags
Expand Down Expand Up @@ -171,7 +199,7 @@ jobs:
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
WASM_ENV="Production"
fi
dotnet publish ${{ inputs.webPath }} --configuration ${{ inputs.buildConfiguration }} --no-restore --output ${{ env.outputPath }} -p:WasmApplicationEnvironmentName=$WASM_ENV
dotnet publish ${{ inputs.webPath }} --configuration ${{ inputs.buildConfiguration }} --no-restore --output ${{ env.TEMPLATE_OUTPUT_PATH }} -p:WasmApplicationEnvironmentName=$WASM_ENV

- name: Detect Lambda custom build images
id: lambda-custom-container
Expand All @@ -197,7 +225,7 @@ jobs:
functions: ${{ inputs.functions }}
configuration: ${{ inputs.buildConfiguration }}
lambdaRuntimeVersion: ${{ inputs.lambdaRuntimeVersion }}
outputPath: ${{ env.outputPath }}
outputPath: ${{ env.TEMPLATE_OUTPUT_PATH }}

# Functions whose own aws-lambda-tools-defaults.json declares `container-image-for-build` were
# already built and packaged by the step above, from an isolated checkout copy - see that
Expand All @@ -216,7 +244,7 @@ jobs:
echo "Skipping $NAME - already packaged by the Native AOT isolated-build step"
continue
fi
ZIP_PATH="${{ env.outputPath }}/${NAME}.zip"
ZIP_PATH="${{ env.TEMPLATE_OUTPUT_PATH }}/${NAME}.zip"
dotnet tool run dotnet-lambda package -pl "$FUNC_PATH" --configuration ${{ inputs.buildConfiguration }} --no-restore --framework ${{ inputs.lambdaRuntimeVersion }} --output-package "$ZIP_PATH"
done

Expand Down Expand Up @@ -369,7 +397,7 @@ jobs:
exit 1
fi

SITE_OUT="${{ env.outputPath }}/$OUTPUT_SUBDIR"
SITE_OUT="${{ env.TEMPLATE_OUTPUT_PATH }}/$OUTPUT_SUBDIR"
rm -rf "$SITE_OUT"
mkdir -p "$SITE_OUT"
cp -a "$BUILD_OUT/." "$SITE_OUT/"
Expand Down Expand Up @@ -421,11 +449,12 @@ jobs:

- name: Configure AWS Credentials via OIDC
if: inputs.runCdk == true
uses: aws-actions/configure-aws-credentials@v6
uses: aws-actions/configure-aws-credentials@e1253824e5c10ff9df46874f81ed3ec929e19cfd # v6.3.0
with:
audience: sts.amazonaws.com
aws-region: us-east-1
role-to-assume: arn:aws:iam::403542860824:role/github-deploy-role
aws-region: ${{ inputs.awsRegion }}
role-to-assume: ${{ steps.aws-role.outputs.role-arn }}
mask-aws-account-id: true

- name: Run AWS CDK Deploy
if: inputs.runCdk == true
Expand Down
16 changes: 15 additions & 1 deletion .github/workflows/cdk-preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,21 @@ jobs:
STACKS: ${{ inputs.stacks }}
run: |
set -euo pipefail
[[ "$ROLE_ARN" =~ ^arn:aws:iam::[0-9]{12}:role/[A-Za-z0-9+=,.@_/-]{1,64}$ ]] || { echo "::error::roleArn is not an IAM role ARN."; exit 1; }
# IAM role ARN: arn:<partition>:iam::<account>:role/<optional path segments/><name>. The path
# (with its leading and trailing slash) is at most 512 printable non-space ASCII characters;
# the name is 1-64 characters of [A-Za-z0-9+=,.@_-].
is_role_arn() {
local LC_ALL=C rest name path
[[ "$1" =~ ^arn:aws(-cn|-us-gov)?:iam::[0-9]{12}:role/(.+)$ ]] || return 1
rest="${BASH_REMATCH[2]}"
name="${rest##*/}"
path="/"
[[ "$rest" == */* ]] && path="/${rest%/*}/"
[[ "$name" =~ ^[A-Za-z0-9+=,.@_-]{1,64}$ ]] || return 1
(( ${#path} <= 512 )) || return 1
[[ "$path" =~ ^[[:graph:]]+$ ]]
}
is_role_arn "$ROLE_ARN" || { echo "::error::roleArn is not an IAM role ARN (arn:aws:iam::<12-digit account>:role/[<path>/]<name>)."; exit 1; }
[[ "$AWS_REGION_INPUT" =~ ^[a-z]{2}(-[a-z]+)+-[0-9]$ ]] || { echo "::error::awsRegion is not an AWS region."; exit 1; }
jq -e 'type == "array" and length >= 1 and length <= 20 and (map(type == "object" and (.stack | type) == "string") | all) and (map(.stack) | unique | length) == length' <<<"$STACKS" >/dev/null \
|| { echo "::error::stacks must be a JSON array of 1-20 objects with distinct \"stack\" names."; exit 1; }
Expand Down
Loading
Loading