diff --git a/.github/workflows/deploy-lambda-function.yml b/.github/workflows/deploy-lambda-function.yml index 199035ac..20e1eae5 100644 --- a/.github/workflows/deploy-lambda-function.yml +++ b/.github/workflows/deploy-lambda-function.yml @@ -70,6 +70,18 @@ on: description: 'If true, a lambda version will be published' type: boolean default: true + version-description: + description: 'The description of the version being published (if publish-version is true)' + type: string + default: ${{ github.sha }} + update-alias: + description: 'If true, the alias will be updated to the new version' + type: boolean + default: false + alias-name: + description: 'The name of the alias to update. Required if update-alias is true' + type: string + default: '' # s3 options upload-to-s3: @@ -186,7 +198,7 @@ jobs: # deploy - name: 'Deploy' id: deploy - uses: shopsmart/github-actions/actions/deploy-lambda-function@v3 + uses: shopsmart/github-actions/actions/deploy-lambda-function@feature/lambda-update-alias with: zip-file: ${{ inputs.pattern }} function-name: ${{ inputs.function-name }} @@ -194,6 +206,9 @@ jobs: version=${{ inputs.version }} ${{ inputs.function-tags }} publish-version: ${{ inputs.publish-version }} + version-description: ${{ inputs.version-description || inputs.version }} + update-alias: ${{ inputs.update-alias }} + alias-name: ${{ inputs.alias-name }} upload-to-s3: ${{ inputs.upload-to-s3 }} s3-bucket: ${{ inputs.s3-bucket }} s3-key: ${{ inputs.s3-key }} diff --git a/actions/deploy-lambda-function/action.yml b/actions/deploy-lambda-function/action.yml index c87a79f3..de931a13 100644 --- a/actions/deploy-lambda-function/action.yml +++ b/actions/deploy-lambda-function/action.yml @@ -24,6 +24,15 @@ inputs: publish-version: description: If true, a lambda version will be published default: 'true' + version-description: + description: 'The description of the version being published (if publish-version is true)' + default: ${{ github.sha }} + update-alias: + description: 'If true, the alias will be updated to the new version. Also requires publish-version to be true' + default: 'false' + alias-name: + description: 'The name of the alias to update. Required if update-alias is true' + default: '' # s3 options upload-to-s3: @@ -74,4 +83,9 @@ runs: id: publish-version if: inputs.publish-version == 'true' shell: bash - run: ${{ github.action_path }}/version-lambda.sh "${{ inputs.function-name }}" "${{ inputs.tag || github.sha }}" + run: ${{ github.action_path }}/version-lambda.sh "${{ inputs.function-name }}" "${{ inputs.version-description || github.sha }}" + + - name: 'Update the alias' + if: inputs.update-alias == 'true' && inputs.publish-version == 'true' + shell: bash + run: ${{ github.action_path }}/update-alias.sh "${{ inputs.function-name }}" "${{ inputs.alias-name }}" "${{ steps.publish-version.outputs.version }}" diff --git a/actions/deploy-lambda-function/update-alias.bats b/actions/deploy-lambda-function/update-alias.bats new file mode 100644 index 00000000..563c9482 --- /dev/null +++ b/actions/deploy-lambda-function/update-alias.bats @@ -0,0 +1,44 @@ +#!/usr/bin/env bats + +load update-alias.sh + +function setup() { + export AWS_CMD_FILE="$BATS_TEST_TMPDIR/aws.cmd" + export -f aws + + export GITHUB_OUTPUT="$BATS_TEST_TMPDIR/output.txt" +} + +function teardown() { + rm -f "$AWS_CMD_FILE" +} + +function aws() { + echo "$*" > "$AWS_CMD_FILE" +} + +@test "it should require the function name" { + run update-alias + + [ "$status" -ne 0 ] +} + +@test "it should require the alias name" { + run update-alias my-function + + [ "$status" -ne 0 ] +} + +@test "it should require the version" { + run update-alias my-function my-alias + + [ "$status" -ne 0 ] +} + +@test "it should update the alias" { + run update-alias my-function my-alias 1 + + [ "$status" -eq 0 ] + [ -f "$AWS_CMD_FILE" ] + [ "$(< "$AWS_CMD_FILE")" = "lambda update-alias --function-name my-function --name my-alias --function-version 1 --no-cli-pager" ] +} diff --git a/actions/deploy-lambda-function/update-alias.sh b/actions/deploy-lambda-function/update-alias.sh new file mode 100755 index 00000000..f4f01b79 --- /dev/null +++ b/actions/deploy-lambda-function/update-alias.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +function update-alias() { + set -eo pipefail + + local function_name="${1:-}" + [ -n "$function_name" ] || { + echo "[ERROR] Function name not provided" >&2 + return 1 + } + + local alias_name="${2:-}" + [ -n "$alias_name" ] || { + echo "[ERROR] Alias name not provided" >&2 + return 2 + } + + local version="${3:-}" + [ -n "$version" ] || { + echo "[ERROR] Version not provided" >&2 + return 2 + } + + aws lambda update-alias \ + --function-name "$function_name" \ + --name "$alias_name" \ + --function-version "$version" \ + --no-cli-pager +} + +if [ "${BASH_SOURCE[0]}" = "$0" ]; then + set -u + + update-alias "${@:-}" + exit $? +fi