I have the following YAML file. On every push to the branch, it should pull the latest code, build, and restart the service. It is running successfully on every push but when I SSH into the server, the changes are not pulled. When I run git pull origin dev
, only then the changes are pulled. I have the similar YAML for another repository in the same organization for the frontend, and it is working fine for that.
name: Node.js CI
on:
push:
branches: [ "dev" ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [22.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: cd /home/ubuntu/<project-name>-dev/<projec-name>-backend
- run: git pull origin dev
- name: Save environment variables
run:
echo "PORT=4000" >> .env &&
echo "SOCKET_PORT=4002" >> .env
- name: Test environment variables
run:
cat .env
- run: npm ci
- run: CI='' && npm run build
- run: sudo systemctl restart <project-name>.service
Additional Information:
I have two repositories in the organization (for frontend and backend). Both deployed on EC2. Initially, had only one action and one runner for frontend (main branch). Now, using the same runner, I have added 3 more actions (frontend dev, backend main, backend dev) all of which are showing the above mentioned problematic behavior.
I was expecting to:
- Action running on every push to
dev
(working) - Changes should be pulled on the server (not working)
- Project should be built and service should be restarted (not working)
Ammar Khalid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Many things could be wrong here so here are my suggestions to better narrow down the issue.
- If you want the project in a specific path, checkout to it directly using the checkout action.
- Specify
working-directory
for subsequent steps where applicable.
You should also note that cd
in one step does not affect subsequent steps. Each step is a fresh shell invocation.
Here’s how I’d write this:
name: Node.js CI
on:
push:
branches: [ "dev" ]
env:
PROJECT_DIR: /home/ubuntu/<project-name>-dev/<projec-name>-backend
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [22.x]
steps:
- uses: actions/checkout@v4
with:
path: ${{ env.PROJECT_DIR }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Save environment variables
run: |
echo "PORT=4000" >> .env &&
echo "SOCKET_PORT=4002" >> .env
working-directory: ${{ env.PROJECT_DIR }}
- name: Test environment variables
run:
cat .env
working-directory: ${{ env.PROJECT_DIR }}
- run: npm ci
working-directory: ${{ env.PROJECT_DIR }}
- run: CI='' && npm run build
working-directory: ${{ env.PROJECT_DIR }}
- run: sudo systemctl restart <project-name>.service
working-directory: ${{ env.PROJECT_DIR }}