-
Notifications
You must be signed in to change notification settings - Fork 0
67 lines (58 loc) · 2.43 KB
/
cd-lambda-function.yml
File metadata and controls
67 lines (58 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
name: Deploy lambda function to AWS Lambda
on:
push:
branches:
- master
paths: # this workflow is executed if there is at least one change in
- 'packages/lambda/**'
env:
AWS_REGION: us-east-1
AWS_ECR_REPOSITORY_NAME: rtv-ecr-repo
AWS_LAMBDA_FUNCTION_NAME: rtv-lambda
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
defaults:
run: # provide default options for all run steps in workflow
shell: bash
working-directory: packages/lambda
jobs:
deploy:
name: Deploy lambda function
runs-on: ubuntu-latest
steps: # each step is isolated from other
- name: Checkout
uses: actions/checkout@v2
- name: Authenticate docker client to ECR registry
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1 # equivalent to execute: aws ecr get-login-password --region <AWS_REGION> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com
# - name: Delete the previous images of the ECR repository, if exists
# run: |
# IMAGES_TO_DELETE=$( \
# aws ecr list-images \
# --region $AWS_REGION \
# --repository-name $AWS_ECR_REPOSITORY_NAME \
# --query 'imageIds[*]' \
# --output json \
# )
# aws ecr batch-delete-image \
# --region $AWS_REGION \
# --repository-name $AWS_ECR_REPOSITORY_NAME \
# --image-ids "$IMAGES_TO_DELETE" \
# || true
- name: Build, tag, and push docker image to ECR repository
id: build-image
env:
ECR_REGISTRY_URI: ${{ steps.login-ecr.outputs.registry }} # seems like: <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com
TAG: ${{ github.sha }}
# build the docker image already with a tag to the ECR repository, and push the image to ECR repository
run: |
IMAGE_URI="$ECR_REGISTRY_URI/$AWS_ECR_REPOSITORY_NAME:$TAG"
docker build --tag $IMAGE_URI .
docker push $IMAGE_URI
echo "::set-output name=image-uri::$IMAGE_URI"
- name: Update container image of the AWS Lambda
run: |
aws configure set region ${{ env.AWS_REGION }}
aws lambda update-function-code \
--function-name ${{ env.AWS_LAMBDA_FUNCTION_NAME }} \
--image-uri ${{ steps.build-image.outputs.image-uri }}