@@ -67,43 +67,99 @@ jobs:
6767 retention-days : 7
6868
6969 # Step 3: Build and Deploy (only on main branch)
70+ # build-and-deploy:
71+ # name: Build & Deploy to Docker Hub
72+ # runs-on: ubuntu-latest
73+ # needs: [lint, test]
74+
75+ # steps:
76+ # - name: Checkout code
77+ # uses: actions/checkout@v4
78+
79+ # - name: Set up QEMU
80+ # uses: docker/setup-qemu-action@v3
81+
82+ # - name: Set up Docker Buildx
83+ # uses: docker/setup-buildx-action@v3
84+
85+ # - name: Extract metadata
86+ # id: meta
87+ # run: |
88+ # echo "short_sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
89+ # echo "date=$(date +'%Y%m%d')" >> "$GITHUB_OUTPUT"
90+
91+ # - name: Log in to Docker Hub
92+ # uses: docker/login-action@v3
93+ # with:
94+ # username: ${{ secrets.DOCKER_USERNAME }}
95+ # password: ${{ secrets.DOCKERHUB_TOKEN }}
96+
97+ # - name: Build and push production image
98+ # uses: docker/build-push-action@v6
99+ # with:
100+ # context: .
101+ # file: Dockerfile
102+ # push: true
103+ # platforms: linux/amd64,linux/arm64
104+ # tags: |
105+ # ${{ env.DOCKERHUB_REPO }}:latest
106+ # ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.short_sha }}
107+ # ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.date }}
108+ # cache-from: type=gha
109+ # cache-to: type=gha,mode=max
110+
111+ # Step 4: Build and Deploy to AWS
112+ # Step 2: Build and (conditionally) Deploy
70113 build-and-deploy :
71- name : Build & Deploy to Docker Hub
114+ name : Build & Deploy to AWS ECS Fargate
72115 runs-on : ubuntu-latest
73116 needs : [lint, test]
74117
118+ # Only run on develop branch (For test purposes). To be replaced with main branch when ready.
119+ if : github.event_name == 'push' && github.ref == 'refs/heads/develop'
120+
75121 steps :
76122 - name : Checkout code
77123 uses : actions/checkout@v4
78124
79- - name : Set up QEMU
80- uses : docker/setup-qemu-action@v3
125+ - name : Configure AWS credentials
126+ uses : aws-actions/configure-aws-credentials@v4
127+ with :
128+ aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
129+ aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
130+ aws-region : ${{ secrets.AWS_REGION }}
81131
82- - name : Set up Docker Buildx
83- uses : docker/setup-buildx-action@v3
132+ - name : Login to Amazon ECR
133+ uses : aws-actions/amazon-ecr-login@v2
84134
85135 - name : Extract metadata
86136 id : meta
87137 run : |
88138 echo "short_sha=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
89139 echo "date=$(date +'%Y%m%d')" >> "$GITHUB_OUTPUT"
90140
91- - name : Log in to Docker Hub
92- uses : docker/login-action@v3
93- with :
94- username : ${{ secrets.DOCKER_USERNAME }}
95- password : ${{ secrets.DOCKERHUB_TOKEN }}
141+ - name : Build Docker image
142+ id : build-image
143+ run : |
144+ IMAGE_TAG=${{ steps.meta.outputs.short_sha }}
145+ ECR_IMAGE=${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG
146+ docker build -t $ECR_IMAGE .
147+ echo "ECR_IMAGE=$ECR_IMAGE" >> $GITHUB_ENV
148+
149+ # Only push image & deploy when pushing to main
150+ - name : Push image to ECR
151+ # Only run on develop branch (For test purposes). To be replaced with main branch when ready.
152+ if : github.event_name == 'push' && github.ref == 'refs/heads/develop'
153+ run : |
154+ docker push $ECR_IMAGE
96155
97- - name : Build and push production image
98- uses : docker/build-push-action@v6
99- with :
100- context : .
101- file : Dockerfile
102- push : true
103- platforms : linux/amd64,linux/arm64
104- tags : |
105- ${{ env.DOCKERHUB_REPO }}:latest
106- ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.short_sha }}
107- ${{ env.DOCKERHUB_REPO }}:${{ steps.meta.outputs.date }}
108- cache-from : type=gha
109- cache-to : type=gha,mode=max
156+ - name : Force new ECS deployment
157+ # Only run on develop branch (For test purposes). To be replaced with main branch when ready.
158+ if : github.event_name == 'push' && github.ref == 'refs/heads/develop'
159+ run : |
160+ echo "Triggering ECS service update..."
161+ aws ecs update-service \
162+ --cluster ${{ env.ECS_CLUSTER }} \
163+ --service ${{ env.ECS_SERVICE }} \
164+ --force-new-deployment \
165+ --region ${{ secrets.AWS_REGION }}
0 commit comments