1. 在Jenkins上找到用户->设置->API Token,增加用户生成token,如下图
2. 在需要被远程调用的任务里,勾选触发远程构建,填入步骤一的token
3. 在其他机器上,发送post请求
curl -d 'job=job_name&token={token}¶m=xxx' http://x.x.x.x/jenkins/buildByToken/buildWithParameters
报错信息
1、403报错
解决方案:
一般通过api调用Jenkins job出现403(HTTP ERROR 403 No valid crumb was included in the request)报错,是因为新版本Jenkins为了安全,搞的一套crsf认证机制。jenkins高版本在系统设置里没有关闭crsf的功能。
1.1、通过命令安装jenkins
① 编辑配置文件
sudo systemctl edit jenkins
② 加入下面这个配置
[Service] Environment="JAVA_OPTS=-Djava.awt.headless=true -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true"
③ 重启
sudo systemctl restart jenkins
④ 查看是否生效
ps -ef | grep jenkins
1.2、通过tomcat启动jenkins war包方式
找到tomcat ->bin ->catalina.sh找到JAVA_OPTS,加入下面内容
Register custom URL handlers
Do this here so custom URL handles (specifically ‘war:…’) can be used in the security policy
JAVA_OPTS=“$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true”
2、Authentication required,如下
`
Authentication required