1

我是声明式管道和探索其功能的新手。我尝试使用正则表达式。以下示例是反复试验的结果。那里有更好的解决方案吗?我正在寻找一种新的闪亮的声明性管道方式,如果有的话。

我没有得到的是,为什么我必须使用script{}-Enviroment以及为什么我必须使用局部变量(def matcherX)?

工作示例,然后是第一次尝试:

工作示例:

pipeline {
    environment {
        dummyText = 'aaa123aaa'
        transfare = ''
    }
    agent any
    stages {
        stage('regex') {
            steps{
                script{
                    def matcherX = dummyText =~ /(\d+)/
                    transfare = matcherX[0][0]
                    echo transfare     // results in 123
                }
            } 
        }
    }
}

错误:没有脚本{}

/*
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 11: Expected a step @ line 11, column 21.
                       def matcherX = dummyText =~ /(\d+)/
                       ^
WorkflowScript: 12: Expected a step @ line 12, column 21.
                       transfare = matcherX[0][0]
*/

错误:使用脚本但使用全局变量

//script{
//                    transfare = dummyText =~ /(\d+)/
/*

// No line displayed where error occured???                    
an exception which occurred:
    in field org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript.script
    in object org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript@192ebed
    in field groovy.lang.Closure.delegate
    in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@31fd6bc8
    in field groovy.lang.Closure.delegate
    in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@609aa9b5
    in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@3e51c1c3
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@3e51c1c3
Caused: java.io.NotSerializableException: java.util.regex.Matcher
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
*/
4

1 回答 1

0

脚本块表明背离了纯声明性语法。理想情况下,构建(就像任何其他过程一样)可以利用纯声明式语法来避免步骤之间的隐式依赖关系。

匹配器类型的全局变量无法序列化,这是在 Jenkins 主机和从机之间传递它所需的(我猜)。

于 2018-05-17T20:51:37.850 回答