From 7a3d3eccc4b6a6c3b311e73f79e1046f9f2a67f4 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:22:10 -0600 Subject: [PATCH 1/7] Allows the version description to be configured for lambda --- .github/workflows/deploy-lambda-function.yml | 5 +++++ actions/deploy-lambda-function/action.yml | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-lambda-function.yml b/.github/workflows/deploy-lambda-function.yml index 199035ac..a60f5cf2 100644 --- a/.github/workflows/deploy-lambda-function.yml +++ b/.github/workflows/deploy-lambda-function.yml @@ -70,6 +70,10 @@ 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 }} # s3 options upload-to-s3: @@ -194,6 +198,7 @@ jobs: version=${{ inputs.version }} ${{ inputs.function-tags }} publish-version: ${{ inputs.publish-version }} + version-description: ${{ inputs.version }} 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..dc3717e5 100644 --- a/actions/deploy-lambda-function/action.yml +++ b/actions/deploy-lambda-function/action.yml @@ -24,6 +24,9 @@ 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 }} # s3 options upload-to-s3: @@ -74,4 +77,4 @@ 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-tag || github.sha }}" From 6b8a43c2c07d10f04d67f0daa7d4a7eda697dc03 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:42:15 -0600 Subject: [PATCH 2/7] Adds updating lambda alias as part of lambda deploy --- .github/workflows/deploy-lambda-function.yml | 12 ++++- actions/deploy-lambda-function/action.yml | 11 +++++ .../deploy-lambda-function/update-alias.bats | 44 +++++++++++++++++++ .../deploy-lambda-function/update-alias.sh | 36 +++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 actions/deploy-lambda-function/update-alias.bats create mode 100644 actions/deploy-lambda-function/update-alias.sh diff --git a/.github/workflows/deploy-lambda-function.yml b/.github/workflows/deploy-lambda-function.yml index a60f5cf2..8aa19df2 100644 --- a/.github/workflows/deploy-lambda-function.yml +++ b/.github/workflows/deploy-lambda-function.yml @@ -74,6 +74,14 @@ on: 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: @@ -190,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 }} @@ -199,6 +207,8 @@ jobs: ${{ inputs.function-tags }} publish-version: ${{ inputs.publish-version }} 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 dc3717e5..d79dd406 100644 --- a/actions/deploy-lambda-function/action.yml +++ b/actions/deploy-lambda-function/action.yml @@ -27,6 +27,12 @@ inputs: 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' + default: 'false' + alias-name: + description: 'The name of the alias to update. Required if update-alias is true' + default: '' # s3 options upload-to-s3: @@ -78,3 +84,8 @@ runs: if: inputs.publish-version == 'true' shell: bash run: ${{ github.action_path }}/version-lambda.sh "${{ inputs.function-name }}" "${{ inputs.version-tag || github.sha }}" + + - name: 'Update the alias' + if: inputs.update-alias == 'true' + shell: bash + run: ${{ github.action_path }}/alias-lambda.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 100644 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 From 170219cec72722d46b47b8bc446957d126c8dab0 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:50:51 -0600 Subject: [PATCH 3/7] Updates permissions and corrects filenam,e --- actions/deploy-lambda-function/action.yml | 2 +- actions/deploy-lambda-function/update-alias.sh | 0 2 files changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 actions/deploy-lambda-function/update-alias.sh diff --git a/actions/deploy-lambda-function/action.yml b/actions/deploy-lambda-function/action.yml index d79dd406..48118d74 100644 --- a/actions/deploy-lambda-function/action.yml +++ b/actions/deploy-lambda-function/action.yml @@ -88,4 +88,4 @@ runs: - name: 'Update the alias' if: inputs.update-alias == 'true' shell: bash - run: ${{ github.action_path }}/alias-lambda.sh "${{ inputs.function-name }}" "${{ inputs.alias-name }}" "${{ steps.publish-version.outputs.version }}" + 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.sh b/actions/deploy-lambda-function/update-alias.sh old mode 100644 new mode 100755 From 3276f3a319af419b5a38216925ce3382c0604ffe Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:57:32 -0600 Subject: [PATCH 4/7] Actually pass the version description --- .github/workflows/deploy-lambda-function.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-lambda-function.yml b/.github/workflows/deploy-lambda-function.yml index 8aa19df2..eacbd018 100644 --- a/.github/workflows/deploy-lambda-function.yml +++ b/.github/workflows/deploy-lambda-function.yml @@ -206,7 +206,7 @@ jobs: version=${{ inputs.version }} ${{ inputs.function-tags }} publish-version: ${{ inputs.publish-version }} - version-description: ${{ inputs.version }} + version-description: ${{ inputs.version-description }} update-alias: ${{ inputs.update-alias }} alias-name: ${{ inputs.alias-name }} upload-to-s3: ${{ inputs.upload-to-s3 }} From 70519eff518b4427c879d06d658098846a82ee29 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:57:47 -0600 Subject: [PATCH 5/7] Default version description to inputs.version --- .github/workflows/deploy-lambda-function.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/deploy-lambda-function.yml b/.github/workflows/deploy-lambda-function.yml index eacbd018..20e1eae5 100644 --- a/.github/workflows/deploy-lambda-function.yml +++ b/.github/workflows/deploy-lambda-function.yml @@ -206,7 +206,7 @@ jobs: version=${{ inputs.version }} ${{ inputs.function-tags }} publish-version: ${{ inputs.publish-version }} - version-description: ${{ inputs.version-description }} + version-description: ${{ inputs.version-description || inputs.version }} update-alias: ${{ inputs.update-alias }} alias-name: ${{ inputs.alias-name }} upload-to-s3: ${{ inputs.upload-to-s3 }} From a31a691e188dab588b09f1bbd9193a634301a282 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:59:45 -0600 Subject: [PATCH 6/7] Require publish-version to be true if updating an alias --- actions/deploy-lambda-function/action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actions/deploy-lambda-function/action.yml b/actions/deploy-lambda-function/action.yml index 48118d74..7a471f42 100644 --- a/actions/deploy-lambda-function/action.yml +++ b/actions/deploy-lambda-function/action.yml @@ -28,7 +28,7 @@ inputs: 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' + 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' @@ -86,6 +86,6 @@ runs: run: ${{ github.action_path }}/version-lambda.sh "${{ inputs.function-name }}" "${{ inputs.version-tag || github.sha }}" - name: 'Update the alias' - if: inputs.update-alias == 'true' + 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 }}" From b1e15d6ac6221b1fe9334e77b75c5c5c11d16358 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:10:16 -0600 Subject: [PATCH 7/7] Corrects variable references --- actions/deploy-lambda-function/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actions/deploy-lambda-function/action.yml b/actions/deploy-lambda-function/action.yml index 7a471f42..de931a13 100644 --- a/actions/deploy-lambda-function/action.yml +++ b/actions/deploy-lambda-function/action.yml @@ -83,7 +83,7 @@ runs: id: publish-version if: inputs.publish-version == 'true' shell: bash - run: ${{ github.action_path }}/version-lambda.sh "${{ inputs.function-name }}" "${{ inputs.version-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'