diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0619b81..6e7e5c9 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,66 +1,4 @@ -# name: CI/CD - -# on: -# push: -# branches: -# - "*" -# pull_request: - -# env: -# IMAGE_NAME: "project_badging" - -# jobs: -# build: -# runs-on: ubuntu-latest -# steps: -# - uses: actions/checkout@v3 -# - name: Install dependencies -# run: npm install -# - name: Lint code -# run: npm run format - -# deploy: -# needs: build -# runs-on: ubuntu-latest -# if: github.ref == 'refs/heads/main' && github.event_name == 'push' -# steps: -# - uses: actions/checkout@v3 -# - name: Build Docker image -# run: docker build -t ${{ env.IMAGE_NAME }}:${{ github.sha }} . - -# - name: Create image archive -# run: docker save ${{ env.IMAGE_NAME }}:${{ github.sha }} -o badging.tar - -# - name: Upload image archive using appleboy/scp-action -# uses: appleboy/scp-action@v0.1.4 -# with: -# host: ${{ secrets.HOST }} -# username: ${{ secrets.USERNAME }} -# password: ${{ secrets.PASSWORD }} -# source: badging.tar -# target: ~/ - -# - name: Extract and load image on droplet -# uses: appleboy/ssh-action@v0.1.10 -# with: -# host: ${{ secrets.HOST }} -# username: ${{ secrets.USERNAME }} -# password: ${{ secrets.PASSWORD }} -# script: | -# if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then -# docker stop ${{ env.IMAGE_NAME }} -# docker rm ${{ env.IMAGE_NAME }} -# fi -# docker load -i ~/badging.tar -# docker run -d \ -# -p ${{ secrets.PORT }}:${{ secrets.PORT }} \ -# --env-file /home/${{ secrets.USERNAME }}/.env \ -# --restart=always \ -# --name ${{ env.IMAGE_NAME }} \ -# ${{ env.IMAGE_NAME }}:${{ github.sha }} - - -name: Node.js CI/CD +name: CI/CD on: push: @@ -69,57 +7,109 @@ on: pull_request: env: - DEPLOY_FOLDER: "project-badging-backend" + IMAGE_NAME: "project_badging" jobs: deploy: runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' && github.event_name == 'push' - steps: - - name: Checkout repository - uses: actions/checkout@v2 + - uses: actions/checkout@v3 + - name: Build Docker image + run: docker build -t ${{ env.IMAGE_NAME }}:${{ github.sha }} . - - name: Cleanup existing content on the server - uses: appleboy/ssh-action@v0.1.10 - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - script: | - if [ -d ./${{ env.DEPLOY_FOLDER }} ]; then - cd ./${{ env.DEPLOY_FOLDER }} - find . -maxdepth 1 -type f ! -name '.env' -exec rm -f {} + - else - mkdir -p ./${{ env.DEPLOY_FOLDER }} - fi + - name: Create image archive + run: docker save ${{ env.IMAGE_NAME }}:${{ github.sha }} -o badging.tar - - name: Upload repository contents to the server + - name: Upload image archive using appleboy/scp-action uses: appleboy/scp-action@v0.1.4 with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} - source: ./ - target: ./${{ env.DEPLOY_FOLDER }}/ + source: badging.tar + target: ~/ - - name: Install npm dependencies on the server + - name: Extract and load image on droplet uses: appleboy/ssh-action@v0.1.10 with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} script: | - cd ./${{ env.DEPLOY_FOLDER }} - npm install + if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then + docker stop ${{ env.IMAGE_NAME }} + docker rm ${{ env.IMAGE_NAME }} + fi + docker load -i ~/badging.tar + docker run -d \ + -p ${{ secrets.PORT }}:${{ secrets.PORT }} \ + --env-file /home/${{ secrets.USERNAME }}/.env \ + --restart=always \ + --name ${{ env.IMAGE_NAME }} \ + ${{ env.IMAGE_NAME }}:${{ github.sha }} - - name: Deploy with PM2 - uses: appleboy/ssh-action@v0.1.10 - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - script: | - cd ./${{ env.DEPLOY_FOLDER }} - pm2 restart index.js - pm2 save --force + +# name: Node.js CI/CD + +# on: +# push: +# branches: +# - "*" +# pull_request: + +# env: +# DEPLOY_FOLDER: "project-badging-backend" + +# jobs: +# deploy: +# runs-on: ubuntu-latest +# if: github.ref == 'refs/heads/main' && github.event_name == 'push' + +# steps: +# - name: Checkout repository +# uses: actions/checkout@v2 + +# - name: Cleanup existing content on the server +# uses: appleboy/ssh-action@v0.1.10 +# with: +# host: ${{ secrets.HOST }} +# username: ${{ secrets.USERNAME }} +# password: ${{ secrets.PASSWORD }} +# script: | +# if [ -d ./${{ env.DEPLOY_FOLDER }} ]; then +# cd ./${{ env.DEPLOY_FOLDER }} +# find . -maxdepth 1 -type f ! -name '.env' -exec rm -f {} + +# else +# mkdir -p ./${{ env.DEPLOY_FOLDER }} +# fi + +# - name: Upload repository contents to the server +# uses: appleboy/scp-action@v0.1.4 +# with: +# host: ${{ secrets.HOST }} +# username: ${{ secrets.USERNAME }} +# password: ${{ secrets.PASSWORD }} +# source: ./ +# target: ./${{ env.DEPLOY_FOLDER }}/ + +# - name: Install npm dependencies on the server +# uses: appleboy/ssh-action@v0.1.10 +# with: +# host: ${{ secrets.HOST }} +# username: ${{ secrets.USERNAME }} +# password: ${{ secrets.PASSWORD }} +# script: | +# cd ./${{ env.DEPLOY_FOLDER }} +# npm install + +# - name: Deploy with PM2 +# uses: appleboy/ssh-action@v0.1.10 +# with: +# host: ${{ secrets.HOST }} +# username: ${{ secrets.USERNAME }} +# password: ${{ secrets.PASSWORD }} +# script: | +# cd ./${{ env.DEPLOY_FOLDER }} +# pm2 restart index.js +# pm2 save --force diff --git a/Dockerfile b/Dockerfile index 8f9a7d6..d2b9df4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:latest +FROM node:16 WORKDIR /app COPY package*.json ./ RUN npm install