「Jenkins」- 使用接口批量创建任务

在 GitLab 中,有很多项目(大约 200 个),现在需要为这些项目创建 Jenkins 自动化流水(Jenkins Pipeline)。

我们想通过接口批量创建所有项目,而不是手工创建(避免巨大工作量)。该笔记见记录:如何通过 API 操作 Jenkins 服务。

注意事项:我们仅记录解决问题的关键步骤,没有面面俱到,具体操作需要根据需求调整。

文档:https://www.jenkins.io/doc/book/using/remote-access-api/

API wrappers

jenkins-rest | Java client is built on the top of jclouds for working with Jenkins REST API.

Java API wrappers:https://www.jenkins.io/doc/book/using/remote-access-api/#RemoteaccessAPI-JavaAPIwrappers

官网:None
文档:

仓库:https://github.com/cdancy/jenkins-rest

Maven Repository
https://mvnrepository.com/artifact/com.cdancy/jenkins-rest

通过 Jenkins Python 模块

方案一、通过 REST API 操作(使用 Python jenkins 模块)。通过 Jenkins 的 Python 模块进行批量操作(编程)。我们使用 python-jenkins 1.7.0 模块。

最开始,准备工作

我们需要在 Jenkins 中创建用于请求认证的 TOKEN 信息:
1)Click your name (upper-right corner).
2)Click Configure (left-side menu).
3)在创建 TOKEN 后,要保存页面,并将 TOKEN 记录(刷新页面将无法查看)

演示程序(Demo)

如下示例,演示如何使用 python-jenkins 模块获取所有 Job 定义,并过滤出所有使用 Join plugin 的 Job:

#!/usr/bin/python3

# 打印所有 Job
from jenkins import Jenkins
jks = Jenkins("https://jenkins.example.com/", "username", "token")
job_list = jks.get_all_jobs()
print(job_list)

# 美化输出
import json
print(json.dumps(joblist, sort_keys=True, indent=4))

jjb/python-jenkins: Python API for managing jobs and nodes in a Jenkins CI instance – python-jenkins
Python Jenkins — Python Jenkins 1.1.1.dev1 documentation

通过 Shell 命令 | REST API

How to create a job using the REST API and cURL?

通过 Shell 命令(curl),发送 HTTP 请求

或,Jenkins/Remote access API
或,页面右下角,REST API,能够获取页面功能相关的接口地址信息。

最开始,准备工作

我们需要在 Jenkins 中创建用于请求认证的 TOKEN 信息:
1)Click your name (upper-right corner).
2)Click Configure (left-side menu).
3)在创建 TOKEN 后,要保存页面,并将 TOKEN 记录(刷新页面将无法查看)

第一步、获取 config.xml 文件

首先,获取已定义任务的 config.xml 文件(用作模板):

curl -X GET "http://<example.com>/job/<your-job-name>/config.xml" \
    -u "<username>:<API_TOKEN>" \
    -o "<mylocalconfig.xml>"

第二步、修改 config.xml 文件

将得到的 config.xml 为任务的配置文件,根据自己的需要进行修改。

第三步、创建任务 or 跟新任务

使用修改的 config.xml 定义新任务:

curl -s -XPOST 'http://<example.com>/createItem?name=<yourJobName>' \
    -u "<username>:<API_TOKEN>" \
    --data-binary "@<mylocalconfig.xml>" \
    -H "Content-Type: text/xml"

与创建新任务类似,只是需要修改请求地址:

curl -X POST 'http://<example.com>/job/<your-job-name>/config.xml' \
    -u "<username>:<password>" \
    --data-binary "@<config.xml>" \
    -H "Content-Type: text/xml"

参考文献

How to update Jenkins Job config.xml file using curl
How to get the API Token for Jenkins – Stack Overflow

在 Jenkins Pipeline 中,访问 Jenkins 服务本身

api – Getting all Jenkins jobs in a single http request with tree or depth parameter – Stack Overflow
Get all pipeline Jobs in Jenkins Groovy Script – Stack Overflow

org.jenkinsci.plugins.workflow.job.WorkflowJob
https://javadoc.jenkins.io/plugin/workflow-job/org/jenkinsci/plugins/workflow/job/WorkflowJob.html

获取所有 Job 或 MultibranchPipeline 作业:

def jobs = Hudson.instance.getAllItems(hudson.model.Job.class)
def mbp = Hudson.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject.class)

获取 Job 参数:
println Jenkins.instance.getAllItems().collect {it.fullName}