From b3a8c2039694ea70b5c70fa026b763f209e67ec1 Mon Sep 17 00:00:00 2001 From: Anish Mishra Date: Wed, 3 Jul 2024 18:51:18 +0530 Subject: [PATCH] Update deploy-aws.yml --- .github/workflows/deploy-aws.yml | 74 ++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/.github/workflows/deploy-aws.yml b/.github/workflows/deploy-aws.yml index d0de664..7fce4d7 100644 --- a/.github/workflows/deploy-aws.yml +++ b/.github/workflows/deploy-aws.yml @@ -5,35 +5,39 @@ on: branches: - main - prod + jobs: - - Deploy: + build_push: runs-on: ubuntu-latest steps: - - - name: Checkout - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ secrets.GHCR_USERNAME }} + password: ${{ secrets.GHCR_TOKEN }} - - name: Login to GitHub Container Registry - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{ secrets.GHCR_USERNAME }} - password: ${{ secrets.GHCR_TOKEN }} - - - name: Build and Push Docker Image - run: | - export CURRENT_BRANCH=${GITHUB_REF#refs/heads/} - export TAG=$([[ $CURRENT_BRANCH == $(basename ${{ github.ref }}) ]] && echo $CURRENT_BRANCH || echo "latest") - echo $TAG - export GITHUB_REF_IMAGE=ghcr.io/$GITHUB_REPOSITORY:$GITHUB_SHA - export GITHUB_BRANCH_IMAGE=ghcr.io/$GITHUB_REPOSITORY:$TAG - export GITHUB_REF_IMAGE=$(echo $GITHUB_REF_IMAGE | tr '[:upper:]' '[:lower:]') - export GITHUB_BRANCH_IMAGE=$(echo $GITHUB_BRANCH_IMAGE | tr '[:upper:]' '[:lower:]') - docker build --build-arg version=$GITHUB_SHA -t $GITHUB_REF_IMAGE -t $GITHUB_BRANCH_IMAGE . - echo "Pushing Image to GitHub Container Registry" - docker push $GITHUB_REF_IMAGE - docker push $GITHUB_BRANCH_IMAGE + - name: Build and Push Docker Image + run: | + export CURRENT_BRANCH=${GITHUB_REF#refs/heads/} + export TAG=$([[ $CURRENT_BRANCH == $(basename ${{ github.ref }}) ]] && echo $CURRENT_BRANCH || echo "latest") + echo $TAG + export GITHUB_REF_IMAGE=ghcr.io/$GITHUB_REPOSITORY:$GITHUB_SHA + export GITHUB_BRANCH_IMAGE=ghcr.io/$GITHUB_REPOSITORY:$TAG + export GITHUB_REF_IMAGE=$(echo $GITHUB_REF_IMAGE | tr '[:upper:]' '[:lower:]') + export GITHUB_BRANCH_IMAGE=$(echo $GITHUB_BRANCH_IMAGE | tr '[:upper:]' '[:lower:]') + docker build --build-arg version=$GITHUB_SHA -t $GITHUB_REF_IMAGE -t $GITHUB_BRANCH_IMAGE . + echo "Pushing Image to GitHub Container Registry" + docker push $GITHUB_REF_IMAGE + docker push $GITHUB_BRANCH_IMAGE + + deploy: + needs: build_push + runs-on: self-hosted + steps: - name: Deploy on DEV server if: github.ref == 'refs/heads/main' uses: appleboy/ssh-action@v0.1.7 @@ -50,11 +54,27 @@ jobs: docker pull ghcr.io/netsepio/gateway:main docker run --restart=on-failure:14 --dns=1.1.1.1 --name="gateway" --env-file=.env -p 3000:3000 --net=bridge -d ghcr.io/netsepio/gateway:main - - name: Deploy on PROD server + - name: Deploy on PROD server 1 + if: github.ref == 'refs/heads/prod' + uses: appleboy/ssh-action@v0.1.7 + with: + host: ${{ secrets.PROD_REMOTE_SERVER_1 }} + username: ${{ secrets.PROD_SERVER_USERNAME }} + key: ${{ secrets.PROD_REMOTE_SERVER_KEY }} + port: ${{ secrets.PROD_SSH_PORT }} + script: | + pwd + cd gateway + docker stop gateway && docker rm gateway && docker image rm ghcr.io/netsepio/gateway:prod + echo ${{ secrets.GHCR_TOKEN }} | docker login ghcr.io -u ${{ secrets.GHCR_USERNAME }} --password-stdin + docker pull ghcr.io/netsepio/gateway:prod + docker run --restart=on-failure:14 --dns=1.1.1.1 --name="gateway" --env-file=.env -p 3000:3000 --net=bridge -d ghcr.io/netsepio/gateway:prod + + - name: Deploy on PROD server 2 if: github.ref == 'refs/heads/prod' uses: appleboy/ssh-action@v0.1.7 with: - host: ${{ secrets.PROD_REMOTE_SERVER_ADDRESS }} + host: ${{ secrets.PROD_REMOTE_SERVER_2 }} username: ${{ secrets.PROD_SERVER_USERNAME }} key: ${{ secrets.PROD_REMOTE_SERVER_KEY }} port: ${{ secrets.PROD_SSH_PORT }}