I would like to make my long "ssh" command reusable as a simple variable throughout my pipeline. To do this, it would be nice to declare my hostname in a var, then use that var in another var declaration to build the final command:
environment {
BUILDHOST = 'buildhost.example.com'
SSHCMD = 'ssh -o StrictHostKeyChecking=no jenkins@${env.BUILDHOST}'
}
So conceptually in something like this:
pipeline {
agent any
environment {
BUILDHOST = 'buildhost01.example.com'
SSHCMD = 'ssh -o StrictHostKeyChecking=no jenkins@${env.BUILDHOST}'
}
stages {
stage('SSH Testing') {
steps {
sshagent ( ['jenkins_ssh']) {
sh '''
$SSHCMD uname -a
'''
}
}
}
stage('Example Test') {
steps {
sshagent ( ['jenkins_ssh']) {
sh '''
$SSHCMD /run/something.sh
'''
}
}
}
}
}
It works fine when I declare the var without any substitutions:
SSHCMD = 'ssh -o StrictHostKeyChecking=no jenkins@buildhost.example.com'
but I can't seem to nest together the variables. Any way to do this?