From 3b7058829a1232c03ff642815220315718c91337 Mon Sep 17 00:00:00 2001 From: Ravencodess <115118888+Ravencodess@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:18:57 +0100 Subject: [PATCH 1/3] Update dev-deployment.yml Signed-off-by: Ravencodess <115118888+Ravencodess@users.noreply.github.com> --- .github/workflows/dev-deployment.yml | 31 ++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/.github/workflows/dev-deployment.yml b/.github/workflows/dev-deployment.yml index 771de8476..324533896 100644 --- a/.github/workflows/dev-deployment.yml +++ b/.github/workflows/dev-deployment.yml @@ -2,7 +2,7 @@ name: Dev Deployment on: workflow_run: - workflows: ["Build-and-Upload"] + workflows: ["Buil and Upload"] types: - completed @@ -16,6 +16,27 @@ jobs: url: ${{ vars.URL }} steps: + - name: Download build artifact + uses: actions/download-artifact@v4 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + name: boilerplate-build + path: . + + - name: Decode and create .env file + run: | + echo ${{ secrets.ENV_BASE64 }} | base64 -d > .env + + - name: Copy Artifacts to server + uses: appleboy/scp-action@v0.1.7 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: ".env,boilerplate.tar.gz" + target: "~/hng_boilerplate_nextjs" + - name: Deploy on server uses: appleboy/ssh-action@master with: @@ -23,6 +44,8 @@ jobs: username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} script: | - cd hng_boilerplate_nextjs - chmod +x ./scripts/*.sh - ./scripts/dev_deploy.sh + cd ~/hng_boilerplate_nextjs + tar -xzf boilerplate.tar.gz + rm -f boilerplate.tar.gz + cp -r .next/standalone/* . + # pm2 restart nextjs_boilerplate --update-env From 4427cb9f31044f5a8ddb943260bb81521e89a568 Mon Sep 17 00:00:00 2001 From: Ravencodess <115118888+Ravencodess@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:33:00 +0100 Subject: [PATCH 2/3] Update staging-deployment.yml Signed-off-by: Ravencodess <115118888+Ravencodess@users.noreply.github.com> --- .github/workflows/staging-deployment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/staging-deployment.yml b/.github/workflows/staging-deployment.yml index 61efbe1dd..2f0d73f60 100644 --- a/.github/workflows/staging-deployment.yml +++ b/.github/workflows/staging-deployment.yml @@ -74,4 +74,4 @@ jobs: tar -xzf nextjs-staging.tar.gz rm -f nextjs-staging.tar.gz cp -r .next/standalone/* . - pm2 restart boilerplate-staging --update-env + pm2 restart nextjs_boilerplate --update-env From e053056f42c60cbba4a8eb2d1e1b1b64c5237a49 Mon Sep 17 00:00:00 2001 From: Ravencodess <115118888+Ravencodess@users.noreply.github.com> Date: Wed, 21 Aug 2024 22:33:29 +0100 Subject: [PATCH 3/3] Update production-deployment.yml Signed-off-by: Ravencodess <115118888+Ravencodess@users.noreply.github.com> --- .github/workflows/production-deployment.yml | 108 ++++++++++---------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/.github/workflows/production-deployment.yml b/.github/workflows/production-deployment.yml index 61bc3a7ee..d08b9cf09 100644 --- a/.github/workflows/production-deployment.yml +++ b/.github/workflows/production-deployment.yml @@ -11,58 +11,62 @@ on: jobs: build: runs-on: ubuntu-latest + steps: - - uses: actions/checkout@v2 - - - name: Build Docker image - run: docker build -t hngdevops/nextjs-boilerplate:prod -f docker/prod/Dockerfile . - - - name: Save and compress Docker image - run: | - docker save hngdevops/nextjs-boilerplate:prod > nextjs-prod.tar - gzip nextjs-prod.tar - - - name: Upload artifact - uses: actions/upload-artifact@v2 - with: - name: docker-image - path: nextjs-prod.tar.gz - - deploy_to_prod: - if: github.event.repository.fork == false - runs-on: ubuntu-latest + - uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" - environment: - name: "prod" - url: ${{ vars.URL }} + - name: Cache pnpm modules + uses: actions/cache@v3 + with: + path: ~/.pnpm-store + key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm- - steps: - - name: Download artifact - uses: actions/download-artifact@v2 - with: - name: docker-image - path: . - - - name: Copy to server - uses: appleboy/scp-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - source: "nextjs-prod.tar.gz" - target: "/tmp/nextjs" - - - name: Deploy on server - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - script: | - cd /tmp/nextjs - gunzip nextjs-prod.tar.gz - docker load < nextjs-prod.tar - rm -f nextjs-prod.tar - cd /home/${{ secrets.USERNAME }}/hng_boilerplate_nextjs - chmod +x ./scripts/*.sh - ./scripts/prod_deploy.sh + - name: Install pnpm + uses: pnpm/action-setup@v4 + with: + version: 9 + + - name: Decode and create .env file + run: | + echo ${{ secrets.ENV_BASE64 }} | base64 -d > .env + + - name: Install dependencies + run: pnpm install + + - name: Build Next.js application + run: pnpm build + + - name: Archive production artifacts + run: tar -czf nextjs-prod.tar.gz .next public .env + + - name: Copy to server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + source: "nextjs-prod.tar.gz" + target: "~/hng_boilerplate_nextjs" + + - name: Delete zip file + run: rm -f nextjs-prod.tar.gz + + - name: Deploy on server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + password: ${{ secrets.PASSWORD }} + script: | + cd ~/hng_boilerplate_nextjs + tar -xzf nextjs-prod.tar.gz + rm -f nextjs-prod.tar.gz + cp -r .next/standalone/* . + pm2 restart nextjs_boilerplateg --update-env