From 5eae712a7866a14c15bf3e383a229670dbe26e0d Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 04:50:42 +0100 Subject: [PATCH 01/30] Update dev.yml --- .github/workflows/dev.yml | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index ffe930cf..445bae62 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,3 +1,4 @@ +YAML name: Build, Test, and Deploy for Dev Branch on: @@ -34,13 +35,14 @@ jobs: - name: Deploy and start on remote server run: | sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " - cd /var/www/aihomework/dev - git stash - git pull origin dev - docker load -i /tmp/docker-images.tar.gz - docker compose down - docker compose up -d - rm /tmp/docker-images.tar.gz + gunzip /tmp/docker-images.tar.gz + docker load < /tmp/docker-images.tar.gz + cd /var/www/aihomework/dev + git stash + git pull origin dev + docker compose down + docker compose up -d + rm /tmp/docker-images.tar.gz " env: SSH_HOST: ${{ secrets.HOST }} From 56d392447888d13d9e1f86ece1c8f70d97909739 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 04:54:39 +0100 Subject: [PATCH 02/30] Update dev.yml --- .github/workflows/dev.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 445bae62..ea4f008c 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,4 +1,3 @@ -YAML name: Build, Test, and Deploy for Dev Branch on: From 56c19116556cd20a6ab2372e03fc4cf192c29be2 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 05:24:24 +0100 Subject: [PATCH 03/30] Update dev.yml --- .github/workflows/dev.yml | 90 ++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index ea4f008c..69c751dd 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,49 +1,63 @@ -name: Build, Test, and Deploy for Dev Branch +name: Dev-Deployment on: - push: - branches: - - dev + workflow_run: + workflows: [CI] + types: + - completed + branches: [dev] jobs: - build-and-deploy: runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v3 - - name: Build Docker images using docker-compose + - name: Build docker images + run: docker compose -f docker-compose.yml build + + - name: Save and compress Docker images run: | - docker compose -f docker-compose.yml build + docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') > dev-images.tar + gzip dev-images.tar + + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: docker-images + path: dev-images.tar.gz + + - name: Download artifact + uses: actions/download-artifact@v2 + with: + name: docker-images + path: . + + - name: Copy to server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "dev-images.tar.gz,docker-compose.yml" + target: "~/tmp/" - - name: Save Docker images to tarball - run: | - docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz - - - name: Install sshpass - run: sudo apt-get install sshpass + - name: Deploy images to server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + script: | + cd ~/tmp/ + gunzip dev-images.tar.gz + docker load < dev-images.tar + rm -f dev-images.tar + cd /var/www/aihomework/dev + git stash + git pull origin dev + docker compose down + docker compose up -d + - - name: Upload to server - run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz docker-compose.yml ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: Deploy and start on remote server - run: | - sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " - gunzip /tmp/docker-images.tar.gz - docker load < /tmp/docker-images.tar.gz - cd /var/www/aihomework/dev - git stash - git pull origin dev - docker compose down - docker compose up -d - rm /tmp/docker-images.tar.gz - " - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} From cbc11d4b97219e29a8f4763b4098eded4d2eaf56 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 05:33:29 +0100 Subject: [PATCH 04/30] Update dev.yml --- .github/workflows/dev.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 69c751dd..a2d32531 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,4 +1,4 @@ -name: Dev-Deployment +name: Deploy to dev on: workflow_run: @@ -8,7 +8,9 @@ on: branches: [dev] jobs: + on-success: runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion=='success' && github.event.workflow_run.head_branch == 'dev' } steps: - name: Checkout code uses: actions/checkout@v3 @@ -59,5 +61,9 @@ jobs: docker compose down docker compose up -d - - + on-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion=='failure' }} + steps: + - run: echo "Triggering the Workflow Failed" + From c0bd7d1bafa2f4865e29179942afed503970d03f Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 11:30:42 +0100 Subject: [PATCH 05/30] fetch .env from server for compose up --- .github/workflows/dev.yml | 104 +++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 57 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index a2d32531..ada99c5f 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,69 +1,59 @@ -name: Deploy to dev +name: Build, Test, and Deploy for Dev Branch on: - workflow_run: - workflows: [CI] - types: - - completed - branches: [dev] + push: + branches: + - dev jobs: - on-success: + build-and-deploy: runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion=='success' && github.event.workflow_run.head_branch == 'dev' } steps: - - name: Checkout code - uses: actions/checkout@v3 + - name: Checkout repository + uses: actions/checkout@v4 - - name: Build docker images - run: docker compose -f docker-compose.yml build - - - name: Save and compress Docker images + - name: Install sshpass + run: sudo apt-get install sshpass + + - name: Fetch .env file from server + run: | + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/dev/.env .env + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} + + - name: Build Docker images using docker-compose + run: | + docker compose --env-file .env -f docker-compose.yml build + + - name: Save Docker images to tarball run: | - docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') > dev-images.tar - gzip dev-images.tar - - - name: Upload artifact - uses: actions/upload-artifact@v2 - with: - name: docker-images - path: dev-images.tar.gz - - - name: Download artifact - uses: actions/download-artifact@v2 - with: - name: docker-images - path: . - - - name: Copy to server - uses: appleboy/scp-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - source: "dev-images.tar.gz,docker-compose.yml" - target: "~/tmp/" + docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz - - name: Deploy images to server - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - script: | - cd ~/tmp/ - gunzip dev-images.tar.gz - docker load < dev-images.tar - rm -f dev-images.tar + - name: Install sshpass + run: sudo apt-get install sshpass + + - name: Upload to server + run: | + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz docker-compose.yml ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} + + - name: Deploy and start on remote server + run: | + sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " cd /var/www/aihomework/dev git stash git pull origin dev - docker compose down - docker compose up -d - - on-failure: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion=='failure' }} - steps: - - run: echo "Triggering the Workflow Failed" - + docker load -i /tmp/docker-images.tar.gz + docker compose -f /tmp/docker-compose.yml down + docker compose -f /tmp/docker-compose.yml up -d + rm /tmp/docker-images.tar.gz /tmp/docker-compose.yml + " + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} From ddce402edd93887cda40886666d249710dfab9ac Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 11:44:50 +0100 Subject: [PATCH 06/30] Update staging.yml --- .github/workflows/staging.yml | 69 ++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 3e020bd7..cde27135 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,4 +1,4 @@ -name: Build, Test, and Deploy for Dev Branch +name: Deploy to staging Branch on: push: @@ -6,35 +6,54 @@ on: - dev jobs: - build: - runs-on: self-hosted - defaults: - run: - working-directory: /var/www/aihomework/staging - + build-and-deploy: + runs-on: ubuntu-latest steps: - - name: Pull from github - id: pull - run: | - git stash - git pull origin staging + - name: Checkout repository + uses: actions/checkout@v4 - - name: install dependencies - run: yarn install + - name: Install sshpass + run: sudo apt-get install sshpass - - name: Run Test - run: yarn test + - name: Fetch .env file from server + run: | + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/staging/.env .env + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: buld the dist - run: yarn build + - name: Build Docker images using docker-compose + run: | + docker compose --env-file .env -f docker-compose.staging.yml build - - name: migrate - run: yarn reset-db + - name: Save Docker images to tarball + run: | + docker save $(docker compose -f docker-compose.staging.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz + + - name: Install sshpass + run: sudo apt-get install sshpass - - name: setup service file - run: sudo cp server-script/aihomeworkstaging.service /etc/systemd/system + - name: Upload to server + run: | + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: start the app + - name: Deploy and start on remote server run: | - sudo systemctl daemon-reload - sudo systemctl restart aihomeworkstaging + sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " + cd /var/www/aihomework/dev + git stash + git pull origin dev + docker load -i /tmp/docker-images.tar.gz + docker compose -f docker-compose.staging.yml down + docker compose -f docker-compose.staging.yml up -d + rm /tmp/docker-images.tar.gz + " + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} From 2565a327927757ec70a3d6ec21a81bc4bdb83049 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 11:46:08 +0100 Subject: [PATCH 07/30] Update prod.yml --- .github/workflows/prod.yml | 68 ++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index 64a6096e..e05ca15d 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -1,35 +1,59 @@ -name: Build, Test, and Deploy for Prod Branch +name: Deploy to staging Branch on: push: - branches: [main] + branches: + - dev jobs: - build: - runs-on: self-hosted - defaults: - run: - working-directory: /var/www/aihomework/prod - + build-and-deploy: + runs-on: ubuntu-latest steps: - - name: Pull from GitHub + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install sshpass + run: sudo apt-get install sshpass + + - name: Fetch .env file from server run: | - git stash - git pull origin main + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/staging/.env .env + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: Install dependencies - run: yarn install + - name: Build Docker images using docker-compose + run: | + docker compose --env-file .env -f docker-compose.production.yml build - - name: Build the dist - run: yarn build + - name: Save Docker images to tarball + run: | + docker save $(docker compose -f docker-compose.production.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz - - name: migrate - run: yarn migrate + - name: Install sshpass + run: sudo apt-get install sshpass - - name: Setup service file - run: sudo cp server-script/aihomeworkprod.service /etc/systemd/system + - name: Upload to server + run: | + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: Start the app + - name: Deploy and start on remote server run: | - sudo systemctl daemon-reload - sudo systemctl restart aihomeworkprod + sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " + cd /var/www/aihomework/dev + git stash + git pull origin dev + docker load -i /tmp/docker-images.tar.gz + docker compose -f docker-compose.production.yml down + docker compose -f docker-compose.production.yml up -d + rm /tmp/docker-images.tar.gz + " + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} From b908517e48b2c9d10efbb3ec0e0fe0498c121304 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 11:46:31 +0100 Subject: [PATCH 08/30] Update dev.yml --- .github/workflows/dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index ada99c5f..8999e9fa 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -51,7 +51,7 @@ jobs: docker load -i /tmp/docker-images.tar.gz docker compose -f /tmp/docker-compose.yml down docker compose -f /tmp/docker-compose.yml up -d - rm /tmp/docker-images.tar.gz /tmp/docker-compose.yml + rm /tmp/docker-images.tar.gz " env: SSH_HOST: ${{ secrets.HOST }} From 3de0541d17f251b1b075282ff3333bd50a48a3a1 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 12:28:53 +0100 Subject: [PATCH 09/30] Update prod.yml --- .github/workflows/prod.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index e05ca15d..21f8983d 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -31,9 +31,6 @@ jobs: run: | docker save $(docker compose -f docker-compose.production.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz - - name: Install sshpass - run: sudo apt-get install sshpass - - name: Upload to server run: | sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ From 0c1f3b809e602f76b3195b4548c0e929931c9584 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 12:29:25 +0100 Subject: [PATCH 10/30] Update staging.yml --- .github/workflows/staging.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index cde27135..5c6d315e 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -31,9 +31,6 @@ jobs: run: | docker save $(docker compose -f docker-compose.staging.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz - - name: Install sshpass - run: sudo apt-get install sshpass - - name: Upload to server run: | sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ From e8f5c194c7e8f7dfc9785b88f1f3398aceab2dec Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 12:32:00 +0100 Subject: [PATCH 11/30] Update dev.yml --- .github/workflows/dev.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 8999e9fa..dc4f8744 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -31,9 +31,6 @@ jobs: run: | docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz - - name: Install sshpass - run: sudo apt-get install sshpass - - name: Upload to server run: | sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz docker-compose.yml ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ From 9feb21ba61ff0956de0920c28fda754f7f07a443 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:30:16 +0100 Subject: [PATCH 12/30] Update .env path in docker-compose.yml --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b3b36c4f..89f740c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: ports: - 2222:8000 env_file: - - /var/www/aihomework/dev/.env + - .env environment: NODE_ENV: development DB_HOST: backend_db @@ -24,7 +24,7 @@ services: container_name: backend_db restart: unless-stopped env_file: - - /var/www/aihomework/dev/.env + - .env environment: POSTGRES_USER: ${DB_USER} POSTGRES_PASSWORD: ${DB_PASSWORD} From 5966e228c61095d4b362cbcf42fe897b6edddee9 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:30:52 +0100 Subject: [PATCH 13/30] Update .env path in docker-compose.staging.yml --- docker-compose.staging.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml index 1c49750c..8abf5b17 100644 --- a/docker-compose.staging.yml +++ b/docker-compose.staging.yml @@ -6,7 +6,7 @@ services: ports: - 3333:8000 env_file: - - /var/www/aihomework/staging/.env + - .env environment: NODE_ENV: staging DB_HOST: backend_db_staging @@ -24,7 +24,7 @@ services: container_name: backend_db_staging restart: unless-stopped env_file: - - /var/www/aihomework/staging/.env + - .env environment: POSTGRES_USER: ${DB_USER} POSTGRES_PASSWORD: ${DB_PASSWORD} From f96e264f254fa66b8dd2a6fdb0c254d005f2dc39 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:31:26 +0100 Subject: [PATCH 14/30] Update .env path in docker-compose.production.yml --- docker-compose.production.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.production.yml b/docker-compose.production.yml index 82eb3069..39ba8c10 100644 --- a/docker-compose.production.yml +++ b/docker-compose.production.yml @@ -6,7 +6,7 @@ services: ports: - 4444:8000 env_file: - - /var/www/aihomework/prod/.env + - .env environment: NODE_ENV: production DB_HOST: backend_db_prod @@ -24,7 +24,7 @@ services: container_name: backend_db_prod restart: unless-stopped env_file: - - /var/www/aihomework/prod/.env + - .env environment: POSTGRES_USER: ${DB_USER} POSTGRES_PASSWORD: ${DB_PASSWORD} From 82179da2f23ead2b8b9942ea1fb2413e6b5d5dc5 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:39:00 +0100 Subject: [PATCH 15/30] Update dev.yml --- .github/workflows/dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index dc4f8744..291e3bb8 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,4 +1,4 @@ -name: Build, Test, and Deploy for Dev Branch +name: Deploy to Dev on: push: From 3c3abbf9bf44368182bf8de6705a45c02a5a4586 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:39:31 +0100 Subject: [PATCH 16/30] Update prod.yml --- .github/workflows/prod.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index 21f8983d..e275d488 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -1,9 +1,9 @@ -name: Deploy to staging Branch +name: Deploy to prod Branch on: push: branches: - - dev + - prod jobs: build-and-deploy: From df14e73a45807abdd28bc0f419b3005970980d54 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:48:01 +0100 Subject: [PATCH 17/30] Update staging.yml depends on CI --- .github/workflows/staging.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 5c6d315e..6a458a03 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,10 +1,12 @@ name: Deploy to staging Branch on: - push: - branches: - - dev - + workflow_run: + workflows: [CI] + types: + - completed + branches: [staging] + jobs: build-and-deploy: runs-on: ubuntu-latest From 41f37aeafb3da2c251e6358507b7ff21a835f6c3 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:49:30 +0100 Subject: [PATCH 18/30] Update CI.yml run on push to all branches and on pull requests --- .github/workflows/CI.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 20a83ed3..dc06e527 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,9 +1,11 @@ name: CI on: + push: + branches: [dev, staging, main] pull_request: - branches: - - dev + types: [opened, synchronize, reopened] + branches: [dev, staging, main] jobs: test: @@ -25,5 +27,6 @@ jobs: run: yarn test env: CI: true - - name: buld the dist + + - name: Build the dist run: yarn build From ffce86a23cd0f192e564f15d2f9ab8a38793b5d2 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:50:11 +0100 Subject: [PATCH 19/30] Update dev.yml depends on ci --- .github/workflows/dev.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 291e3bb8..1a367ce4 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,9 +1,11 @@ name: Deploy to Dev on: - push: - branches: - - dev + workflow_run: + workflows: [CI] + types: + - completed + branches: [dev] jobs: build-and-deploy: From de40e9acc8a6d920d8076d63525c593289bf4c1d Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:51:40 +0100 Subject: [PATCH 20/30] Update prod.yml depend on ci --- .github/workflows/prod.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index e275d488..0f580ff5 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -1,9 +1,11 @@ name: Deploy to prod Branch on: - push: - branches: - - prod + workflow_run: + workflows: [CI] + types: + - completed + branches: [main] jobs: build-and-deploy: @@ -42,13 +44,12 @@ jobs: - name: Deploy and start on remote server run: | sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " - cd /var/www/aihomework/dev + cd /var/www/aihomework/prod git stash - git pull origin dev + git pull origin main docker load -i /tmp/docker-images.tar.gz docker compose -f docker-compose.production.yml down docker compose -f docker-compose.production.yml up -d - rm /tmp/docker-images.tar.gz " env: SSH_HOST: ${{ secrets.HOST }} From 076de18eaa49119e979076c9c40157ab1842b58c Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Fri, 9 Aug 2024 18:52:53 +0100 Subject: [PATCH 21/30] Update staging.yml pull staging branch --- .github/workflows/staging.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 6a458a03..729c31ee 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -44,9 +44,9 @@ jobs: - name: Deploy and start on remote server run: | sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " - cd /var/www/aihomework/dev + cd /var/www/aihomework/staging git stash - git pull origin dev + git pull origin staging docker load -i /tmp/docker-images.tar.gz docker compose -f docker-compose.staging.yml down docker compose -f docker-compose.staging.yml up -d From b16153a54aa4dd108c7528f38b423c3fd23ceb72 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 10 Aug 2024 11:13:50 +0100 Subject: [PATCH 22/30] chore: user table --- src/models/user.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/models/user.ts b/src/models/user.ts index 03dc0d22..ad88e983 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -68,6 +68,9 @@ export class User extends ExtendedBaseEntity { @Column({ nullable: true }) otp: number; + @Column({ default: false }) + is_superadmin: boolean; + @Column({ nullable: true }) otp_expires_at: Date; From 9bc4d4fecd0bdc9d0f8267edac396a16d93f4bf4 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 10 Aug 2024 11:19:50 +0100 Subject: [PATCH 23/30] chore: user table --- src/models/faq.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/models/faq.ts b/src/models/faq.ts index 86932bec..8f570d22 100644 --- a/src/models/faq.ts +++ b/src/models/faq.ts @@ -1,4 +1,10 @@ -import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"; +import { + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from "typeorm"; import ExtendedBaseEntity from "./extended-base-entity"; import { UserRole } from "../enums/userRoles"; @@ -18,6 +24,12 @@ class FAQ extends ExtendedBaseEntity { @Column({ nullable: false, default: UserRole.SUPER_ADMIN }) createdBy: string; + + @CreateDateColumn() + created_at: Date; + + @UpdateDateColumn() + updated_at: Date; } export { FAQ }; From d6459271295184903abdc676dabab35d62561816 Mon Sep 17 00:00:00 2001 From: Rob-in-son Date: Sat, 10 Aug 2024 16:08:02 +0100 Subject: [PATCH 24/30] Update dev workflow --- .github/workflows/dev.yml | 83 ++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 1a367ce4..9a956343 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -1,4 +1,4 @@ -name: Deploy to Dev +name: Deploy to Dev on: workflow_run: @@ -10,49 +10,70 @@ on: jobs: build-and-deploy: runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - name: Checkout repository uses: actions/checkout@v4 - - name: Install sshpass - run: sudo apt-get install sshpass + - name: Install Docker Compose + run: | + sudo apt-get update + sudo apt-get install -y docker-compose - name: Fetch .env file from server - run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/dev/.env .env - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "/var/www/aihomework/dev/.env" + target: "." + strip_components: 4 - - name: Build Docker images using docker-compose + - name: Build Docker images run: | docker compose --env-file .env -f docker-compose.yml build + - name: List Docker images + run: docker images + - name: Save Docker images to tarball run: | - docker save $(docker compose -f docker-compose.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz + docker save $(docker images --format '{{.Repository}}:{{.Tag}}' | grep -v '') | gzip > dev-images.tar.gz - - name: Upload to server - run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz docker-compose.yml ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: dev-images + path: dev-images.tar.gz - - name: Deploy and start on remote server - run: | - sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " + - name: Copy Docker images to server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "dev-images.tar.gz" + target: "~/images-tar" + + - name: Deploy to server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + script: | cd /var/www/aihomework/dev + git fetch origin dev git stash - git pull origin dev - docker load -i /tmp/docker-images.tar.gz - docker compose -f /tmp/docker-compose.yml down - docker compose -f /tmp/docker-compose.yml up -d - rm /tmp/docker-images.tar.gz - " - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + git checkout dev + git pull + docker load -i ~/images-tar/dev-images.tar.gz + docker compose -f docker-compose.yml down + docker compose -f docker-compose.yml up -d + + on-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + steps: + - run: echo "CI Workflow failed. Dev deployment was not triggered." \ No newline at end of file From 9cd70b30d3bad3b7f9edb2a0c5a6f0402d23d972 Mon Sep 17 00:00:00 2001 From: Rob-in-son Date: Sat, 10 Aug 2024 17:56:09 +0100 Subject: [PATCH 25/30] Copy env file to .env --- .github/workflows/dev.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 9a956343..05115ef5 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -27,8 +27,7 @@ jobs: username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} source: "/var/www/aihomework/dev/.env" - target: "." - strip_components: 4 + target: ".env" - name: Build Docker images run: | From 659af2dc23ebc13dd35a3cc94885c5f83e744676 Mon Sep 17 00:00:00 2001 From: Rob-in-son Date: Sat, 10 Aug 2024 18:45:09 +0100 Subject: [PATCH 26/30] change scp .env step --- .github/workflows/dev.yml | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 05115ef5..09f3e3bf 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -15,19 +15,25 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Install Docker Compose - run: | - sudo apt-get update - sudo apt-get install -y docker-compose + # - name: Fetch .env file from server + # uses: appleboy/scp-action@master + # with: + # host: ${{ secrets.HOST }} + # username: ${{ secrets.USERNAME }} + # password: ${{ secrets.PASSWORD }} + # source: "/var/www/aihomework/dev/.env" + # target: ".env" + + - name: Install sshpass + run: sudo apt-get install sshpass - name: Fetch .env file from server - uses: appleboy/scp-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - source: "/var/www/aihomework/dev/.env" - target: ".env" + run: | + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/dev/.env .env + env: + SSH_HOST: ${{ secrets.HOST }} + SSH_USERNAME: ${{ secrets.USERNAME }} + SSH_PASSWORD: ${{ secrets.PASSWORD }} - name: Build Docker images run: | From 948f27a366d734155dea77ede172f5007c34740f Mon Sep 17 00:00:00 2001 From: Rob-in-son Date: Sat, 10 Aug 2024 18:57:13 +0100 Subject: [PATCH 27/30] change docker save --- .github/workflows/dev.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 09f3e3bf..154f74e4 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -8,7 +8,7 @@ on: branches: [dev] jobs: - build-and-deploy: + on-success: runs-on: ubuntu-latest if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: @@ -44,7 +44,7 @@ jobs: - name: Save Docker images to tarball run: | - docker save $(docker images --format '{{.Repository}}:{{.Tag}}' | grep -v '') | gzip > dev-images.tar.gz + docker save hng_boilerplate_expressjs-backend:latest | gzip > dev-images.tar.gz - name: Upload artifact uses: actions/upload-artifact@v2 From 8c431956dc367d7706ee0e0c27f7cb2db9bc9f27 Mon Sep 17 00:00:00 2001 From: Rob-in-son Date: Sat, 10 Aug 2024 22:10:33 +0100 Subject: [PATCH 28/30] Updated deployment dir --- .github/workflows/dev.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 154f74e4..c2a45f31 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -68,8 +68,7 @@ jobs: username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} script: | - cd /var/www/aihomework/dev - git fetch origin dev + cd ~/dev-deployment/hng_boilerplate_expressjs git stash git checkout dev git pull From 53cca85df61f06acfe97079192f83b0e75958d31 Mon Sep 17 00:00:00 2001 From: Rob-in-son Date: Sun, 11 Aug 2024 00:13:27 +0100 Subject: [PATCH 29/30] updated dockercompose files and workflows --- .github/workflows/dev.yml | 9 ----- .github/workflows/prod.yml | 67 +++++++++++++++++++++------------- .github/workflows/staging.yml | 68 +++++++++++++++++++++-------------- docker-compose.production.yml | 2 +- docker-compose.staging.yml | 2 +- 5 files changed, 86 insertions(+), 62 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index c2a45f31..ddf55eda 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -14,15 +14,6 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 - - # - name: Fetch .env file from server - # uses: appleboy/scp-action@master - # with: - # host: ${{ secrets.HOST }} - # username: ${{ secrets.USERNAME }} - # password: ${{ secrets.PASSWORD }} - # source: "/var/www/aihomework/dev/.env" - # target: ".env" - name: Install sshpass run: sudo apt-get install sshpass diff --git a/.github/workflows/prod.yml b/.github/workflows/prod.yml index 0f580ff5..df8f28ae 100644 --- a/.github/workflows/prod.yml +++ b/.github/workflows/prod.yml @@ -1,57 +1,74 @@ -name: Deploy to prod Branch +name: Deploy to Prod on: workflow_run: workflows: [CI] types: - completed - branches: [main] + branches: [prod] jobs: - build-and-deploy: + on-success: runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - name: Checkout repository uses: actions/checkout@v4 - + - name: Install sshpass run: sudo apt-get install sshpass - name: Fetch .env file from server run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/staging/.env .env + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/prod-deployment/hng_boilerplate_expressjs/.env .env env: SSH_HOST: ${{ secrets.HOST }} SSH_USERNAME: ${{ secrets.USERNAME }} SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: Build Docker images using docker-compose + - name: Build Docker images run: | docker compose --env-file .env -f docker-compose.production.yml build + - name: List Docker images + run: docker images + - name: Save Docker images to tarball run: | - docker save $(docker compose -f docker-compose.production.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz + docker save hng_boilerplate_expressjs-backend_prod:latest | gzip > prod-images.tar.gz - - name: Upload to server - run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: prod-images + path: prod-images.tar.gz - - name: Deploy and start on remote server - run: | - sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " - cd /var/www/aihomework/prod + - name: Copy Docker images to server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "prod-images.tar.gz" + target: "~/images-tar" + + - name: Deploy to server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + script: | + cd ~/prod-deployment/hng_boilerplate_expressjs git stash - git pull origin main - docker load -i /tmp/docker-images.tar.gz + git checkout prod + git pull + docker load -i ~/images-tar/prod-images.tar.gz docker compose -f docker-compose.production.yml down docker compose -f docker-compose.production.yml up -d - " - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + + on-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + steps: + - run: echo "CI Workflow failed. Prod deployment was not triggered." \ No newline at end of file diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index 729c31ee..9d757dd3 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,4 +1,4 @@ -name: Deploy to staging Branch +name: Deploy to staging on: workflow_run: @@ -6,53 +6,69 @@ on: types: - completed branches: [staging] - + jobs: - build-and-deploy: + on-success: runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - name: Checkout repository uses: actions/checkout@v4 - + - name: Install sshpass run: sudo apt-get install sshpass - name: Fetch .env file from server run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/staging/.env .env + sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/staging-deployment/hng_boilerplate_expressjs/.env .env env: SSH_HOST: ${{ secrets.HOST }} SSH_USERNAME: ${{ secrets.USERNAME }} SSH_PASSWORD: ${{ secrets.PASSWORD }} - - name: Build Docker images using docker-compose + - name: Build Docker images run: | docker compose --env-file .env -f docker-compose.staging.yml build + - name: List Docker images + run: docker images + - name: Save Docker images to tarball run: | - docker save $(docker compose -f docker-compose.staging.yml config | grep 'image:' | awk '{print $2}') | gzip > docker-images.tar.gz + docker save hng_boilerplate_expressjs-backend_staging:latest | gzip > staging-images.tar.gz - - name: Upload to server - run: | - sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no docker-images.tar.gz ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/tmp/ - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: staging-images + path: staging-images.tar.gz - - name: Deploy and start on remote server - run: | - sshpass -p ${{ secrets.PASSWORD }} ssh -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }} " - cd /var/www/aihomework/staging + - name: Copy Docker images to server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "staging-images.tar.gz" + target: "~/images-tar" + + - name: Deploy to server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + script: | + cd ~/staging-deployment/hng_boilerplate_expressjs git stash - git pull origin staging - docker load -i /tmp/docker-images.tar.gz + git checkout staging + git pull + docker load -i ~/images-tar/staging-images.tar.gz docker compose -f docker-compose.staging.yml down docker compose -f docker-compose.staging.yml up -d - rm /tmp/docker-images.tar.gz - " - env: - SSH_HOST: ${{ secrets.HOST }} - SSH_USERNAME: ${{ secrets.USERNAME }} - SSH_PASSWORD: ${{ secrets.PASSWORD }} + + on-failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' }} + steps: + - run: echo "CI Workflow failed. Staging deployment was not triggered." \ No newline at end of file diff --git a/docker-compose.production.yml b/docker-compose.production.yml index 39ba8c10..82ba1d0c 100644 --- a/docker-compose.production.yml +++ b/docker-compose.production.yml @@ -1,5 +1,5 @@ services: - backend: + backend_prod: container_name: backend_prod build: context: . diff --git a/docker-compose.staging.yml b/docker-compose.staging.yml index 8abf5b17..3e9dba04 100644 --- a/docker-compose.staging.yml +++ b/docker-compose.staging.yml @@ -1,5 +1,5 @@ services: - backend: + backend_staging: container_name: backend_staging build: context: . From 31a8bd7c8d3cd8b57afeeb8dcd9e4a4ddee19a24 Mon Sep 17 00:00:00 2001 From: Robinson Uchechukwu Date: Tue, 13 Aug 2024 13:26:06 +0100 Subject: [PATCH 30/30] cd: update dev.yml --- .github/workflows/dev.yml | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index ddf55eda..9acefc15 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -8,15 +8,14 @@ on: branches: [dev] jobs: - on-success: + deploy: runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'success' }} steps: - name: Checkout repository uses: actions/checkout@v4 - - name: Install sshpass - run: sudo apt-get install sshpass + # - name: Install sshpass + # run: sudo apt-get install sshpass - name: Fetch .env file from server run: | @@ -37,12 +36,6 @@ jobs: run: | docker save hng_boilerplate_expressjs-backend:latest | gzip > dev-images.tar.gz - - name: Upload artifact - uses: actions/upload-artifact@v2 - with: - name: dev-images - path: dev-images.tar.gz - - name: Copy Docker images to server uses: appleboy/scp-action@master with: @@ -66,9 +59,3 @@ jobs: docker load -i ~/images-tar/dev-images.tar.gz docker compose -f docker-compose.yml down docker compose -f docker-compose.yml up -d - - on-failure: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'failure' }} - steps: - - run: echo "CI Workflow failed. Dev deployment was not triggered." \ No newline at end of file