Skip to content

Backend Build and Deploy #90

Backend Build and Deploy

Backend Build and Deploy #90

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: |
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 >> $GITHUB_ENV
echo IMAGE_TAG=$(git rev-parse --short HEAD) >> $GITHUB_ENV
echo BUILD_SHA=$(git rev-parse --short HEAD) >> $GITHUB_ENV
fi
outputs:
AWS_APPENV: ${{ env.AWS_APPENV }}
IMAGE_TAG: ${{ env.IMAGE_TAG }}
BUILD_SHA: ${{ env.BUILD_SHA }}
build:
name: Build & Push Docker Image
runs-on: ubuntu-latest
permissions:
id-token: write # Needed for OIDC authentication to AWS
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@v3 # Sets AWS credentials for CLI
with:
role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy
role-session-name: incubator-cicd-vrms-gha # Session name for audit
aws-region: us-west-2 # 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: Debug Build Variables
env:
BUILD_SHA: ${{ needs.setup_env.outputs.BUILD_SHA }}
IMAGE_TAG: ${{ needs.setup_env.outputs.IMAGE_TAG }}
run: |
echo "=== Build Debug Information ==="
echo "BUILD_SHA: $BUILD_SHA"
echo "IMAGE_TAG: $IMAGE_TAG"
echo "DOCKERFILE: ${{ env.DOCKERFILE }}"
echo "DOCKER_PATH: ${{ env.DOCKER_PATH }}"
echo "================================"
- 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 }}
BUILD_SHA: ${{ needs.setup_env.outputs.BUILD_SHA }}
run: |
docker buildx build \
--platform linux/amd64 \
--cache-from type=gha \
--cache-to type=gha,mode=max \
--push \
--build-arg BUILD_SHA=$BUILD_SHA \
-f ${{ env.DOCKERFILE }} \
-t $ECR_REGISTRY/$ECR_REPOSITORY:${{ github.event.inputs.env }} \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
${{ env.DOCKER_PATH }}
deploy:
name: Deploy to AWS ECS
runs-on: ubuntu-latest
needs: [setup_env, build]
permissions:
id-token: write # Needed for OIDC authentication to AWS
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v3 # Sets AWS credentials for CLI
with:
role-to-assume: arn:aws:iam::035866691871:role/incubator-cicd-vrms # IAM role for deploy
role-session-name: incubator-cicd-vrms-gha # Session name for audit
aws-region: us-west-2 # AWS region
- name: Restart ECS Service
id: redeploy-service
env:
SERVICE_NAME: ${{env.AWS_APP_NAME}}-${{ github.event.inputs.env }} # ECS service name
# Force a new deployment of the ECS service to use the latest Docker image
run: |
aws ecs update-service --force-new-deployment --service $SERVICE_NAME --cluster $AWS_SHARED_CLUSTER