From dc42ba7a1d5ee865d5dc6a8851876c1071e8c622 Mon Sep 17 00:00:00 2001 From: Prakash Date: Wed, 22 May 2024 22:00:07 +0530 Subject: [PATCH] ci: update deploy script --- .github/workflows/deploy-to-ec2.yml | 61 ++++++++++++++++------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/.github/workflows/deploy-to-ec2.yml b/.github/workflows/deploy-to-ec2.yml index a0ec511f..4edab7c8 100644 --- a/.github/workflows/deploy-to-ec2.yml +++ b/.github/workflows/deploy-to-ec2.yml @@ -3,49 +3,54 @@ name: Deploy to EC2 on: push: branches: - - main + - develop + - feat/aws-deploy # Remove before merting this PR to dev jobs: build-and-push: runs-on: ubuntu-latest + timeout-minutes: 10 + environment: staging steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - - name: Set up AWS CLI - uses: aws-actions/configure-aws-credentials@v2 + - name: Login to Docker Hub + uses: docker/login-action@v3 with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ${{ secrets.AWS_REGION }} + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Login to Amazon ECR Public - id: login-ecr - run: aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_URI }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 - - name: Build Docker image - run: docker build -t skill-tree-backend -f skill-tree/Dockerfile . - - name: Tag Docker image - run: docker tag skill-tree-backend:latest ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest - - - name: Push Docker image to ECR Public - run: docker push ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + platforms: linux/arm64 + push: true + tags: | + ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:${{ github.sha }} + ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest + cache-from: type=gha + cache-to: type=gha,mode=max - deploy: - needs: build-and-push - runs-on: ubuntu-latest - steps: - name: Deploy to EC2 uses: appleboy/ssh-action@master with: host: ${{ secrets.AWS_EC2_HOST }} username: ${{ secrets.AWS_EC2_USERNAME }} key: ${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }} - - name: Pull Image from docker hub - run: docker pull ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest - - name: Delete old container - run: docker rm -f skill-tree-backend - - name: Run docker container - run: docker run -d -p 8080:8080 --name skill-tree-backend -e RDS_PUBLIC_KEY=${{secrets.RDS_PUBLIC_KEY}} -e API_V1_PREFIX=/api/v1 ${{ secrets.AWS_ECR_URI }}/skill-tree-backend - + script: | + docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}:latest + docker stop ${{ github.event.repository.name }}-${{vars.ENV}} || true + docker rm ${{ github.event.repository.name }}-${{vars.ENV}} || true + docker run -d -p ${{vars.PORT}}:${{vars.PORT}} \ + --name ${{ github.event.repository.name }}-${{vars.ENV}} \ + --network=${{vars.DOCKER_NETWORK}} \ + -e RDS_PUBLIC_KEY=${{secrets.RDS_PUBLIC_KEY}} \ + -e API_V1_PREFIX=/api/v1 \ + ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }} \ No newline at end of file