参考jenkins官方的文档https://wiki.jenkins.io/display/JENKINS/HTTP+Request+Plugin
The request is made as a job execution in Jenkins and depending of the HTTP response the job could be marked as failed (configurable). For example responses such as 404, 500 could make the job fail. When a job fails it will log the response to help identify the problem.
每一个请求就代表一个job执行 response代表每一个job的状态 [succ/fail]
1.登陆 通过http解决登陆token
jenkins 用户验证采用 Basic Auth 就是在header里带上auth信息
auth: username:password
在jenkins configure 后台找到自己的
User ID -> username
API Token -> password

2.jenkins 各个接口文档
jenkins的任务对象的数据也可以通过固定的 URL 进行访问或者查询,且均支持三种形式:
XML: /jenkins/.../api/xml
JSON: /jenkins/.../api/json
PYTHON:/jenkins/.../api/python
我门用到第二种
http://10.10.106.240:8001/jenkins/view/InPromo/job/${yourjob}/api/json
在你的任务后面加上 /api/json 可以看到这个任务的对象信息
http://10.10.106.240:8001/jenkins/view/InPromo/job/${yourjob}/api/json?tree=[name]
参数 tree 可以分开得到每个key的值
3.构建流程
// 通过charles抓包 可以得到每个任务的body path是任务路径
let jenkisPost = (body, path) => new Promise((resolve, reject) => {
var bodyString = `json=${encodeURIComponent(JSON.stringify(body))}`
var req = request({
port: 8001,
hostname: '10.10.106.240',
method: 'POST',
auth: username:password, // 这里就是 auth: username:password
headers: {
'Content-Length': Buffer.byteLength(bodyString),
'Content-Type': 'application/x-www-form-urlencoded'
},
path: path
}, res => {
let rawData = ''
res.setEncoding('utf8')
res.on('data', chunk => (rawData += chunk))
res.on('end', (_res) => {
resolve(rawData)
})
})
req.write(bodyString)
req.end()
})
ps:
发布分之:
const BRANCH = execSync('git status | head -n 1 | awk \'{print $3}\'')
网友评论