检测Job是否存在

1
curl -XGET http://USER:API_TOKEN@JENKINS_URL/checkJobName?value=JOB_NAME

创建Job

1
curl -XPOST -H "Content-Type: application/xml" 'http://<USER>:<API_TOKEN>@<JENKINS_URL>/createItem?name=<JOB_NAME>' -d "@/PATH/TO/config.xml"

删除Job

1
curl -XPOST http://<USER>:<API_TOKEN>@<JENKINS_URL>/job/<JOB_NAME>/doDelete

获取Job当前配置

1
curl -XGET http://<USER>:<API_TOKEN>@<JENKINS_URL>/job/<JOB_NAME>/config.xml -o <JOB_NAME>.xml

更新Job配置

1
curl -XPOST http://<USER>:<API_TOKEN>@<JENKINS_URL>/job/<JOB_NAME>/config.xml -d "@<JOB_NAME>.xml"

构建Job

1
curl -XPOST http://<USER>:<API_TOKEN>@<JENKINS_URL>/job/<JOB_NAME>/build

参数化构建Job

1
curl -XPOST http://<USER>:<API_TOKEN>@<JENKINS_URL>/job/<JOB_NAME>/build --form json='{"parameter": [{"name":"<NAME>","value":"<VALUE>"}]}'

或者

1
curl -XPOST http://<USER>:<API_TOKEN>@<JENKINS_URL>/job/<JOB_NAME>/buildWithParameters?<NAME>=<VALUE>

示例

  • 检测Job是否存在

    1
    2
    
    ➜  ~ curl -XGET http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/checkJobName?value=test
    <div class=error><img src='/static/0c8c91ca/images/none.gif' height=16 width=1>A job already exists with the name ‘test’</div>%
    
  • 创建job

    1
    
    curl -s http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test/config.xml | curl -X POST 'http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/createItem?name=test2' -H "Content-Type: application/xml" -d @-
    
  • 删除job

    1
    
    curl -XPOST http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test2/doDelete
    
  • 获取Job当前配置

    1
    
    curl -XGET http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test/config.xml
    
  • 更新Job配置

    1
    
    curl -XPOST http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test/config.xml -d "@test.xml"
    
  • 构建Job

    1
    
    curl -XPOST http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test/build
    
  • 参数化构建Job

    1
    2
    3
    
    curl -XPOST http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test/build --data-urlencode json='{"parameter": [{"name":"NAME","value":"11111"}]}'
    curl -X POST http://admin:117fd7d3aae52a3ce02ace7d783d3bbd9d@jenkins.dianduidian.com/job/test/build \
    --form json='{"parameter": [{"name":"branch_name","value":"k8s"}]}'
    

参考:

https://stackoverflow.com/questions/8424228/export-import-jobs-in-jenkins

https://wiki.jenkins.io/display/JENKINS/Remote+access+API