簡體   English   中英

如何在Groovy中將Jenkins DSL管道命令設置為變量?

[英]How to set Jenkins DSL pipeline commands to variable in groovy?

我正在嘗試將某些DSL插件命令的輸出設置為env。 Jenkins DSL中的變量,沒有運氣。

我嘗試模仿以下將sh腳本輸出設置為變量的方法:

env.BUILD_NUM = sh([script: "get_build_number_from_s3 ${env.TARGET_ENV}", returnStdout: true]).trim()

我想做的第一個是git模塊。

我將命令設置如下:

env.DEVOPS_REPO_CLONE = git([branch: "development", credentialsId: 'bitbucket', url: 'https://bitbucket.org/team/repo.git'])
sh 'ls -lah'

但我收到以下錯誤:

WorkflowScript: 119: Expected a step @ line 119, column 13.
               env.DEVOPS_REPO_CLONE = git([branch: "development", credentialsId: 'bitbucket', url: 'https://bitbucket.org/team/repo.git'])
               ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:133)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:559)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:520)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:319)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

基本上,我試圖將命令的輸出設置為變量,這樣,我只能在將DEBUG標志設置為對開發人員抽象無關的輸出時顯示輸出,這樣他們就不必解析文本頁面。

感謝您的投入。

似乎您需要在environment {...}script { ... }范圍內創建這些變量。 例:

pipeline {
    environment {
        FOO = sh (
                script: 'pwd',
                returnStdout: true
              ).trim()
    }

    agent { label "master" }
    stages {
        stage("first") {
            steps {
                script {
                    BAR = sh (
                            script: 'ls',
                            returnStdout: true
                          ).trim()
                }
                sh "echo ${FOO}"
                sh "echo ${BAR}"
            }
        }
    }
}

您可以使用returnStdoutreturnStatus ,因為它描述的選項shell腳本在這里這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM