Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI deployment failed: "bash: pm2: command not found" during gitlab-ci.yml run

I'm deploying a Next.JS app from GitLab directly to my remote server. I've configured a variable which contains my private SSH key in order to connect to the remote server, and I'm using rsync to copy from the docker runner to my remote server. Everything works up until the last line when I use PM2 over SSH to restart the service.

image: node:latest

stages:
  - build
  - test
  - deploy

cache:
  paths:
    - node_modules/
    - .next/

install_dependencies:
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - node_modules/
      - .next/

test-build:
  stage: test
  script:
    - npm run test

deploy_production:
  stage: deploy
  only:
    - master
  before_script:
    - "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
    - mkdir -p ~/.ssh
    - eval $(ssh-agent -s)
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - apt-get update -y
    - apt-get -y install rsync
  script:
    - ssh -p22 [email protected] "mkdir -p /var/www/example.com/index_tmp"
    - rsync -rav -e ssh --exclude='.git/' --exclude='.gitlab-ci.yml' --delete-excluded ./ [email protected]:/var/www/example.com/index_tmp
    - ssh -p22 [email protected] "mv /var/www/example.com/index /var/www/example.com/index_old && mv /var/www/example.com/index_tmp /var/www/example.com/index"
    - ssh -p22 [email protected] "rm -rf /var/www/example.com/index_old"
    - ssh -p22 [email protected] "pm2 restart landing-page"

And here are the last 8 lines or so from the task runner log:

[... rsync output ...]
sent 193,022,347 bytes  received 550,996 bytes  9,003,411.30 bytes/sec
total size is 191,108,661  speedup is 0.99
$ ssh -p22 [email protected] "mv /var/www/example.com/index /var/www/example.com/index_old && mv /var/www/example.com/index_tmp /var/www/example.com/index"
$ ssh -p22 [email protected] "rm -rf /var/www/example.com/index_old"
$ ssh -p22 [email protected] "pm2 restart landing-page"
bash: pm2: command not found
ERROR: Job failed: exit code 1

The strange part is that if I were to connect directly to the remote server and run pm2 restart landing-page this would be the result:

[email protected]:~$ pm2 restart landing-page
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [landing-page](ids: 0)
[PM2] [landing-page](0) ✓
┌──────────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬──────────┬──────┬──────────┐
│ App name     │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem      │ user │ watching │
├──────────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤
│ landing-page │ 0  │ 0.33.11 │ fork │ 18174 │ online │ 2       │ 0s     │ 0%  │ 7.6 MB   │ dev  │ disabled │
└──────────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴──────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

So at the end, I'm totally lost as to why the last command doesn't work if everything from rsync worked perfectly, because I did check, and the files were changed in the remote server.

Do you guys/gals have any idea on how to solve this? I would greatly appreciate it!

Thanks for reading, and thanks for your time.

like image 456
fillipvt Avatar asked Oct 19 '18 10:10

fillipvt


1 Answers

That's because your SSH call is directly inline. There is no .bash_aliases or .bash_rc file sourced.

To make your pm2 work, you should call it with the full path. To do so, check your pm2 location using this command directly on your remote server :

whereis pm2
# Output something like /usr/bin/pm2

Then use the full path previously given to make your SSH call :

 script:
    - ...
    - ssh -p22 [email protected] "/usr/bin/pm2 restart landing-page"
like image 172
André DS Avatar answered Nov 06 '22 16:11

André DS