From 8709ff6e3315fbe4f8483b69425fbde731c48661 Mon Sep 17 00:00:00 2001 From: Hussen Ali Date: Sat, 27 Jun 2026 12:14:31 +0530 Subject: [PATCH] Update workflow for AWS dev deployment using env variables --- .github/workflows/deploy.yml | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 675029d..594c55a 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -26,27 +26,39 @@ jobs: - name: Run tests run: pytest -q - build-and-push: + deploy: if: github.event_name == 'push' needs: validate runs-on: ubuntu-latest permissions: + id-token: write contents: read - packages: write steps: - name: Check out code uses: actions/checkout@v4 - - name: Log in to GitHub Container Registry - uses: docker/login-action@v3 + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} + role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} + aws-region: ${{ vars.AWS_REGION }} - - name: Build and push Docker image - uses: docker/build-push-action@v6 - with: - context: . - push: true - tags: ghcr.io/${{ github.repository_owner }}/simple-ecs-python-app:latest + - name: Ensure ECR repository exists + run: | + aws ecr describe-repositories --repository-names ${{ vars.ECR_REPOSITORY }} >/dev/null 2>&1 || \ + aws ecr create-repository --repository-name ${{ vars.ECR_REPOSITORY }} + + - name: Log in to Amazon ECR + run: | + aws ecr get-login-password --region ${{ vars.AWS_REGION }} | \ + docker login --username AWS --password-stdin ${{ vars.AWS_ACCOUNT_ID }}.dkr.ecr.${{ vars.AWS_REGION }}.amazonaws.com + + - name: Build and push Docker image to ECR + run: | + IMAGE_URI=${{ vars.AWS_ACCOUNT_ID }}.dkr.ecr.${{ vars.AWS_REGION }}.amazonaws.com/${{ vars.ECR_REPOSITORY }}:${{ github.sha }} + docker build -t "$IMAGE_URI" . + docker push "$IMAGE_URI" + + - name: Update ECS service + run: | + aws ecs update-service --cluster "${{ vars.ECS_CLUSTER }}" --service "${{ vars.ECS_SERVICE }}" --force-new-deployment