Post

Mac配置Jenkins七:Jenkins调用另外一台Jenkins

Mac配置Jenkins七:Jenkins调用另外一台Jenkins

前言

情景一:项目中为了代码安全,防止所有代码都集中到同一台电脑被所有人到看到,所以iOS、安卓、PC、后端各自维护了一套自己独立的Jenkins打包环境 情景二:由于iOS有多套开发者账号,为了账号安全,每一套开发者账号都独立配置在不同的mac电脑上,不同的电脑配置了不同的Jenkins,导致要打包也要记录不同的地址和账号密码很繁琐

解决方案:

提供一台Jenkins,用于提供账号及权限控制,测试的所有打包都在这一台Jenkins上,然后需要打其他平台或其他开发者账号的包时,由Jenkins使用API Token的功能调用其他Jenkins

一、 在子Jenkins电脑上创建用户及API Token

1、使用管理员账户在子Jenkins上配置支持API Token访问
1.1 系统管理 -> 全局安全配置 -> API Token -> 允许用户手动创建一个遗留的API Token
http://127.0.0.1:8080/manage/configureSecurity/ image

1.2 创建一个Jenkins账号,比如admin
1.3 登录账号 -> 设置 -> API Token -> 添加新 Token
http://127.0.0.1:8080/user/xxx/configure http://127.0.0.1:8080/user/admin/configure image image image 记录好用户名及创建好的token


二、 子Jenkins电脑上创建一个项目Job

image image

三、 主Jenkins电脑上访问子Jenkins的任务

3.1、使用shell脚本访问,前提是要子Jenkins的ip域名能正常访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 无参数的格式
# http://ip:8080/job/TestRemote/build
# 有参数的格式
# http://ip:8080/job/TestRemote/buildWithParameters?name=Jim&age=100
# 完整的
# curl -X POST "http://ip:8080/job/MainTest/buildWithParameters?name=Jim&age=100" --user username:userapitoken
# 完整的demo,userapitoken是之前第一步创建的

# 原始参数
nameStr="John Doe (Junior)"
age="100"

# 使用-d参数会自带urlencode
myparamOriginStr="name=${nameStrEncoded}&age=${age}"
curl -d"$myparamOriginStr" -X POST  "http://xxx.xxx.xxx.xxx:8080/job/MainTest/buildWithParameters" --user admin:11xxxxxxxx5d

# 或者手动encode参数,对需要encode的参数手动encode
nameStrEncoded=$(perl -MURI::Escape -e "print uri_escape('${nameStr}');")
paramStr="name=${nameStrEncoded}&age=100"
curl -X POST "http://xxx.xxx.xxx.xxx:8080/job/MainTest/buildWithParameters?${paramStr}" --user admin:11xxxxxxxx5d

3.2、 在主Jenkins上,创建一个Job,然后使用shell脚本来调用子Jenkins的任务 image image


执行结果: image image image image

为了读取打包用户信息,可以安装build user vars plugin插件,然后直接读取BUILD_USER就能直接获取构建用户 参考
jenkins api token使用
curl 命令,当url中存在匹配条件和空格时如何书写。 shell urlencode编码

This post is licensed under CC BY 4.0 by the author.