Skip to content

container_lambda

container_lambda #148

name: container_lambda
on:
push:
branches:
- main
paths:
# Build when there are changes in the directory that holds the component,
# or when this workflow file is changed
- 'container_lambda/**'
- '.github/workflows/container_lambda.yml'
schedule:
# A weekly build to pick up updates to the base container image
# A weekday at mid-day - when someone is likely to be working
- cron: "0 12 * * 2"
# Based on: https://github.com/aws-actions/amazon-ecr-login
jobs:
deploy:
name: Deploy Lambda container image
runs-on: ubuntu-latest
env:
component: container_lambda
ecrRepoName: ${{ secrets.ECR_CONTAINER_LAMBDA }}
lambdaName: ${{ secrets.CONTAINER_LAMBDA }}
steps:
- uses: actions/checkout@v3
- name: AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-2
- name: Login to ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ecrRepoName }}
TAG: latest
run: |
# For simplicity, we're only building "latest" on the basis that we can
# fix-forward by rolling back to the state of an earlier commit in the repo:
COMMIT_HASH=$(git rev-parse HEAD)
docker build --build-arg COMMIT_HASH=$COMMIT_HASH -t $ECR_REGISTRY/$ECR_REPOSITORY:$TAG ./${{ env.component }}
- name: Integration test using Docker Compose
env:
ACTIVECAMPAIGN_URL: ${{ secrets.ACTIVECAMPAIGN_URL }}
ACTIVECAMPAIGN_KEY: ${{ secrets.ACTIVECAMPAIGN_KEY }}
run: |
docker-compose up -d
docker-compose exec -T integration-test npm run test
- name: Push image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ecrRepoName }}
TAG: latest
run: |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$TAG
- name: Update Lambda function
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ecrRepoName }}
TAG: latest
run: |
# NB Lambdas that run from containers resolve to an image hash, rather than "name:tag",
# Pushing to the same tag doesn't automatically update the image.
# Therefore we need to explicitly update the lambda code so it will point to the new hash referenced by "nome:tag":
aws lambda update-function-code --function-name=${{ env.lambdaName }} --image-uri=$ECR_REGISTRY/$ECR_REPOSITORY:$TAG
- name: Notify Slack
if: failure()
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_USERNAME: ${{ env.component }} build
SLACK_COLOR: ${{ job.status }}
SLACK_ICON_EMOJI: ":github_octocat:"
SLACK_FOOTER: Github Actions
SLACK_CHANNEL: build