diff --git a/.github/workflows/cd-production.yml b/.github/workflows/cd-production.yml new file mode 100644 index 000000000..bf82af664 --- /dev/null +++ b/.github/workflows/cd-production.yml @@ -0,0 +1,59 @@ +name: Deploy AI Platform to ECS + +on: + push: + tags: + - 'v*' # Deploy only when tags like v1.0.0, v2.1.0, etc., are created + +jobs: + build: + runs-on: ubuntu-latest + + permissions: + packages: write + contents: read + attestations: write + id-token: write + + steps: + - name: Checkout the repo + uses: actions/checkout@v4 + + - name: Check if tag is from main branch + id: check-branch + run: | + if [ "$(git branch -r --contains ${{ github.ref }})" == "origin/main" ]; then + echo "IS_MAIN=true" >> $GITHUB_ENV + else + echo "IS_MAIN=false" >> $GITHUB_ENV + fi + + - name: Configure AWS credentials + if: env.IS_MAIN == 'true' + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::024209611402:role/github-action-role + aws-region: ap-south-1 + + - name: Login to Amazon ECR + if: env.IS_MAIN == 'true' + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build and Push Docker Image + if: env.IS_MAIN == 'true' + env: + REGISTRY: ${{ steps.login-ecr.outputs.registry }} + REPOSITORY: ${{ github.event.repository.name }}-repo + TAG: ${{ github.ref_name }} + run: | + docker build -t $REGISTRY/$REPOSITORY:$TAG ./backend + docker push $REGISTRY/$REPOSITORY:$TAG + + - name: Deploy to ECS + if: env.IS_MAIN == 'true' + run: | + aws ecs update-service \ + --cluster ${{ github.event.repository.name }}-cluster \ + --service ${{ github.event.repository.name }}-service \ + --force-new-deployment