简体   繁体   中英

Pass a value to a shell script from jenkins pipeline

How to pass values to a shell script from jenkins during the runtime of the pipeline job. I have a shell script and want to pass the values dynamically.

#!/usr/bin/env bash
....
/some code
....
export USER="" // <--- want to pass this value from pipeline
export password=""  //<---possibly as a secret

The jenkins pipeline executes the above shell script

node('abc'){
    stage('build'){
      sh "cd .."
      sh "./script.sh"
    }
}

You can do something like the following:

pipeline {
    
    agent any
    environment {
        USER_PASS_CREDS = credentials('user-pass')
    }

    stages {
        stage('build') {
            steps {
                sh "cd .."
                sh('./script.sh ${USER_PASS_CREDS_USR} ${USER_PASS_CREDS_PSW}')
            }
        }
    }
}

The credentials is from using the Credentials API and Credentials plugin. Your other option is Credentials Binding plugin where it allows you to include credentials as part of a build step:

         stage('build with creds') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                    // available as an env variable, but will be masked if you try to print it out any which way
                    // note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
                    sh 'echo $PASSWORD'
                    // also available as a Groovy variable
                    echo USERNAME
                    // or inside double quotes for string interpolation
                    echo "username is $USERNAME"

                    sh('./script.sh $USERNAME $PASSWORD')
                }
            }
        }

Hopefully this helps.

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