Skip to content

Deploy Lambda Functions #72

Deploy Lambda Functions

Deploy Lambda Functions #72

name: Deploy Lambda Functions
on:
push:
branches:
- master
paths:
- "aws/**"
workflow_dispatch:
inputs:
environment:
description: "Select target environment"
required: true
default: "dev"
type: choice
options:
- dev
- test
- prod
env:
WORKING_DIRECTORY: ./aws
NODE_VERSION: 20
GITHUB_IMAGE_REPO: ghcr.io/bcgov/jasper
jobs:
get-lambdas:
runs-on: ubuntu-latest
outputs:
lambda_dir_list: ${{ steps.convert.outputs.LAMBDA_DIR_LIST }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Get Lambda directories
id: lambdas
shell: bash
working-directory: ${{ env.WORKING_DIRECTORY }}
run: |
dirs=$(find lambdas -mindepth 2 -maxdepth 2 -type d | sed 's|lambdas/||' | paste -sd ';' -) # Space-separated
echo "LAMBDA_DIRS=$dirs" >> $GITHUB_ENV
- name: Convert FOLDERS to JSON array
id: convert
shell: bash
working-directory: ${{ env.WORKING_DIRECTORY }}
run: |
LAMBDA_DIR_LIST=$(echo "${LAMBDA_DIRS}" | jq -R 'split(";")' -c)
echo "LAMBDA_DIR_LIST=$LAMBDA_DIR_LIST" >> $GITHUB_OUTPUT
deploy2gchr:
needs: get-lambdas
environment: ${{ inputs.environment }}
outputs:
short_sha: ${{ steps.short_sha.outputs.SHORT_SHA }}
permissions:
id-token: write
packages: write
runs-on: ubuntu-latest
env:
LAMBDA_ECR_REPO_URL: ${{ vars.AWS_ACCOUNT }}.dkr.ecr.${{ vars.AWS_REGION }}.amazonaws.com/${{ vars.APP_NAME }}-lambda-repo-${{ vars.ENVIRONMENT_NAME }}
strategy:
matrix:
lambda: ${{ fromJSON(needs.get-lambdas.outputs.lambda_dir_list) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Build Lambdas codebase
uses: ./.github/workflows/actions/build-lambdas
with:
working_directory: ${{ env.WORKING_DIRECTORY }}
node_version: ${{ env.NODE_VERSION }}
- name: Log in to the GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get short SHA
id: short_sha
run: |
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Parse Resource and Lambda Name
id: parse
run: |
echo "Lambda: ${{ matrix.lambda }}"
RESOURCE=$(echo "${{ matrix.lambda }}" | cut -d'/' -f1)
LAMBDA=$(echo "${{ matrix.lambda }}" | cut -d'/' -f2)
echo "RESOURCE=$RESOURCE" >> $GITHUB_ENV
echo "LAMBDA=$LAMBDA" >> $GITHUB_ENV
- name: Setup Image Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.GITHUB_IMAGE_REPO }}/${{ env.RESOURCE }}.${{ env.LAMBDA }}
tags: |
type=raw,value=${{ steps.short_sha.outputs.SHORT_SHA }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver: docker
- name: Build ${{ matrix.lambda }} image
uses: docker/build-push-action@v6
with:
push: true
file: ./docker/aws/Dockerfile.release
context: ./aws
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
TARGET_FUNCTION=${{ matrix.lambda }}
NODE_VERSION=${{ env.NODE_VERSION }}
deploy2dev:
name: Deploy to DEV
needs: [get-lambdas, deploy2gchr]
env:
ENVIRONMENT: dev
permissions:
id-token: write
packages: write
runs-on: ubuntu-latest
environment: dev
strategy:
matrix:
lambda: ${{ fromJSON(needs.get-lambdas.outputs.lambda_dir_list) }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Parse Resource and Lambda Name
id: parse
run: |
echo "Lambda: ${{ matrix.lambda }}"
RESOURCE=$(echo "${{ matrix.lambda }}" | cut -d'/' -f1)
LAMBDA=$(echo "${{ matrix.lambda }}" | cut -d'/' -f2)
echo "RESOURCE=$RESOURCE" >> $GITHUB_ENV
echo "LAMBDA=$LAMBDA" >> $GITHUB_ENV
- name: Deploy to ${{ env.ENVIRONMENT }}
uses: ./.github/workflows/actions/deploy-lambda
with:
environment: ${{ env.ENVIRONMENT }}
aws_account: ${{ vars.AWS_ACCOUNT }}
region: ${{ vars.AWS_REGION }}
app_name: ${{ vars.APP_NAME }}
aws_role_arn: ${{ vars.AWS_ROLE_ARN }}
ghcr_token: ${{ secrets.GITHUB_TOKEN }}
github_image_repo: ${{ env.GITHUB_IMAGE_REPO }}
lambda_name: ${{ env.LAMBDA }}
image_name: ${{ env.RESOURCE }}.${{ env.LAMBDA }}
short_sha: ${{ needs.deploy2gchr.outputs.short_sha }}