|
| 1 | +name: Deploy Lambda Service |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ master, main ] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v3 |
| 15 | + |
| 16 | + - name: Set up Docker Buildx |
| 17 | + uses: docker/setup-buildx-action@v2 |
| 18 | + |
| 19 | + - name: Configure AWS credentials |
| 20 | + uses: aws-actions/configure-aws-credentials@v1 |
| 21 | + with: |
| 22 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 23 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 24 | + aws-region: ${{ secrets.AWS_REGION }} |
| 25 | + |
| 26 | + - name: Login to Amazon ECR |
| 27 | + id: login-ecr |
| 28 | + uses: aws-actions/amazon-ecr-login@v1 |
| 29 | + |
| 30 | + # Set ECR image URI as environment variables |
| 31 | + - name: Set ECR image URIs |
| 32 | + id: set-image-uris |
| 33 | + run: | |
| 34 | + ECR_REGISTRY="${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com" |
| 35 | + ECR_REPOSITORY="${{ secrets.ECR_REPOSITORY }}" |
| 36 | + IMAGE_TAG_LATEST="$ECR_REGISTRY/$ECR_REPOSITORY:latest" |
| 37 | + IMAGE_TAG_SHA="$ECR_REGISTRY/$ECR_REPOSITORY:${{ github.sha }}" |
| 38 | + echo "image_uri_latest=$IMAGE_TAG_LATEST" >> $GITHUB_OUTPUT |
| 39 | + echo "image_uri_sha=$IMAGE_TAG_SHA" >> $GITHUB_OUTPUT |
| 40 | + echo "image_tags=$IMAGE_TAG_SHA,$IMAGE_TAG_LATEST" >> $GITHUB_OUTPUT |
| 41 | + |
| 42 | + - name: Build and push Docker image |
| 43 | + uses: docker/build-push-action@v4 |
| 44 | + with: |
| 45 | + context: . |
| 46 | + file: ./Dockerfile.lambda |
| 47 | + platforms: linux/arm64 |
| 48 | + push: true |
| 49 | + tags: ${{ steps.set-image-uris.outputs.image_tags }} |
| 50 | + |
| 51 | + - name: Update Lambda function |
| 52 | + run: | |
| 53 | + aws lambda update-function-code \ |
| 54 | + --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \ |
| 55 | + --image-uri ${{ steps.set-image-uris.outputs.image_uri_latest }} \ |
| 56 | + --region ${{ secrets.AWS_REGION }} |
| 57 | + |
| 58 | + - name: Wait for Lambda update |
| 59 | + run: | |
| 60 | + aws lambda wait function-updated \ |
| 61 | + --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \ |
| 62 | + --region ${{ secrets.AWS_REGION }} |
| 63 | + |
| 64 | + - name: Test Lambda function |
| 65 | + run: | |
| 66 | + echo "Lambda function updated successfully!" |
| 67 | + echo "Image deployed: ${{ steps.set-image-uris.outputs.image_uri_latest }}" |
| 68 | + echo "Function URL: $(aws lambda get-function-url-config --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} --query 'FunctionUrl' --output text --region ${{ secrets.AWS_REGION }} || echo 'No function URL configured')" |
0 commit comments