Merge pull request #359 from gitautoai/wes #435
This file contains 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: Deployment to AWS Lambda with Docker | |
on: | |
push: | |
branches: | |
- '*' | |
# Global environment variables | |
env: | |
AWS_REGION: us-west-1 | |
jobs: | |
deploy_lambda: | |
name: Publish and Deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 # https://github.com/actions/checkout | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 # https://github.com/aws-actions/configure-aws-credentials | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ env.AWS_REGION }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 # https://github.com/aws-actions/amazon-ecr-login | |
# LAMBDA_ARN is only used in production | |
- name: Set deployment environment variables | |
run: | | |
{ | |
echo "ECR_REGISTRY=${{ steps.login-ecr.outputs.registry }}" | |
echo "EMAIL=${{ secrets.EMAIL }}" | |
echo "IMAGE_TAG=${{ github.sha }}" | |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then | |
echo "ECR_REPOSITORY=${{ secrets.PROD_LAMBDA_NAME }}" | |
echo "LAMBDA_NAME=${{ secrets.PROD_LAMBDA_NAME }}" | |
echo "LAMBDA_ARN=arn:aws:lambda:${{ env.AWS_REGION }}:${{ secrets.AWS_ACCOUNT_ID }}:function:${{ secrets.PROD_LAMBDA_NAME }}" | |
echo "SLACK_WEBHOOK_URL=${{ secrets.SLACK_WEBHOOK_URL_FOR_PRD }}" | |
else | |
echo "ECR_REPOSITORY=${{ secrets.STAGE_LAMBDA_NAME }}" | |
echo "LAMBDA_NAME=${{ secrets.STAGE_LAMBDA_NAME }}" | |
echo "SLACK_WEBHOOK_URL=${{ secrets.SLACK_WEBHOOK_URL_FOR_STG }}" | |
fi | |
} >> $GITHUB_ENV | |
- name: Build, tag, and push image to Amazon ECR | |
id: build-image | |
run: | | |
docker build -f ./Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
- name: Update Lambda function with latest ECR image | |
run: | | |
aws lambda update-function-code \ | |
--function-name $LAMBDA_NAME \ | |
--image-uri $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
# Create or update the CloudFormation stack only for the main branch | |
- name: Deploy CloudFormation stack for scheduled event | |
if: github.ref == 'refs/heads/main' | |
run: | | |
aws cloudformation deploy \ | |
--stack-name ScheduleStack \ | |
--template-file cloudformation.yml \ | |
--parameter-overrides LambdaFunctionName=$LAMBDA_NAME LambdaFunctionArn=$LAMBDA_ARN Email=$EMAIL \ | |
--capabilities CAPABILITY_NAMED_IAM | |
- name: Notify Slack of deployment status | |
# Execute this step even if the previous steps fail | |
if: always() | |
env: | |
SLACK_MESSAGE: ${{ job.status == 'success' && 'Deployment successful' || 'Deployment failed' }} | |
run: | | |
curl -X POST -H 'Content-type: application/json' --data '{"msg":"'"${SLACK_MESSAGE}"'"}' $SLACK_WEBHOOK_URL |