- How to create a job using the REST API and cURL? - CloudBee
- Remote Access API - Jenkins
- Jenkins Remote Build Trigger - Build Jobs with URL and Token
关于远程创建任务,在 http://your_jenkins_url/api/ 有如下描述
Create Job
To create a new job, post
config.xml
to this URL with query parametername=*JOBNAME*
. You need to send aContent-Type: application/xml
header. You will get a 200 status code if the creation is successful, or 4xx/5xx code if it fails.config.xml
is the format Jenkins uses to store the project in the file system, so you can see examples of them in the Jenkins home directory, or by retrieving the XML configuration of existing jobs from/job/*JOBNAME*/config.xml
.
一、CRUMB TOKEN 的使用
Each Jenkins page has a REST API hyperlink at the bottom, this is because each page has its own endpoint.
http://localhost:8080/me
configure
Click 'Show API Token'
78e21f82a9e137614fef5b9593bcf827 = API Token
curl -s -u goll:78e21f82a9e137614fef5b9593bcf827 http://localhost:8080/crumbIssuer/api/json
curl -s -u goll:78e21f82a9e137614fef5b9593bcf827 -H 'Jenkins-Crumb:0f23a062e0b9b9d13295e26bc8c8e206' http://localhost:8080/job/foo2/buildWithParameters -d 'directoryName=zaid222'
查询 crumb
curl -s 'http://admin:yourtoken@jenkins-url/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
curl -s -X GET -u ${USERNAME}:${API_TOKEN} ${JENKINS_URL}/${API_ENDPOINT}
curl -v -X GET http://jenkins-url:8080/crumbIssuer/api/json --user <username>:<password>
curl -X POST http://jenkins-url:8080/job/<job-name>/build --user <username>:<password> -H 'Jenkins-Crumb: 0db38413bd7ec9e98974f5213f7ead8b'
官方范例 -CloudBees
Before 2.176.2, no session required:
# Replace with your Jenkins URL and admin credentials
SERVER="https://localhost:8080"
CRUMB=$(curl -u "admin:admin" "$SERVER"/crumbIssuer/api/xml?xpath=concat\(//crumbRequestField,%22:%22,//crumb\))
curl -X POST -u "admin:admin" -H "$CRUMB" "$SERVER"/job/someJob/build
After 2.176.2, session is required:
# Replace with your Jenkins URL and admin credentials
SERVER="https://localhost:8080"
# File where web session cookie is saved
COOKIEJAR="$(mktemp)"
CRUMB=$(curl -u "admin:admin" --cookie-jar "$COOKIEJAR" "$SERVER/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)")
curl -X POST -u "admin:admin" --cookie "$COOKIEJAR" -H "$CRUMB" "$SERVER"/job/someJob/build
二、远程创建Job范例
执行如下命令:
SERVER="http://10.51.99.15:8080"
COOKIEJAR="$(mktemp)"
echo $COOKIEJAR
CRUMB=$(curl -u "admin:admin" --cookie-jar "$COOKIEJAR" "$SERVER/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)")
echo $CRUMB
curl -X POST -u "admin:admin" --cookie "$COOKIEJAR" -H "$CRUMB" "$SERVER"/createItem?name=t5 --data-binary @t4config.xml -H "Content-Type:application/xml"
操作过程
[root@VM-99-15-centos ~]# SERVER="http://10.51.99.15:8080"
[root@VM-99-15-centos ~]# COOKIEJAR="$(mktemp)"
[root@VM-99-15-centos ~]# echo $COOKIEJAR
/tmp/tmp.QaKGJUaciO
[root@VM-99-15-centos ~]# CRUMB=$(curl -u "admin:admin" --cookie-jar "$COOKIEJAR" "$SERVER/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,%22:%22,//crumb)")
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 78 100 78 0 0 1021 0 --:--:-- --:--:-- --:--:-- 1026
[root@VM-99-15-centos ~]# echo $CRUMB
Jenkins-Crumb:c8f0a3749869ec0d85ec602479b24cfe68f7011d24cc228ddae08fc1f48d676d
[root@VM-99-15-centos ~]# curl -v -X POST -u "admin:admin" --cookie "$COOKIEJAR" -H "$CRUMB" "$SERVER"/createItem?name=t8 --data-binary @t4config.xml -H "Content-Type:application/xml"
* About to connect() to 10.51.99.15 port 8080 (#0)
* Trying 10.51.99.15...
* Connected to 10.51.99.15 (10.51.99.15) port 8080 (#0)
* Server auth using Basic with user 'admin'
> POST /createItem?name=t8 HTTP/1.1
> Authorization: Basic YWRtaW46YWRtaW4=
> User-Agent: curl/7.29.0
> Host: 10.51.99.15:8080
> Accept: */*
> Cookie: JSESSIONID.88236ae8=node01ka2a1vxl0buu1tuzgiwpeyfmo28.node0
> Jenkins-Crumb:c8f0a3749869ec0d85ec602479b24cfe68f7011d24cc228ddae08fc1f48d676d
> Content-Type:application/xml
> Content-Length: 1634
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Mon, 31 Jul 2023 13:18:41 GMT
< X-Content-Type-Options: nosniff
< Content-Length: 0
< Server: Jetty(9.4.45.v20220203)
<
* Connection #0 to host 10.51.99.15 left intact
网友评论