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.

 

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

  1. Anonymous

    May 7, 2013 at 12:00pm

    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.

  2. jhigman

    May 7, 2013 at 12:09pm

    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 fas as I can remember.

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

  3. Spajus

    May 8, 2013 at 9:15am

    Or you could just exec “ssh server /path/to/deploy_script.sh” without any extra plugins.

  4. David

    May 16, 2013 at 5:51am

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

Leave a Reply

Your email will not be published. Name and Email fields are required.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>