簡體   English   中英

如何從 Groovy 代碼解析 Jenkinsfile

[英]How to parse Jenkinsfile from Groovy code

我有一個帶有一些屬性的 Jenkinsfile,例如

trace = false

userNotifications = [
    build_master :                 [name : 'name',
                                    email : 'email',
                                    slackid: 'slack id',
                                    slackchannel: 'slack channel']
]
env.aProperty = "aValue"
node('COMPILE')
{
...
}

我想解析 groovy 代碼中的上述 Jenkinsfile 以訪問一些屬性值。

當我像這樣使用 GroovyShell

        Binding binding = new Binding()
        GroovyShell shell = new GroovyShell(binding)
        Object groovyDsl = shell.evaluate(clean)

我收到這個錯誤

groovy.lang.MissingMethodException: No signature of method: Script1.node() is applicable for argument types: (String, Script1$_run_closure1) values: [COMPILE, Script1$_run_closure1@7d804e7]

我可能能夠通過一些 Groovy 元編程來解決特定錯誤,但是,我不確定這是否是正確的方向。 我的問題是在 Groovy 代碼中解析 Jenkinsfile 的最佳方法是什么? 歸根結底,這是 Groovy DSL,我希望它更簡單。

DSL 是特定於上下文的,不能脫離上下文。

你可以試試

  • 管理 Jenkins 下的腳本控制台。

這是我最終做我想做的事的方式:

import org.codehaus.groovy.control.CompilerConfiguration

class JenkinsfileParser {
    static void main(String[] args) {
        LinkedHashMap vars = JenkinsfileParser.parse('Jenkinsfile' as File)
        println vars.toString()
    }

    static LinkedHashMap parse(File jenkinsfile) {
        CompilerConfiguration config = new CompilerConfiguration()
        config.scriptBaseClass = JenkinsfileBaseClass.class.name
        Binding binding = new Binding()
        GroovyShell shell = new GroovyShell(this.class.classLoader, binding, config)
        String clean = jenkinsfile.text.replace('import hudson.model.*', '')
                .replace('import hudson.EnvVars', '')
        Script script = shell.parse(clean)
        script.run()
        return binding.variables
    }

    static abstract class JenkinsfileBaseClass extends Script {
        LinkedHashMap<String, Closure> nodeClosures = new LinkedHashMap<>()
        LinkedHashMap env = new LinkedHashMap()
        CurrentBuild currentBuild = new CurrentBuild()
        LinkedHashMap parallelClosures = new LinkedHashMap<>()

        void node(String name, Closure closure) {
            nodeClosures.put(name, closure)
        }

        void parallel(LinkedHashMap closures) {
            parallelClosures.putAll(closures)
        }

        class CurrentBuild {
            List<String> expectedResults = new ArrayList<>()

            boolean resultIsBetterOrEqualTo(String expected) {
                expectedResults << expected
                return true
            }
        }
    }
}

JenkinsfileBaseClass特定於我的 Jenkinsfile,需要針對不同的 Jenkinsfile 進行調整。

相關文檔可以在這里找到: https://groovy-lang.org/integrating.html

I tried using the implementation group: 'org.jenkins-ci.main', name: 'jenkins-core', version: '2.9' package from the maven repository http://repo.jenkins-ci.org/public/ ,但是,那里似乎對我的情況沒有任何用處。

讓我知道是否有更好或更優雅的方法。

暫無
暫無

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

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