简体   繁体   中英

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. 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

  1. In your Jenkins instance, add a global credential of type SSH Username with private key .
  2. In your pipeline, use the 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

  1. Since yours is a scripted pipeline, use the parameters([string...]) block wrapped inside the properties([]) block to allow users enter the version as a string parameter .
  2. Pass the parameter as an argument to your shell script.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM