I'm new to Jenkins scripted pipeline. Below is the code which i'm trying to execute on a remote host. I want to know, two things:
1) How to pass credentials without hard coding it. Unlike the way i did in the script below.
2) How can i pass a parameter to my test.sh script. Meaning I want to pass it as
sshScript remote: remote, script: "myscript.sh {version} "
Update:
Below, is the script I got:
node {
properties([
parameters([
string(name: 'version', defaultValue: '', description: 'Enter the version in x.y.z format')
])
])
version = params.version.trim()
def remote = [:]
remote.name = 'Filetransfer'
remote.host = 'X.X.XX.XXXX'
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: 'saltmaster', passwordVariable: 'password', usernameVariable: 'ops')]) {
remote.user = ops
remote.password = password
stage('Filetransfer') {
sshCommand remote: remote, command: "hostname"
//sshCommand remote: remote, command: "whoami"
sshGet remote: remote, from: '/srv/salt/tm-server/files/docker-compose.yaml', into: '/home/jenkins/jenkins-data/docker-compose.yaml', override: true
//sshScript remote: remote, script: '/home/jenkins/jenkins-data/rebuilt_dockercompose.sh "${version}"'
}
sh 'echo "Executing the script now ..."'
sh "echo Current version: ${version}"
sh "/home/jenkins/jenkins-data/rebuilt_dockercompose.sh //"${version}//""
}
}
Here is what you can do:
Pass credentials without hard-coding
withCredentials([sshUserPrivateKey...])
directive from Credential Binding Plugin to pass the credentials. This will also mask the credentials in the console output.Pass user defined parameters
parameters([string...])
block wrapped inside the properties([])
block to allow users enter the version as a string parameter .Modified pipeline script
node {
properties([
parameters([
string(name: 'version', defaultValue: '', description: 'Enter the version in x.y.z format')
])
])
def remote = [:]
remote.name = 'testPlugin'
remote.host = 'x.x.x.x'
remote.allowAnyHosts = true
withCredentials([
sshUserPrivateKey(credentialsId: 'ssh-credentials', usernameVariable: 'ssh-user', passphraseVariable: 'ssh-pass')
]) {
remote.user = ssh-user
remote.password = ssh-pass
stage('testPlugin') {
sshPut remote: remote, from: 'myscript.sh', into: '.'
sshScript remote: remote, script: "myscript.sh ${version}"
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.