[GITHUB] CI CD NodeJS APP using ssh action - fourslickz/notes GitHub Wiki

FIRST THING TODO:

  • SERVER DEVELOPMENT
    1. git clone -b {development-branch} {github-repository}
    2. npm install
    3. npm run build
    4. pm2 start index.js --name CI-CD-NODE
    5. pm2 save

  1. github account setting add ssh-key from server production / development

  2. repo setting

    • {SSH_PRIVATE_KEY_PRODUCTION}
    • {SSH_USERNAME_PRODUCTION}
    • {SSH_HOST_PRODUCTION}
    • {SSH_PRIVATE_KEY_DEVELOPMENT}
    • {SSH_USERNAME_DEVELOPMENT}
    • {SSH_HOST_DEVELOPMENT}
  3. setup workflow

name: Node.js CI/CD

on:
  push:
    branches: [ "main", "development" ]
  pull_request:
    branches: [ "main", "development" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js 14
      uses: actions/setup-node@v3
      with:
        node-version: 14.x
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present

  deploy-production:
    name: Deploy Project to Production Server
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js 14
        uses: actions/setup-node@v3
        with:
          node-version: 14.x
          cache: 'npm'
      - run: npm ci
      - run: npm run build --if-present
      - name: Deploy using ssh
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST_PRODUCTION }}
          username: ${{ secrets.SSH_USERNAME_PRODUCTION }}
          key: ${{ secrets.SSH_PRIVATE_KEY_PRODUCTION }}
          port: 22
          script_stop: true
          script: |
            cd ~/apps/ci-cd-node
            git pull origin main
            git status
            npm install --only=prod
            pm2 restart CI-CD-NODE