Skip to content

Variable passing fix #20

Variable passing fix

Variable passing fix #20

Workflow file for this run

name: Build and Deploy Backend and Frontend
on:
push:
branches:
- main
permissions:
contents: read
env:
CLUSTER_NAME: calendar-app-v2
jobs:
build-backend:
name: Build backend and push to ECR
runs-on: ubuntu-latest
environment: build-backend
permissions:
id-token: write
contents: read
outputs:
image: ${{ steps.export-image.outputs.image }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE }}
- name: Log in 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: Define image URI
id: define-image-uri
run: echo "IMAGE_URI=${{ steps.login-ecr.outputs.registry }}/calendar-app-backend:${{ github.sha }}" >> $GITHUB_ENV
- name: Build and push using Buildx
uses: docker/build-push-action@v6
with:
context: ./backend
push: true
tags: ${{ env.IMAGE_URI }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Export image URI
id: export-image
run: echo "image=${{ env.IMAGE_URI }}" >> $GITHUB_OUTPUT
build-frontend:
name: Build frontend and push to ECR
runs-on: ubuntu-latest
environment: build-frontend
permissions:
id-token: write
contents: read
outputs:
image: ${{ steps.build-image.outputs.image }}
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE }}
- name: Log in to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build, tag, and push frontend Docker image
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: calendar-app-frontend
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./frontend
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
deploy-backend:
name: Deploy Backend to ECS
runs-on: ubuntu-latest
environment: deploy-backend
needs: build-backend
permissions:
id-token: write
contents: read
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE }}
- name: Download backend task definition
run: |
aws ecs describe-task-definition --task-definition calendar-app-backend --query taskDefinition > backend-task-def.json
- name: Render Amazon ECS backend task definition
id: render-backend
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: backend-task-def.json
container-name: backend
image: ${{ needs.build-backend.outputs.image }}
- name: Deploy Amazon ECS backend task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-backend.outputs.task-definition }}
service: backend-service
cluster: ${{ env.CLUSTER_NAME }}
wait-for-service-stability: true
deploy-frontend:
name: Deploy Frontend to ECS
runs-on: ubuntu-latest
environment: deploy-frontend
needs: build-frontend
permissions:
id-token: write
contents: read
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
audience: sts.amazonaws.com
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE }}
- name: Download frontend task definition
run: |
aws ecs describe-task-definition --task-definition calendar-app-frontend --query taskDefinition > frontend-task-def.json
- name: Render Amazon ECS frontend task definition
id: render-frontend
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: frontend-task-def.json
container-name: frontend
image: ${{ needs.build-frontend.outputs.image }}
- name: Deploy Amazon ECS frontend task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.render-frontend.outputs.task-definition }}
service: frontend-service
cluster: ${{ env.CLUSTER_NAME }}
wait-for-service-stability: true