Skip to content

jzbahrai

jzbahrai #1254

name: Build, push, and deploy lambda image to staging
on:
workflow_dispatch:
push:
branches: [main]
env:
REGISTRY: ${{ secrets.STAGING_AWS_ACCOUNT_ID }}.dkr.ecr.ca-central-1.amazonaws.com/notify
ACCOUNT_ID: ${{ secrets.STAGING_AWS_ACCOUNT_ID }}
AWS_DEFAULT_REGION: ca-central-1
permissions:
id-token: write # This is required for requesting the OIDC JWT
contents: write # This is required for actions/checkout
pull-requests: write
jobs:
build-push-and-deploy:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
image: ["api-lambda"]
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Configure credentials to Notify using OIDC
uses: aws-actions/configure-aws-credentials@ececac1a45f3b08a01d2dd070d28d111c5fe6722 # v4.1.0
with:
role-to-assume: arn:aws:iam::${{env.ACCOUNT_ID}}:role/notification-api-apply
role-session-name: NotifyApiApply
aws-region: ${{ env.AWS_DEFAULT_REGION }}
- name: Login to ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@5a88a04c91d5c6f97aae0d9be790e64d9b1d47b7 # v1.7.1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build container
run: |
set -euo pipefail
DOCKERFILE="ci/Dockerfile.lambda"
docker buildx build \
--provenance=false \
--build-arg GIT_SHA=${GITHUB_SHA::7} \
-t $REGISTRY/${{ matrix.image }}:${GITHUB_SHA::7} \
-f ${DOCKERFILE} \
--output type=image,push=true \
.
- name: Logout of Amazon ECR
run: docker logout ${{ steps.login-ecr.outputs.registry }}
- name: Deploy lambda
run: |
aws lambda update-function-code \
--function-name ${{ matrix.image }} \
--image-uri $REGISTRY/${{ matrix.image }}:${GITHUB_SHA::7} > /dev/null 2>&1
- name: Publish lambda version and update alias
run: |
aws lambda wait function-updated --function-name ${{ matrix.image }}
VERSION="$(aws lambda publish-version --function-name ${{ matrix.image }} | jq -r '.Version')"
aws lambda update-alias \
--function-name ${{ matrix.image }} \
--name latest \
--function-version "$VERSION" > /dev/null 2>&1
- name: Notify Slack channel if this job failed
if: ${{ failure() }}
run: |
json='{"text":"<!here> CI is failing in <https://github.com/cds-snc/notification-api/actions/runs/'${{ github.run_id }}'|notification-api> !"}'
curl -X POST -H 'Content-type: application/json' --data "$json" ${{ secrets.SLACK_WEBHOOK }}