How can i update a jenkins job using the api

前端 未结 6 525
青春惊慌失措
青春惊慌失措 2020-12-24 07:47

I have to create/update a jenkins job using its api because all of my jobs are using parameters which are also used by other scripts and I am trying to centralize the script

6条回答
  •  臣服心动
    2020-12-24 08:28

    http://asheepapart.blogspot.ca/2014/03/use-jenkins-rest-api-to-update-job.html

    That little bit of scripting looks to be what you are looking for. Uses the REST API to get and set the config with some regex S&R in the middle.

    Edit: Code below based on comment. It is copied directly from the blog so I take no credit for it.

    # First, get the http://jenkins.example.com/job/folder-name/job/sample-job--template/configure looking like you want
    
    read -s token
    # type token from http://jenkins.example.com/user/$userName/configure
    
    # Download the configuration XML for the template job (which will be our model template)
    curl -v -u "bvanevery:$token" http://jenkins.example.com/job/folder-name/job/sample-job--template/config.xml > generic-config.xml
    
    # My modules
    declare modules=('module1' 'module2' 'module3')
    
    # POST the updated configuration XML to Jenkins
    for m in ${modules[@]}; do
       echo "module $m";
       sed "s/MODULE/$m/g" generic-config.xml > $m-config.xml; 
       curl -v -X POST --data-binary @$m-config.xml -u "bvanevery:$token" \
            -H 'Content-Type: application/xml' \
            "http://jenkins.example.com/job/folder-name/job/$m/config.xml" ;
    done
    

提交回复
热议问题