groovy 在jenkins管道阶段之间访问变量

2hh7jdfx  于 2023-05-06  发布在  Jenkins
关注(0)|答案(1)|浏览(148)

希望从Jenkins管道的前一阶段访问变量,导致以下错误。
尝试解析文件的内容(第一阶段的示例格式)。

def os_type
pipeline {
    agent {label 'jenLab'}
        stage('stage 1') {
            steps {
                sh '''
                echo "ver=1.3.0" > ${WORKSPACE}/some_file
                echo "os_type=win" > ${WORKSPACE}/some_file
                '''
        }
    }
    stage('stage 2') {
            steps {
                script{         
                    os_type = sh(
                        script: "grep "os_type" ${WORKSPACE}/some_file | awk -F "=" '{print $2}'",
                        returnStdout: true,
                    )
            }
        }
    }
    
    stage('stage 3') {
            steps {
                echo "From stage 1 : ${os_type}"
        }
    }
}

错误如下所示。如果awk需要在脚本块中以不同的方式运行,请提出建议

Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 16: unexpected token: os_type @ line 16, column 22.
                        script: "grep "os_type" ${WORKSPACE}/some_file | awk -F "=" '{print $2}'",
                        ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
    at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
    at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:144)
    at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
    at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
    at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    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:142)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
uurity8g

uurity8g1#

您需要转义sh块中的特殊字符。看看下面的管道。

def os_type
pipeline {
    agent any
    stages{
        stage('stage 1') {
            steps {
                sh '''
                echo "ver=1.3.0" > ${WORKSPACE}/some_file
                echo "os_type=win" > ${WORKSPACE}/some_file
                '''
            }
        }
        stage('stage 2') {
                steps {
                    script{         
                        os_type = sh(
                            script: "grep \"os_type\" ${WORKSPACE}/some_file | awk -F \"=\" '{print \$2}'",
                            returnStdout: true,
                        )
                }
            }
        }
        
        stage('stage 3') {
                steps {
                    echo "From stage 1 : ${os_type}"
            }
        }
    }

}

相关问题