实现Jenkinsfile与Json的转换

简介: 实现Jenkinsfile与Json的转换

目录

最近在做个需求,需要支持Jenkinsfile和json的转换。

方法1:使用现有的jenkins插件

参考的是这篇文章。下面介绍一下将插件打包成镜像的步骤:

  • 本地安装jdk和maven,jdk建议采用的版本为8(该工程会依赖一个名为tools.jar的包,jdk 9之后移除了该包)
    如果本地没有找到tools.jar,可以下载一个1.8版本的jdk,然后在pom.xml中增加如下依赖
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>/root/jdk1.8.0_271/lib/tools.jar</systemPath>
</dependency>
<settings>
<pluginGroups>
<pluginGroup>org.jenkins-ci.tools</pluginGroup>
</pluginGroups>
<profiles>
<!-- Give access to Jenkins plugins -->
<profile>
<id>jenkins</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- change this to false, if you don't like to have it on per default -->
</activation>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<mirrors>
<mirror>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
<mirrorOf>m.g.o-public</mirrorOf>
</mirror>
</mirrors>
</settings>
  • 执行 mvn install进行编译
  • 由于主要用到的是Jenkinsfile和json之间的转换关系,因此主要用的是如下两个REST API:
  • Conversion to JSON representation from Jenkinsfile
  • URL: JENKINS_URL/pipeline-model-converter/toJson
  • Parameters: jenkinsfile - the Jenkinsfile contents
  • Info: Takes a Jenkinsfile and converts it to the JSON representation for its pipeline step.
  • Returns: JSON with a result field that will either be success or failure. If success, the JSON representation will be in the json field. If failure, there'll be an additional array in the errors field of the error messages encountered.
  • Conversion to Jenkinsfile from JSON representation
  • URL: JENKINS_URL/pipeline-model-converter/toJenkinsfile
  • Parameters: json - the JSON representation of the model
  • Info: Takes the JSON representation of the model and converts it to the contents for a Jenkinsfile invoking the pipeline step.
  • Returns: JSON with a result field that will either be success or failure. If success, the Jenkinsfile contents will be in the jenkinsfile field. If failure, there'll be an additional array in the errors field of the error messages encountered.
  • 上述两个API在pipeline-model-definition-plugin/pipeline-model-definition目录下,因此在该目录下直接运行:mvn hpi:run -Dhost=0.0.0.0 -Djetty.port=8080即可。
  • 将json转换为Jenkinsfile的操作如下:
    完整的返回值如下:
{
"status": "ok",
"data": {
"result": "success",
"json": {
"pipeline": {
"stages": [
{
"name": "Hello",
"branches": [
{
"name": "default",
"steps": [
{
"name": "echo",
"arguments": [
{
"key": "message",
"value": {
"isLiteral": true,
"value": "Hello World"
}
}
]
}
]
}
]
}
],
"agent": {
"type": "any"
}
}
}
}
}
  • 将Jenkinsfile转换为json的操作如下
  • 制作容器镜像时,只需要将本地工程和/root/.m2上传到容器,生成对应的镜像即可,下面Dockerfile假设生成的镜像为pipeline-model-definition-plugin:latest
FROM pipeline-model-definition-plugin:latest
WORKDIR /usr/pipeline-model-definition-plugin/pipeline-model-definition
ENV PATH=$PATH:/usr/local/bin/maven-3.6.3/bin
ENTRYPOINT ["sh", "-c", "mvn hpi:run -Dhost=0.0.0.0"]
  • 我自己打包了一个镜像:docker pull quay.io/woodliu/pipeline-model-definition-plugin

需要注意的是,本插件提供的转换API toJenkinsfile和toJson并不是万能的,只能支持jenkins标准的参数类型,例如对于gitParameter这样的参数就无法解析(扩展功能),一种解决方式是独立解析扩展的参数,然后将其插入解析好的标准JenkinsFile中;另外一个方式就是写一个jenkinsfile的解析器。

参考

  • mvn hpi的命令可以参考官方文档
  • 可以运行mvn hpi:hpi生成对应的hpi文件,如:
    /pipeline-model-definition-plugin/pipeline-model-definition/target/pipeline-model-definition.hpi

方法2:解析原生的jenkinsfile文件

在GitHub上有一个支持jenkinsfile解析的项目,该项目使用rust的pest crate来编写jenkinsfile的语法,支持对jenkinsfile的格式验证。Pest官方文档中给出了一个非常好的对json语法的解析例子,主要是使用递归的方式来解析语法。

pest官方提供了一个编辑器,可以使用该编辑器查看经过pest解析之后的字段,对了解pest的工作方式非常有用。如,使用jdp项目提供的pest文件解析如下jenkinsfile:

pipeline {
    agent {
        docker {
            reuseNode true
            image 'maven:3-alpine'
            label 'my-defined-label'
            args  '-v /tmp:/tmp'
            registryUrl 'https://myregistry.com/'
            registryCredentialsId 'myPredefinedCredentialsInJenkins'
        }
    }
    stages {
        stage('Build') {
            steps { sh 'make' }
        }
    }
}

对应的解析结果如下:

- preceeding_junk: ""
- opening_brace: "{"
- agentDecl > agentBlock
- opening_brace: "{"
- dockerAgent
- opening_brace: "{"
- bool: "true"
- string > single_quoted
- single_quote: "\'"
- inner_single_str: "maven:3-alpine"
- single_quote: "\'"
- string > single_quoted
- single_quote: "\'"
- inner_single_str: "my-defined-label"
- single_quote: "\'"
- string > single_quoted
- single_quote: "\'"
- inner_single_str: "-v /tmp:/tmp"
- single_quote: "\'"
- string > single_quoted
- single_quote: "\'"
- inner_single_str: "https://myregistry.com/"
- single_quote: "\'"
- string > single_quoted
- single_quote: "\'"
- inner_single_str: "myPredefinedCredentialsInJenkins"
- single_quote: "\'"
- closing_brace: "}"
- closing_brace: "}"
- stagesDecl
- opening_brace: "{"
- stage
- string > single_quoted
- single_quote: "\'"
- inner_single_str: "Build"
- single_quote: "\'"
- opening_brace: "{"
- stepsDecl
- opening_brace: "{"
- step > simple_step
- IDENT: "sh"
- args > string > single_quoted
- single_quote: "\'"
- inner_single_str: "make"
- single_quote: "\'"
- closing_brace: "}"
- closing_brace: "}"
- closing_brace: "}"
- closing_brace: "}"
- ending_junk: ""
- EOI: ""

Pest语法重点标注:

  • 当使用静默规则时,解析结果中将不会出现该规则字段。当解析下面规则时,解析结果中将不会存在silent,即parsed.as_rule() 中不会存在silent
silent = _{ ... }
  • 当使用原子语法时,整个规则体将视为一个规则,如double_quoted = ${ (quote ~ inner_double_str ~ quote) },在解析时会将quote ~ inner_double_str ~ quote视为一个规则,而不是三个。这有利于获取一段完整的字符串。
atomic = @{ ... }
compound_atomic = ${ ... }

我尝试使用该项目解析jenkinsfile,但发现实现起来太过复杂,且rust和jenkinsFile的语法也是一言难尽。如下,当step中带括号和不带括号混用时会导致解析错误。

steps {
    echo 'test'
    dir('command') {
    sh "sh ./saas.sh ${params.channel} ${params.buildType} "
  }     
}

有精力的大神可以在此基础上实现解析JenkinsFile的功能。

参考

目录
相关文章
|
缓存 编译器
软件体系结构 - 指令集架构
软件体系结构 - 指令集架构
978 0
|
存储 缓存 Linux
Linux内核文件系统:比万物之神还要强大的存储魔法!
Linux内核文件系统:比万物之神还要强大的存储魔法!
|
存储 Kubernetes 数据库
小笔记:关于 gitlab 运行 gitlab-ctl reconfigure 后数据清空(gitlab数据备份与恢复)
小笔记:关于 gitlab 运行 gitlab-ctl reconfigure 后数据清空(gitlab数据备份与恢复)
2285 0
|
4月前
|
人工智能 运维 监控
CC Switch路由代理实操:零代码打通Codex CLI,无缝调用DeepSeek等主流大模型
对于后端开发、运维工程师而言,Codex CLI凭借脚本自动生成、代码调试排错、日志解析、自动化运维脚本编写等能力,已经成为命令行场景下高频使用的AI辅助工具。但该工具底层原生只适配OpenAI Responses API交互协议,而国内绝大多数商用、开源大模型,包括DeepSeek、Kimi、MiniMax、通义千问等,全部采用Chat Completions API标准进行对外服务。两套API协议在请求结构、参数字段定义、流式分片传输规则、错误回调逻辑、多轮会话管理机制上完全割裂互不兼容。
828 0
|
8月前
|
人工智能 API 网络安全
超易上手教程:OpenClaw阿里云/本地部署,零代码无缝接入Qwen3.6 Plus,5分钟速成AI Agent
Qwen3.5 Plus作为2026年初更新的旗舰大模型,凭借3970亿总参数量(推理仅激活170亿)的高效架构,成为智能体的“顶配大脑”;而OpenClaw则是轻量级智能体编排框架,无需手写上下文管理、工具调用逻辑,堪称小白福音。两者搭配,Qwen3.5 Plus负责思考决策,OpenClaw负责执行调度,零代码基础也能在5分钟内跑通可用的智能体。
1313 0
|
12月前
|
机器学习/深度学习 人工智能 自然语言处理
景区博物馆最新应用:历史IP复活,移动AI数字人互动屏打造文旅爆款体验
世优波塔移动AI数字人互动屏,以高精度建模与AI大模型技术“复活”历史人物,打造可对话、会讲解、能导览的沉浸式文旅体验。通过自然语言交互、智能移动导引与多设备联动,让文物“开口说话”,让文化“活”起来,重新定义博物馆、景区与传统文化的连接方式,开启智慧文旅新纪元。
822 0
|
存储 固态存储 安全
阿里云服务器最新租用价格:收费标准与2核4G/4核8G等热门配置活动价格参考
阿里云服务器租用价格是多少?阿里云服务器报价主要看所选云服务器的实例规格与带宽和云盘等配置,现在购买阿里云服务器,轻量应用服务器2核2G200M带宽38元1年,经济型e实例2核2G3M带宽99元1年,通用算力型u1实例2核4G5M带宽199元1年。本文为大家展示阿里云服务器最新的收费标准与活动价格情况,以供了解和参考。
|
存储 缓存 资源调度
深入浅出Node.js中的node_modules(二)
深入浅出Node.js中的node_modules
|
存储 Linux Windows
制作Linux系统SD启动卡
【9月更文挑战第2天】这段内容介绍了一种制作 Linux 系统 SD 启动卡的方法,包括准备工作、安装写入软件、插入与识别 SD 卡、使用写入软件、写入后检查及设置启动设备等步骤。需选择高质量 SD 卡和可靠来源的镜像文件,避免误操作导致数据丢失。
744 4