I have below .gitlab-ci.yml file for gitlab repository hosted on gitlab.com to build and deploy vue app using gitlab CI/CD:
image: node:latest
cache:
paths:
- node_modules/
before_script:
- apt-get update -qq && apt-get install -y -qq sshpass
build_deploy_stage:
stage: build
environment: Staging
only:
- master
script:
- rm ./package-lock.json
- npm install
- node -v
- npm run build
- ls
- sshpass -V
- export SSHPASS=$USER_PASS
- sshpass -e scp -o stricthostkeychecking=no -r dist user@example.com:~/myapp
After deployment I want to run below commmand as if I am remote logged into it (ssh user@example.com) :
$ pm2 restart myapp
$ cp ~/myfiles/file1 ~/backup/myfiles/file1.bk
How can I add above command to .gitlab-ci.yml ?
Recommended way is to use ssh with public private keys instead of sshpass. What is the solution using ssh key pairs and log into remote host (example.com) and run commands on the remote host from within gitlab CI/CD runner?