【问题标题】:enviroment variables on jenkins pipeline for user and password詹金斯管道上用于用户和密码的环境变量
【发布时间】:2018-04-12 22:13:43
【问题描述】:

我对将 groovy 用于 jenkins 管道非常陌生。我有一个已经在运行的管道,它正在执行一些步骤,例如运行 unitest、sonnarqube anaysis 等。其中一个步骤是使用 curl -u 将工件上传到工件。我不想显示用户并传递输出脚本,所以我正在使用凭据插件,我在其中存储了用户和密码并具有 ID。但我不知道如何使用变量将其传递给 sh 命令。这就是我现在在使用 withCredentials 的那一步中所拥有的。

stage ('Upload war to Artifactory') {
            withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'willy11', passwordVariable: 'hello123')])
sh "sudo curl -u ${willy11}:{$hello123} -T $warPath 'https://artifactory.xxxxx.com:443/artifactory/Platform/$warFile'" 

我不知道如何传递或定义 usernameVariable 和 passwordVariable 的值以用于 curl 命令。现在的方式,我得到了输出脚本:

java.lang.IllegalStateException: **There is no body to invoke**
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:283)
at org.jenkinsci.plugins.workflow.cps.CpsStepContext.newBodyInvoker(CpsStepContext.java:95)

我怎样才能做到这一点?凭据插件,我读到据说将 *** 放在脚本的输出上,这是如何工作的?我应该声明 "will11" 和 "hello123" elsewere 并用作环境变量吗?

谢谢。

def call(body) {

def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

def artifactName = 'extractor'
def artifactExt = '.war'
def artifactVersion = '0.0.1'

def buildPath = 'target/'
def warFile = artifactName + '-' + artifactVersion + artifactExt
def warPath = buildPath + warFile
def warNoVersion = artifactName + artifactExt

def deployPath = '/var/lib/tomcat8/webapps/'
def deployFile = deployPath + warNoVersion

node {
    // Clean workspace before doing anything
    //deleteDir()

    try {

        stage ('Code Checkout') {
            git branch: 'master',
                credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
                url: 'ssh://git@bitbucket.org/xxxxxxx/xxxxxxxctor'
        }


stage ('Configure Application') {
            configFileProvider([configFile(fileId: config.confiFileId, variable: 'CONFIG_FILE_PATH')]) {
                sh 'cp $CONFIG_FILE_PATH resources/config.properties'
            }
            sh "echo 'job_name: $JOB_NAME' > WebContent/version.txt"
            sh "echo 'job_number: $BUILD_NUMBER' >> WebContent/version.txt"
        }

        stage ('Run Unitests') {
            sh 'mvn test'
        }

        /*stage ('SonarQube analysis') {
          withSonarQubeEnv('xxxxxxxxxxxxxxxx) {
            sh 'mvn sonar:sonar'
          }  
        }*/

        stage ('Compile and Build WAR') {
            sh 'mvn clean compile war:war'
        }

        stage ('Upload war to Artifactory') {
            withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')])
            sh "sudo curl -u ${USER}:{$PASSWORD} -T $warPath 'https://artifactory.xxxxxxx.com:443/artifactory/Platform/$warFile'" 

        }




    } catch (err) {
        notifyBuild('FAILURE', config.slackChannel)
        throw err
    }
}

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline credentials


    【解决方案1】:

    当您使用凭据绑定插件时,凭据将绑定到环境变量,并且要执行的代码必须在 withCredentials 语句的花括号内,这是我们错过的。

    所以使用:

     stage ('Upload war to Artifactory') {
           withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
               sh ("sudo curl -u $USER:$PASSWORD -T $warPath 'https://artifactory.xxxxx.com:443/artifactory/Platform/$warFile'")
           }
     }
    

    代替:

     stage ('Upload war to Artifactory') {
         withCredentials([usernamePassword(credentialsId: '7c9e8186-1f16-4920-837b-b571ea88a7e8', usernameVariable: 'USER', passwordVariable: 'PASSWORD')])
             sh "sudo curl -u ${USER}:{$PASSWORD} -T $warPath 'https://artifactory.xxxxxxx.com:443/artifactory/Platform/$warFile'" 
    
     }
    

    【讨论】:

    • 这也是我想知道的。在哪里以及如何在脚本中定义真正的 USER 和 PASSWORD 值? "willy11" "start123" 我在 jenkins globar 变量中创建了它们。他们会从那里被带走吗?
    • 必须在您引用的凭据中定义值 williy11 和 start123 ('7c9e8186-1f16-4920-837b-b571ea88a7e8')。 usernameVariable: 'USER' 将使凭证的用户名值作为 $USER 可用,然后您可以在 curl 或其他命令中使用 $USER 变量。
    • 很奇怪,直到说“java.lang.IllegalStateException:没有要调用的主体”你确定是 USER 和 PASS 要使用的变量吗?我已经在 cred ID 上设置了凭据
    • 这似乎是另一个故障点,而不是 curl 命令,您能否提供更多有关您的管道脚本的信息?
    • 我添加了脚本。所有步骤都在管道中成功执行,直到我要上传到神器战争的部分。如果我只是做 Curl -u "realuser":"realpass" 工件会成功上传,但我不想硬编码并显示它。
    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 2016-04-07
    • 2018-07-26
    • 2011-08-14
    • 2018-05-15
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多