From 0821c454a975f036c6bc73e19b08b9c9e6ed84a4 Mon Sep 17 00:00:00 2001 From: Prakash Choudhary <34452139+prakashchoudhary07@users.noreply.github.com> Date: Wed, 5 Jun 2024 02:00:34 +0530 Subject: [PATCH] AWS staging delpoy script (#121) * ci: update deploy script * ci: update file path * ci: update path * ci: update port * ci: update actions * ci: remove quotes * ci: add env variable * ci: add export key * ci: add quotes * chore: add env * chore: testing echo * chore: put in quotes * chore: remove brackets * chore: add env * chore: add quotes * chore: use secret * chore: remove env form ssh script * feat: add conditional environment for action --------- Co-authored-by: Amit Prakash <34869115+iamitprakash@users.noreply.github.com> --- .github/workflows/deploy-to-ec2.yml | 60 ++++++++++++++++------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/.github/workflows/deploy-to-ec2.yml b/.github/workflows/deploy-to-ec2.yml index a0ec511f..c31820d5 100644 --- a/.github/workflows/deploy-to-ec2.yml +++ b/.github/workflows/deploy-to-ec2.yml @@ -4,48 +4,54 @@ 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: ${{ github.ref == 'refs/heads/main' && 'production' || '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: skill-tree/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 }}:8080 \ + --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 }}