jenkins pipeline Jenkinsfile与Json的转换

前段时间调研了下青云的kubesphere,意外的发现了一个插件,pipeline-model-definition-plugin,用了将jenkins的pipeline、json互相转换的,以前可能关注过这个插件,但是没怎么注意,jenkins的plugin上面也没有找到,不知道为什么,所以,这里简单讲解一下吧。

一、安装

jenkins的plugin上面没有提供,所以只能自己打包编译,github上面也给出了打包的过程:cd pipeline-model-definition && mvn install && mvn hpi:run,然后将hpi文件上传到插件即可。

二、主要api接口

2.1 获取pipeline的json格式JSON Schema

URL: JENKINS_URL/pipeline-model-schema/json Info: 获取json格式的jenkinsfile Returns: json

2.2 将jenkinsfile转成json格式

URL: JENKINS_URL/pipeline-model-converter/toJson Parameters: jenkinsfile格式的文本 Info: 获取jenkinsfile,然后将它转换成json。 Returns: 如果成功,则返回json,否则返回报错信息

2.3 将json格式转换成jenkinsfile

URL: JENKINS_URL/pipeline-model-converter/toJenkinsfile Parameters: json格式的文件 Info: 获取json格式的文件,然后将它转换成jenkinsfile Returns: 如果成功,返回转换后的jenkinsfile,如果失败,返回报错信息。

还有一些其他的接口(如:验证json,groovy语法转换等)就不在讲解了。

三、Demo

这里将官网给出的最简单的一个例子转换成json,构建->单元测试->部署整个过程

pipeline {
    agent any
    stages {
        stage(''Build'') { steps { echo ''Building..'' } } stage(''Test'') { steps { echo ''Testing..'' } } stage(''Deploy'') { steps { echo ''Deploying....'' } } } }

在postman提交请求:

即可获得返回的结果:

{
    "status": "ok",
    "data": { "result": "success", "json": { "pipeline": { "stages": [ { "name": "Build", "branches": [ { "name": "default", "steps": [ { "name": "echo", "arguments": [ { "key": "message", "value": { "isLiteral": true, "value": "Building.." } } ] } ] } ] }, { "name": "Test", "branches": [ { "name": "default", "steps": [ { "name": "echo", "arguments": [ { "key": "message", "value": { "isLiteral": true, "value": "Testing.." } } ] } ] } ] }, { "name": "Deploy", "branches": [ { "name": "default", "steps": [ { "name": "echo", "arguments": [ { "key": "message", "value": { "isLiteral": true, "value": "Deploying...." } } ] } ] } ] } ], "agent": { "type": "any" } } } } }

四、总结

之前发过一篇讲解过如果使用jenkins x来制作一款类似阿里云云效的东西,但是之前没有发现到这款插件,导致语法的转换需要自行切割pipeline语法,之后才发现到这款插件,不再需要重复造轮子,总体上,如果规划设计的好的话,应该可以表面上媲美云效这款产品。

猜你喜欢

转载自www.cnblogs.com/lvcisco/p/12689927.html