栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

模拟詹金斯管道步骤

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模拟詹金斯管道步骤

前一周我遇到了类似的问题,我想到了:

import org.jenkinsci.plugins.workflow.cps.Cpsscriptdef mockCpsscript() {    return [        'sh': { arg -> def script def returnStdout // depending on sh is called arg is either a map or a string vector with arguments if (arg.length == 1 && arg[0] instanceof Map) {     script = arg[0]['script']     returnStdout = arg[0]['returnStdout'] } else {     script = arg[0] } println "Calling sh with script: ${script}"        },        'script' : { arg ->   arg[0]()        },    ] as Cpsscript}

并与您的脚本一起使用(以未命名的sh调用扩展):

class TestBuild {    def build(jenkins) {        jenkins.script { jenkins.sh(returnStdout: true, script: "echo build") jenkins.sh("echo no named arguments")        }    }}def obj = new TestBuild()obj.build(mockCpsscript())

它输出:

[Pipeline] echoCalling sh with script: echo build[Pipeline] echoCalling sh with script: echo no named arguments

现在,它本身并不是很有用,但是很容易添加定义模拟方法行为的逻辑,例如,此版本根据要读取的目录和文件来控制readFile返回的内容:

import org.jenkinsci.plugins.workflow.cps.Cpsscriptdef mockCpsscript(Map<String, String> readFileMap) {    def currentDir = null    return [        'dir' : { arg -> def dir = arg[0] def subClosure = arg[1] if (currentDir != null) {     throw new IllegalStateException("Dir '${currentDir}' is already open, trying to open '${dir}'") } currentDir = dir try {     subClosure() } finally {     currentDir = null }        },        'echo': { arg -> println(arg[0])        },        'readFile' : { arg -> def file = arg[0] if (currentDir != null) {     file = currentDir + '/' + file } def contents = readFileMap[file] if (contents == null) {     throw new IllegalStateException("There is no mapped file '${file}'!") } return contents        },        'script' : { arg ->   arg[0]()        },    ] as Cpsscript}class TestBuild {    def build(jenkins) {        jenkins.script { jenkins.dir ('a') {     jenkins.echo(jenkins.readFile('some.file')) } jenkins.echo(jenkins.readFile('another.file'))        }    }}def obj = new TestBuild()obj.build(mockCpsscript(['a/some.file' : 'Contents of first file', 'another.file' : 'Some other contents']))

输出:

[Pipeline] echoContents of first file[Pipeline] echoSome other contents

如果需要使用currentBuild或类似的属性,则可以在闭包强制之后分配这些属性:

import org.jenkinsci.plugins.workflow.cps.Cpsscriptdef mockCpsscript() {    def jenkins = [        // same as above    ] as Cpsscript    jenkins.currentBuild = [        // Add attributes you need here. E.g. result:        result:null,    ]    return jenkins}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号