groovy 将值从jenkins管道传递到shell脚本

z4iuyo4d  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(219)

如何在管道作业运行时将值从jenkins传递到shell脚本。我有一个shell脚本,想动态传递值。


# !/usr/bin/env bash

....
/some code
....
export USER="" // <--- want to pass this value from pipeline
export password=""  //<---possibly as a secret

jenkins管道执行上面的shell脚本

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

atmip9wb1#

您可以执行以下操作:

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}')
            }
        }
    }
}

credentials是通过使用凭据API和Credentials插件实现的。您的另一个选项是Credentials Binding插件,它允许您将凭据作为构建步骤的一部分:

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')
                }
            }
        }

希望这能帮上忙。

相关问题