|
| 1 | +on: |
| 2 | + push: |
| 3 | + paths: |
| 4 | + - runtime.R |
| 5 | + - Dockerfile |
| 6 | + |
| 7 | +name: Build and Deploy Lambda |
| 8 | + |
| 9 | +jobs: |
| 10 | + container: |
| 11 | + name: Deploy container to ECR |
| 12 | + runs-on: ubuntu-latest |
| 13 | + env: |
| 14 | + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} |
| 15 | + ECR_REPOSITORY: hfsubset |
| 16 | + IMAGE_TAG: ${{ github.sha }} |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v3 |
| 20 | + |
| 21 | + - name: Configure AWS Credentials |
| 22 | + id: aws-credentials |
| 23 | + uses: aws-actions/configure-aws-credentials@v2 |
| 24 | + with: |
| 25 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 26 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 27 | + aws-region: ${{ secrets.AWS_DEFAULT_REGION }} |
| 28 | + |
| 29 | + - name: Login to AWS ECR |
| 30 | + id: login-ecr |
| 31 | + uses: aws-actions/amazon-ecr-login@v1 |
| 32 | + |
| 33 | + - name: Verify ECR Repository |
| 34 | + env: |
| 35 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 36 | + run: | |
| 37 | + (aws ecr describe-repositories --repository-names ${ECR_REPOSITORY} \ |
| 38 | + && docker pull ${ECR_REGISTRY}/${ECR_REPOSITORY}:latest) \ |
| 39 | + || aws ecr create-repository --repository-name ${ECR_REPOSITORY} |
| 40 | +
|
| 41 | + - name: Build and tag image |
| 42 | + env: |
| 43 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 44 | + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 45 | + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 46 | + run: | |
| 47 | + docker build \ |
| 48 | + --cache-from "${ECR_REGISTRY}/${ECR_REPOSITORY}:latest" \ |
| 49 | + --tag="${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG}" \ |
| 50 | + --tag="${ECR_REGISTRY}/${ECR_REPOSITORY}:latest" . |
| 51 | +
|
| 52 | + - name: Push image to ECR |
| 53 | + env: |
| 54 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 55 | + run: docker push ${ECR_REGISTRY}/${ECR_REPOSITORY} --all-tags |
| 56 | + |
| 57 | + - name: Check Lambda function |
| 58 | + id: lambda-function |
| 59 | + continue-on-error: true |
| 60 | + env: |
| 61 | + LAMBDA_FUNCTION: hfsubset |
| 62 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 63 | + run: aws lambda wait function-exists --function-name ${LAMBDA_FUNCTION} > /dev/null 2>&1 |
| 64 | + |
| 65 | + - name: Create Lambda function |
| 66 | + if: steps.lambda-function.outcome == 'failure' |
| 67 | + env: |
| 68 | + LAMBDA_FUNCTION: hfsubset |
| 69 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 70 | + AWS_ACCOUNT_ID: ${{ steps.aws-credentials.outputs.aws-account-id }} |
| 71 | + run: | |
| 72 | + aws iam create-role \ |
| 73 | + --role-name ${LAMBDA_FUNCTION}-role \ |
| 74 | + --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' |
| 75 | +
|
| 76 | + aws iam attach-role-policy \ |
| 77 | + --role-name ${LAMBDA_FUNCTION}-role \ |
| 78 | + --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole |
| 79 | +
|
| 80 | + aws lambda create-function \ |
| 81 | + --function-name ${LAMBDA_FUNCTION} \ |
| 82 | + --role "arn:aws:iam:${AWS_ACCOUNT_ID}:role/${LAMBDA_FUNCTION}-role" \ |
| 83 | + --memory-size 3008 \ |
| 84 | + --code "ImageUri=${ECR_REGISTRY}/${ECR_REPOSTIORY}:${IMAGE_TAG}" \ |
| 85 | + --ephemeral-storage 2048 |
| 86 | +
|
| 87 | + # TODO: put this behind an API Gateway? |
| 88 | + output=$(aws lambda create-function-url-config --function-name ${LAMBDA_FUNCTION} --auth-type NONE) |
| 89 | +
|
| 90 | + echo ":rocket: lambda function running at $(echo $output | jq .FunctionUrl)" >> $GITHUB_STEP_SUMMARY |
| 91 | +
|
| 92 | + - name: Update Lambda function |
| 93 | + if: steps.lambda-function.outcome == 'success' |
| 94 | + env: |
| 95 | + LAMBDA_FUNCTION: hfsubset |
| 96 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 97 | + run: | |
| 98 | + aws lambda update-function-code \ |
| 99 | + --function-name ${LAMBDA_FUNCTION} \ |
| 100 | + --image-uri "${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG}" |
0 commit comments