Using Jenkins to run remote deployment scripts over SSH

We use Jenkins to deploy code to multiple servers, so that we can manage builds and deployments from the same (even better if you’re using the Jenkins IRC plugin).

The deployment is done by a parameterized build job, where the parameter is the version of the project that we want to deploy. The job will run remote commands over ssh on servers that you’ve defined in the Jenkins configuration. Those commands will pull down a version of our code, unpack it, and run the rest of the install steps.


First you’ll need to install the Publish over SSH Plugin, which will allow files to be transferred to your servers and remote commands to be run.

Set up the SSH key for remote access of your target servers, in the Manage Jenkins page:

and setup the definitions for each of the servers that you want to deploy to:

Then in the configuration for the new deployment job you’ve set up, you’ll use the “Send files or execute commands over SSH before the build starts” settings in the “Build Environment” section to remotely execute a script to carry out the install steps on each remote server:

Notice that the build parameter “$version” is available to the Exec command that gets remotely executed – other Jenkins environment variables will also be available (e.g. $BUILD_NUMBER, $JOB_NAME etc).

Use the “Add Server” button to add more target servers, with the same Exec command.

Now you can deploy your project (or run any other remote scripts) by running the build job and specifying a version number.


25 thoughts on “Using Jenkins to run remote deployment scripts over SSH”

  1. Pingback: Using Jenkins to run remote deployment scripts over SSH ยป Julian Higman’s Blog | tksystem

  2. Hi,

    I have tried using multiple commands separated by space and newline in Exec text area but its not working.. can you give the exact syntax in the form of text rather than image.

  3. I don’t have that build server any more, so I can’t pull the exact text out – but there wasn’t anything special about it, as far as I can remember.

    What’s the error when you try text in the Exec section?

  4. Hey Anonymous…usually you need to separate multiple commands with a semicolon. For example cd foo; Try that out.

  5. Hi,

    Does anyone know why the connection is keep failing?
    I entered hostnames, login name and user password, or path to key/key then click Test Configuration tab, the errors occur as below for 2 different servers in the Build environment section:
    “Connected, but failed to setup SFTP – check the SSH server. Exec commands should work, but transferring files will fail
    jenkins.plugins.publish_over_ssh.BapSshSftpSetupException: Failed to connect SFTP channel. Message [failed to send channel request]”

    Failed to connect or change directory
    jenkins.plugins.publish_over.BapPublisherException: Failed to connect session for config [QA-Weblogic-server]. Message [Auth fail]

    Any idea how to connect?

  6. I’m confused about which key to fulfill in the key box.
    I’ll deploy a zip file to multiple servers ,
    Shall I generate the SSH key from each computer (Linux ) and then paste the private key in the jenkins key box?

  7. @lawrence — no, don’t generate multiple keys. generate a keypair on the (Jenkins) machine you’re sending files *from*, then add the resulting public key ( for example) into the .ssh/authorized_keys file of the user that’s receiving the files on your remote machines.

    in the publish over ssh config, you’ll enter the path to your private key on your jenkins host, not the remote hosts. if your Jenkins user doesn’t have a keypair, you can generate one using ‘ssh-keygen’ command and be sure to _not_ enter in a passphrase.

    when your job execs, SSH will use this path to your private key (for ex, /var/lib/jenkins/.ssh/id_rsa) when negotiating with the remote host and it will match it against the remote host’s .ssh/authorized_keys file entry that you made.

  8. Hi,
    Can anyone tell me how i can configure jenkins to setup a interactive shell script which at run will ask for deployment server password and the level for which it want to deploy. Please suggest. Thanks in Advance

  9. i don’t know if this theme is still interesting but you can use something like that

    ssh user@server bash -c “‘
    echo “$MY_NAME is starting a skript:”

  10. Hey Julian, it’s Luke from your Talis days. Casting around the web for a decent ssh plugin for jenkins and i stumbled across your blog. You’re a diamond ๐Ÿ™‚ Good work

  11. I hope you changed your private key ! Next time, put at least a full black rectangle in a significant part. I say that for you ๐Ÿ™‚

  12. My build is

    1) All above files need to push my linux server

    how to do in jenkins

  13. Hi All,

    New to jenkins, could u some one please tell me how to configure Jenkin job to execute shell script which is present in SVN.

    It’ll be a great help

    thank you !!

  14. Hello everyone..!!

    I need help in transferring files from jenkins workspace to my dev server.
    I have configure Publish over ssh plugin, also done the global settings configuration. Output of the build is success. But I cannot see any files into my dev-server.
    I can see tranfer file (22)s, but I can not see any file into that folder.

  15. I have installed Jenkins on my local, I have created my own EC2 instance, I can ssh into my instance and run some shell scripts to shut down my Wildfly server installed on my instance.

    This is what I do when I do it manually on my Mac.

    open my mac terminal, type

    ssh -i /Users/xxx/tools/xxxx.pem

    It will login to my Instance, and then I type:

    cd /srv/wildfly-10.1.0.Final/bin

    sudo -s

    source /etc/profile

    ./ –connect command=:shutdown

    The screen will output

    {“outcome” => “success”}

    Now, I want to using Jenkins, when I click build button, it will ssh into that instance and run these shell scripts for me. The output is expected the same as I run it after I ssh into the instance.

    My question is: what steps should I follow, after I login to my Jenkins local environment: localhost:8080

    Create a New Item, which one? Is there some plugin I can use? Where to put my shell scripts, will it run successfully?

    A guide would be helpful, thanks a lot!

  16. Hi,

    I want to run a script in multiple hosts (which are not Jenkins slaves) in parallel using SSH plugin.

    How to achieve that?

  17. Hi,
    Using the Jenkins plugin SSH pubblisher I want transfer to the server only the set of files that are more recent than those prent on the server.

    There is a way?
    Thank you.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.