You often needs to execute shell scripts on the remote machine if you are using Jenkins to do CD to a remote server from your central server. Sometimes you must prefix your command with
sudo in order to complete your deployment, here the problem comes,
sudo will prompt you to enter password and this is what you can't do from Jenkins.
- Add a dedicated user for Jenkins, eg:
sudo adduser USERNAME sudo usermod -aG sudo USERNAME
- From your local machine, set up private key login.
Note I'm on MacOS Mojave, this version somehow has a new public/private version, if you simply do
ssh-keygenwithout the parameters below, Jenkins will not be able to use the private key. So if you're on the latest MacOS, use the below command to generate your key.
ssh-keygen -m PEM -t rsa -b 4096 ssh-copy-id -i PUBLIC_KEY_PATH USER@HOST
- Go back to your remote host
Add a line at the END of the file.
USERNAME ALL = (ALL) NOPASSWD: ALL
USERNAMEwith your ci user name.
- Save the file.
Now Jenkins should be able to run
sudo command smoothly.
Comments are welcomed!