Jenkins with Publish over SSH

If you are not interested in Jenkins slaves, you can run remote commands and transfer files over SSH. Of course, it only works if you can connect over SSH to that remote server. You can use this practice even if you run commands on localhost, with another user.

Jenkins installation is pretty straightforward. If you manage packets with yum, you can install Jenkins with:


After that, you can start Jenkins server with  systemctl start jenkins  and enable it to start at boot time with systemctl enable jenkins .

When you access Jenkins in browser (it runs on 8080 port by default), you will be asked about a secret key and where you can find it. Also, it is recommended to install suggested Jenkins plugins (this package doesn’t contain Publish over SSH plugin. You have to install it by yourself).

Install Publish over SSH plugin

Open Jenkins and go to Manager Jenkins -> Manage plugins and search for Publish Over SSH plugin. You will see it as Available plugin.

Generate SSH keys pair for Jenkins user

By default, Jenkins user run with jenkins user. You can change the user of Jenkins, but it is not a good practice.

To generate a private and a public SSH key for Jenkins, you have to sudo su jenkins .  If this command doesn’t do anything, it means that Jenkins user doesn’t have a shell. You can become jenkins user with sudo su -s /bin/bash jenkins .

Generate the keys with the following command:

ssh-keygen -t rsa

Now your public key is id_rsa.pub and your private key is id_rsa file. Both files are in ~/.ssh. Jenkins home is /var/lib/jenkins.

Configure SSH in Jenkins

Go to Manage Jenkins -> Configure system.

If the plugin is installed properly, you will see Publish over SSH. Here you have to place the Jenkins private key. You can find it in /var/lib/jenkins/.ssh/id_rsa.

Jenkins private key

Now you have to add public key on the remote server, in authorized_keys file. If ~/.ssh/authorized_keys doesn’t exist, create it. Note that .ssh directory has 700 permissions and authorized_keys file has 600 permissions. Owner of these files is the owner of home directory.

SSH Server configuration

Now configure SSH server and test the configuration. In the Remote Directory field, you can write /. Test Configuration should return Success.

Continuous Integration in Jenkins

  • Start a freestyle project with a suggestive name.
  • Select Git as source code. Clone with HTTPS or SSH.
  • Specify branch you are using.
  • Now create build steps. As you noticed, a new type of build step appeared, called “Send files or execute commands over SSH”. You can send files, execute commands, or both.
  • If you want to transfer files from a SCM, use Source files textbox.

Source files: The string is a comma separated list of includes for an Ant fileset eg. ‘**/*.jar’ (see Patterns in the Ant manual).

When you want a directory from Git, use directory/**, because directory/* transfer only directory’s files, not its subdirectories.

  • If the path to your files in Git is long, remove prefix.

Default exec timeout of a build step is 120 seconds (120000 ms). Click on Advanced and modify or disable it.

  • Save and Build!

Leave a Reply

Your email address will not be published. Required fields are marked *