Capistrano Deployment from Jenkins

Recently I had a challenge of getting capistrano deployments working from jenkins installed on a RHEL box. The problem seemed to be down to the fact that ssh-agent isn’t running for the daemon process that jenkins runs as for whatever reason and it is needed to do ssh agent forwarding (which is what I do so I can use the ssh key on the jenkins server to check code out from github on remote servers).

After banging my head against the head for a couple hours, I came up with a simple solution: execute a shell script through ssh-agent. I simply created a deploy.sh script in the root of my project with the following contents:

Then execute it through ssh agent.

exec /usr/bin/ssh-agent "${WORSKPACE}/deploy.sh" "production"

Works perfectly. Hopefully this helps someone else, took me a lot of grief to figure this out.

Oh yeah, in case you didn’t know, I’m back in PHP development again for awhile so there should be some interesting posts ahead. :)

  • http://grrr.nl David Spreekmeester

    James: thanks a BUNCH! Really. I was risking a concussion from the head-versus-screen-banging.

    I would expect that Jenkins + Capistrano isn’t a really exotic combination, but somehow it took me a long time to find this post, or anything related that was helpful. I had tried configuring .ssh/config in a few different places, but that’s obviously no good.

    Only thing to add is that there’s a typo in ${WORKSPACE}, for the rest this was a flawless victory!

  • http://blog.james-carr.org James Carr

    I’m glad this helped! I spent an entire week on this issue and it was a nightmare. :)

  • Catalin Banu

    Where from do you take the code when deploy? From versioning server or from jenkins workspace? Wen deploying I would need to get css and js files that are minimiezed by jenkins build jobs

    • http://blog.james-carr.org James Carr

      The code comes from the VCS defined in the capistrano configuration (in this case, the master branch of my project in git).

    • carrja99

      Sorry for the late reply, but in that case I’d actually add some cap tasks to push those artifacts to a shared folder and link them when the deploy checks the project out.