Skip to content

Instantly share code, notes, and snippets.

@arehmandev
Created January 30, 2019 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arehmandev/2c705ad22c1d16844b72ba9f5e579778 to your computer and use it in GitHub Desktop.
Save arehmandev/2c705ad22c1d16844b72ba9f5e579778 to your computer and use it in GitHub Desktop.
Jenkins pipeline fail at ssh-add
Hi if you're seeing this - you probably got stuck where I was:
The pipeline randomly fails at ssh-add?
Guess what the fix is - add a new line at the end of your ssh key stored in jenkins, absolutely stupid right.
Here's an example of pipeline:
def tagBuild(gitTag){
// Required for setup of jenkins slave ssh daemon
tryAddKnownHost('github.com')
sshagent (credentials: ['arehmandev-git']) {
sh("git config --global user.email \"arehmandev@outlook.com\" && git config --global user.name \"arehmandev\"")
sh("git tag -a ${gitTag} -m \"[JENKINS] Deployment successful of tag ${gitTag}\" ")
sh("git push -u origin --tags")
}
}
void tryAddKnownHost(String hostUrl){
// ssh-keygen -F ${hostUrl} will fail (in bash that means status code != 0) if ${hostUrl} is not yet a known host
def statusCode = sh script:"ssh-keygen -F ${hostUrl}", returnStatus:true
if(statusCode != 0){
sh "mkdir -p ~/.ssh"
sh "ssh-keyscan ${hostUrl} >> ~/.ssh/known_hosts"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment