Jenkins execute script over ssh

logo-title

To execute a shell script with jenkins into a remote server it’s very easy task, you can use a sshPublisher plugin or configure manually, in this entry I’m going to show how to do it using ssh key pair.

To avoid login prompt in command line, setup a ssh keys in your jenkins instance and copy the public key to the remote server.

Setup the autentication

1. Create a ssh key pair in your jenkins server, login into the jenkins server with the user that is currently running the application server and execute the ssh-keygen command:

sudo su -s /bin/bash jenkins
ssh-keygen -t rsa

Leave the options by default to create a file ~/.ssh/id_rsa, but take care if you have a previous one, this will be overwriten. Or you can set another name for the key pair, I’m going to use id_dummy without passhrase.

Output:

bash-4.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): /var/lib/jenkins/.ssh/id_dummy
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/jenkins/.ssh/id_dummy.
Your public key has been saved in /var/lib/jenkins/.ssh/id_dummy.pub.

Note: The passhrase is an aditional method to keep the keys safe in case that your lost it.

2. Copy the public key to remote server using the command ssh-copy-id

ssh-copy-id -i ~/.ssh/id_dummy username@remote_host

If the command it’s not available you can do it manually with this:

cat ~/.ssh/id_dummy.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"

3. Enable the key authentication into remote server, edit the file /etc/ssh/sshd_config and ensure it has «PubkeyAuthentication yes«, save it and restart the sshd service, in my case I’m using a RHEL server:

sudo systemctl restart sshd

4. Test the connection:

#use this if the pub key is located into ~/.ssh
ssh username@remote_host 

# if the key is located in other path
ssh -i /var/lib/jenkins/.ssh/id_dummy username@remote_host

If the prompt user still appering check this entry to get more detail or you can check this post to get more help.

Setup Jenkins

Ok, then let’s check out how to configure the jenkins server with and without the plugin.

Using the plugin sshPubliser

1. Install the plugin, go to Manage Jenkins > Manage Plugins > Available, check Publish Over SSH and select install witouth restart.

2. Configure ssh key into Jenkins, go to Manage Jenkins > Configure System > Publish over SSH, select the Add button and Advanced to set configuration to the remote server. Complete the fileds name, username, hostname, remote directory, port, check the option Use password authentication, or use a different key and set the path to the private key file.

At the end click Test Configuration to valitade if everything is ok.

3. Create a new freestyle jenkins job, select New Task > freestyle jenkins job, add a build step and select  Execute shell, in this example I’m going to create a zip file and extract into the remote server.
2019-06-11 18_52_04-ID_EPH_GCBANK_DUCORJBOSS3 Config [Jenkins] - Brave

In the command text field add:

echo hello >> file1.txt
echo hello >> file2.txt
zip -r one.zip file1.txt file2.txt

4. Add another build step and select Send files or execute commands over SSH, in Sources files add the files will be transfered, set the remote directory, and the commands to be executed:

cd out
ls -ltr 
unzip -o -d ./ one.zip
date

Save the changes and execute the job.

Configure a job without plugin

1. Create a new freestyle jenkins job, in this example I’m going to copy files from other jenkins job and execute some commands into the remote server. To do that you need to indicate the path to take the files.

In build section add a build step and select Copy artifacts from another project, set the path to take the file (can be relative or absolute), and check the option Flatten directories
2019-06-11 19_16_13-ID_EPH_GCBANK_DUCORJBOSS3 Config [Jenkins] - Brave
Add a build step and select  Execute shell
2019-06-11 18_52_04-ID_EPH_GCBANK_DUCORJBOSS3 Config [Jenkins] - Brave

Add the next commands, explained by self, but basically I’m using it to copy a zip and unpackage over remote server:

scp /path/to/my-bin.zip username@remotehost:/remote_home/destination/path && ssh username@remotehost << 'ENDSSH'
ls -lah --block-size=K /remote_home/path/to/bin/*.jar
unzip -o -d path/to/bin  path/to/bin/my-bin.zip 
rm path/to/bin/my-bin.zip 
chmod +x  path/to/bin/run.sh 
ls -lah --block-size=K /remote_home/path/to/bin/*.jar 
ENDSSH

note: All commands into the ENDSSH section will be executed into the remote server.

Save the changes and execute the job.

That’s all, bye. =)

References

2 comentarios en “Jenkins execute script over ssh

Deja un comentario

Este sitio utiliza Akismet para reducir el spam. Conoce cómo se procesan los datos de tus comentarios.