Backend Build and Deploy #76
Workflow file for this run
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: Backend Build and Deploy | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| env: | |
| type: choice | |
| description: "AWS Incubator Env" | |
| options: | |
| - dev | |
| - prod | |
| ref: | |
| description: "Branch, Tag, or SHA" | |
| required: true | |
| env: | |
| AWS_SHARED_CLUSTER: incubator-prod | |
| AWS_APP_NAME: vrms-backend | |
| AWS_REGION: us-west-2 | |
| DOCKERFILE: backend/Dockerfile.prod | |
| DOCKER_PATH: backend | |
| jobs: | |
| setup_env: | |
| name: Set-up environment | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Debug Action | |
| uses: hmarr/[email protected] | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.inputs.ref }} | |
| - name: Set AWS Env & Image Tag per workflow | |
| run: | | |
| SHORT_SHA=$(git rev-parse --short HEAD) | |
| if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then | |
| INPUT_ENV=${{ github.event.inputs.env }}; INPUT_REF=${{ github.event.inputs.ref }} | |
| echo AWS_APPENV="$AWS_APP_NAME"-$INPUT_ENV >> $GITHUB_ENV | |
| echo IMAGE_TAG=$SHORT_SHA >> $GITHUB_ENV | |
| fi | |
| outputs: | |
| AWS_APPENV: ${{ env.AWS_APPENV }} | |
| IMAGE_TAG: ${{ env.IMAGE_TAG }} | |
| build: | |
| name: Build & Push Docker Image | |
| runs-on: ubuntu-latest | |
| needs: [setup_env] | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.inputs.ref }} | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build & Push Image to ECR | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| ECR_REPOSITORY: ${{ needs.setup_env.outputs.AWS_APPENV }} | |
| IMAGE_TAG: ${{ needs.setup_env.outputs.IMAGE_TAG }} | |
| run: | | |
| docker buildx build \ | |
| --platform linux/amd64 \ | |
| --cache-from type=gha \ | |
| --cache-to type=gha,mode=max \ | |
| --push \ | |
| -f ${{ env.DOCKERFILE }} \ | |
| -t $ECR_REGISTRY/$ECR_REPOSITORY:latest \ | |
| -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \ | |
| ${{ env.DOCKER_PATH }} | |
| deploy: | |
| name: Deploy to AWS ECS | |
| runs-on: ubuntu-latest | |
| needs: [setup_env, build] | |
| steps: | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.INCUBATOR_AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.INCUBATOR_AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| - name: Pull Task Definition & write to file | |
| id: aws-task-definition | |
| run: | | |
| aws ecs describe-task-definition \ | |
| --task-definition ${{ needs.setup_env.outputs.AWS_APPENV }} \ | |
| --query taskDefinition | \ | |
| jq 'del(.taskDefinitionArn,.revision,.status,.registeredBy,.registeredAt,.compatibilities,.requiresAttributes)' > task-def.json | |
| - name: Interpolate new Docker Image into Task Definition | |
| id: task-definition | |
| uses: aws-actions/amazon-ecs-render-task-definition@v1 | |
| with: | |
| task-definition: task-def.json | |
| container-name: ${{ needs.setup_env.outputs.AWS_APPENV }} | |
| image: ${{ steps.login-ecr.outputs.registry }}/${{ needs.setup_env.outputs.AWS_APPENV }}:${{ needs.setup_env.outputs.IMAGE_TAG }} | |
| - name: Deploy Amazon ECS | |
| uses: aws-actions/amazon-ecs-deploy-task-definition@v1 | |
| with: | |
| task-definition: ${{ steps.task-definition.outputs.task-definition }} | |
| service: ${{ needs.setup_env.outputs.AWS_APPENV }} | |
| cluster: ${{ env.AWS_SHARED_CLUSTER }} | |
| wait-for-service-stability: true | |
| wait-for-minutes: 5 |