用Jenkins做持续集成时,一般会使用webhooks触发构建,或者定时构建,这里记录用URL的方式触发Jenkins构建。
Note: This assumes you're using Jenkins' own user database for user management rather than LDAP or another directory service.
1. 创建一个新用户
Manage Jenkins->Manage Users->Create User
输入用户名密码,然后点击注册,这里假设用户名为test
2. 在任务设置中激活Trigger builds remotely (e.g., from scripts),这个token是自己设定的
3. 给用户添加权限
Manage Jenkins->Configure Global Security
勾选Access Control下的matrix-based security,然后将test用户添加到列表里面
只需要以下权限即可:
- Overall - Read
- Job - Build
- Job - Read
- Job - Workspace
4. 创建URL
用test账号登录,从右上角点击用户,进入用户设置,点击Show API Token...
根据这些内容,就可以创建一个这样的URL:
http://test:fe20f2d33760fbeec0a362dbbf59cd18@your-jenkins.com/job/JobName/build?token=
iFdsjS24SDFqp53JGsd9k0Cm
注意这里的your-jenkins.com一般是IP+端口号
最后使用cURL或者Postman发送请求
curl -X POST http://test:fe20f2d33760fbeec0a362dbbf59cd18@your-jenkins.com/job/JobName/build?token=
iFdsjS24SDFqp53JGsd9k0Cm
如果出现403错误:No valid crumb was included in the request
请在系统管理 –> Configure Global Security中调整设置:
取消“防止跨站点请求伪造(Prevent Cross Site Request Forgery exploits)”的勾选。