how to use ssh credentials within workflow plugin for 'sh' step

926 views
Skip to first unread message

Yasir Saleem

unread,
May 7, 2015, 9:33:11 PM5/7/15
to jenkins...@googlegroups.com
Hi, 

  I started using workflow plugin for Jenkins (https://github.com/jenkinsci/workflow-plugin). I really like it, specially for complex jobs and pipelining the CD workflow. 

We want to use some sort of mechanism to pass ssh credentials to 'sh' step of 'workflow' to slaves the way we normally do using ssh-agent plugin (https://wiki.jenkins-ci.org/display/JENKINS/SSH+Agent+Plugin). 

We typically use Swarm slaves to attach to master. ssh keys are only present in Jenkins master, they are passed on to slaves dynamically. This really helps restricting the ssh keys to master only. 

workflow plugin does support credentials for gitSCM or general SCM class. We want same feature for other steps specially 'sh'.

 Is there any workaround or any other means to accomplish same thing?

Yasir Saleem

unread,
May 11, 2015, 1:05:13 PM5/11/15
to jenkins...@googlegroups.com
I endup using secrete file auth, with linux ssh agent. Which is not the most elegant solution, but it works!!

 public def run_shell_with_credentials(ssh_key_file,cmd) {
    env.RUN_CMD = cmd
    withCredentials([[$class: 'FileBinding', variable: 'PVT_KEY_FILE', credentialsId: ssh_key_file]]) {
      sh '''
        chmod 0500 $PVT_KEY_FILE
        env  |grep SIS  
        eval `ssh-agent`
        ssh-add $PVT_KEY_FILE
        $RUN_CMD
        kill -s term $SSH_AGENT_PID
      '''
    }
}

Thanks,
Yasir.
Reply all
Reply to author
Forward
0 new messages