Skip to content

Github Actions

Node App - Build and Deploy example

YAML
name: Deploy on dev server

on:
  push:
    branches: [ "main" ]

jobs:
  build:
    name: Build and install
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [ 16.x ]

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: 'echo "$DEV_ENV" > .env'
        shell: bash
        env:
          DEV_ENV: ${{ secrets.DEV_ENV }}
      - name: Install packages
        run: npm i
      - name: Build
        run: npm run build
      - name: Run migration
        run: npm run prod:migrate
      - run: mv .env .env.dev
      - run: rm -rf node_modules/
      - run: rm -rf .git/
      - name: rsync files to server
        uses: burnett01/[email protected]
        with:
          switches: -avzr --delete --include=".env"
          path: ./
          remote_path: ${{ secrets.DEPLOY_PATH }}
          remote_host: ${{ secrets.HOST }}
          remote_port: ${{ secrets.PORT }}
          remote_user: ${{ secrets.USERNAME }}
          remote_key: ${{ secrets.SSH_KEY }}

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: multiple command
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            /home/ubuntu/script.sh