lambda: add .nvmrc #71
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "image-uri=$IMAGE_URI" >> $GITHUB_OUTPUT | |
- 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 }} |