通过url触发jenkins
// 在本地的命令行执行 node ./trigger_jenkins.js , 或者把此命令放到package.json的script字段
const request = require('request')
let apiToken ='xxxxxxxxx' //个人的apitoken , 路径:jenkins界面右上角用户名->设置->添加新Token
let username = 'luozheao' //用户名
let itemName = '24F-rf-code-shop' // 项目名
let headerStr = http://${username}:${apiToken}@192.168.197.95:8080/job/${itemName}/buildWithParameters
let triggerName = 'test_trigger' //jenkins->项目->配置->构建触发器->触发远程构建->填写你的身份验证令牌
let url = headerStr + ?token=${triggerName}&uploadComponentName=${encodeURI(uploadComponentName)}
//uploadComponentName为要带过去的参数,可以去掉
// 触发更新
request({
url,
method: "get",
json: true,
headers: {
"content-type": "application/json"
}
}, function (error, response, body) {
console.log()
if (error) {
console.log('触发jenkins编译失败', error)
} else {
console.log('触发jenkins编译成功')
}
})