Skip to content

Instantly share code, notes, and snippets.

@zazk
Last active May 25, 2020 01:24
Show Gist options
  • Save zazk/574b0e1345e5c13ce17ffada3af44dc5 to your computer and use it in GitHub Desktop.
Save zazk/574b0e1345e5c13ce17ffada3af44dc5 to your computer and use it in GitHub Desktop.
GitHub Actions NodejS
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
# This project uses PM2 to run the Node.js App https://pm2.keymetrics.io/ and stop and start de server to apply the changes after pull the repository.
# This project use default Ubuntu Digital Ocean Droplet but it can apply to similar environments.
name: Node.js CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
deploy:
if: github.ref == 'refs/heads/master'
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout@v1
- name: Push to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
script: |
cd ${{ secrets.PROJECT_PATH }}
git pull
npm i
pm2 stop recetasapp
pm2 start recetasapp

Deploy to Digital Ocean Droplet

This guide use https://github.com/appleboy/ssh-action to deploy this repository, you can modify the commands as you want.

In the Github Repository

  1. Create a /.github/workflows/deployment.yml
  2. Setup in Github Secrets:
  • SERVER_IP: Server IP Address
  • SERVER_USERNAME: Username
  • SERVER_PASSWORD: Password
  • PROJECT_PATH: Project path
  1. Review de Actions tabs each time the deploy runs.

In the Server

  1. Clone the repository with the user you wanna to use the app. This user should be the same Github Action will use each time the pipeline runs.
  2. Setup PM2 and run the NodeJs project: Example: pm2 start src/app.js --recetasapp
  3. Be sure the app is running. List running apps: pm2 ls

Trubbleshooting

  • Be sure the Github user has access and pass between restarts to the repository: ssh -T [email protected] should print the user.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment