[英]How to pass paramaters to bash script in a Jenkins scripted pipeline and set the credentials to remote host login
I'm new to Jenkins scripted pipeline.我是 Jenkins 脚本化管道的新手。 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. 1)如何在不进行硬编码的情况下传递凭据。 Unlike the way i did in the script below.不像我在下面的脚本中所做的那样。
2) How can i pass a parameter to my test.sh script. 2)如何将参数传递给我的 test.sh 脚本。 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.在您的管道中,使用凭据绑定插件中的withCredentials([sshUserPrivateKey...])
指令来传递凭据。 This will also mask the credentials in the console output.这也将屏蔽控制台 output 中的凭据。Pass user defined parameters传递用户定义的参数
parameters([string...])
block wrapped inside the properties([])
block to allow users enter the version as a string parameter .由于您的管道是脚本化管道,因此请使用包裹在properties([])
块中的parameters([string...])
) 块以允许用户将版本作为字符串参数输入。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}"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.