Automatic Deploy With YAML (.yml) - Configuration for Strapi or Node.js in Ubuntu VPS.
When you git push
the VPS server automatically git pull
, build and pm2 can restart.
Of course you must configure SSH.
A setup that worked for me after too many attempts.
Identation is very important, any wrong identation and it will not work.
- I put strapi deploy file at the path
./strapi-backend/.github/workflows/deploy.yml
./strapi-backend/.github/workflows/deploy.yml
name: Node.js CI
on:
push:
branches:
- main # main or master
pull_request:
branches:
- main # main or master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: echo Building the app
deploy:
runs-on: ubuntu-latest
env:
TARGET_REPO: ~./strapi-backend # Change target, in my case worked as a shorthand for /home/deploy./strapi-backend
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
- name: Deploy to VPS
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.HOST }} # Use a static IP or hostname (e.g., deploy@srv562740 according to VPS login) or define variable in Github ${{ secrets.VAR_NAME }}
username: deploy # Set username as static or define variable in Github ${{secrets.VAR_NAME}}
key: ${{ secrets.SSH_KEY }} # SSH_KEY defining variable in Github ${{secrets.VAR_NAME}}
script: | # remove export and source if not using NVM
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
cd ${{ env.TARGET_REPO }}
git pull origin main
npm ci
npm run build --if-present
# pm2 restart strapi-backend
Setting up secrets and variables in Github
To easily manage node versions get NVM - Node Version Manager (opens in a new tab)
Last updated on