[英]How can i update a jenkins job using the api
我必須使用 api 創建/更新 jenkins 作業,因為我所有的作業都在使用其他腳本也使用的參數,並且我試圖集中腳本,所以當我在一個地方更改它時,更改會全部反映。
目前,如果有人更改腳本,他們還必須手動編輯 jenkins 作業的參數。
我看到了用於創建作業的遠程 API 的示例,並且能夠成功創建測試作業,但是除了刪除它並再次創建它之外,我如何編輯現有作業(這不是一個選項,因為我必須維護構建歷史)。
你可以像這樣使用python:
from jenkinsapi.jenkins import Jenkins
jenkinsSource = 'http://10.52.123.124:8080/'
server = Jenkins(jenkinsSource, username = 'XXXXX', password = 'YYYYY')
myJob=server.get_job("__test")
myConfig=myJob.get_config()
print myConfig
new = myConfig.replace('<string>clean</string>', '<string>string bean</string>')
myJob.update_config(new)
如果其他人也在尋找相同的答案,
看起來解決方案要容易得多,您只需更新config.xml並將更新后的config.xml發布回jenkins,您的工作就會更新。
您還可以將更新的config.xml發布到可以獲取config.xml
的URL,以便以編程方式更新作業的配置。
獲取url模式: $JENKINS_SERVER/job/$JOB_NAME/config.xml
詳細的doc模式: $JENKINS_SERVER/job/$JOB_NAME/api
示例: https : //ci.jenkins-ci.org/job/infra_atlassian-base/api/
http://asheepapart.blogspot.ca/2014/03/use-jenkins-rest-api-to-update-job.html
這一點腳本看起來就像你在尋找什么。 使用REST API來獲取和設置配置,中間使用一些正則表達式S&R。
編輯:以下代碼基於評論。 它直接從博客中復制,所以我不相信它。
# 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
對於那些使用RestSharp的人,我發現我需要確保:
使用Request對象的參數發送更新的XML,類型參數的值為[ParameterType.RequestBody]
(link) 1 。
private XmlDocument JobConfigGet() { Uri JobConfigURI = GetJenkinsURI("job/" + _args.JobName + "/config.xml", null); RestClient restClient = new RestClient(JobConfigURI); RestRequest restRequest = new RestRequest(Method.GET); byte[] ua = Encoding.ASCII.GetBytes(Properties.Settings.Default.UserID + ":" + Properties.Settings.Default.UserPassword); restRequest.AddHeader("authorization", "Basic " + Convert.ToBase64String(ua)); IRestResponse restResponse = restClient.Execute(restRequest); if (restResponse.ResponseStatus != ResponseStatus.Completed || restResponse.StatusCode != HttpStatusCode.OK) throw new Exception(string.Format("Unable to retrieve job config: {0}. Wrong ResponseStatus ({1}) or StatusCode ({2}) returned.\\nURL: {3}", _args.JobName, restResponse.ResponseStatus.ToString(), restResponse.StatusCode.ToString(), restClient.BaseUrl.AbsoluteUri)); if (restResponse.ContentType != "application/xml") throw new Exception("Unexpected data type returned for job config: " + _args.JobName + ". Expected 'application/xml'. Got: " + restResponse.ContentType + ".\\nURL: " + restClient.BaseUrl.AbsoluteUri); XmlDocument jobConfig = new XmlDocument(); jobConfig.LoadXml(restResponse.Content); return jobConfig; } private void JobConfigUpdate(XmlDocument JobConfig, string JenkinCrumb) { // Update JobConfig XML as needed here. Uri JobConfigURI = GetJenkinsURI("job/" + _args.JobName + "/config.xml", null); RestClient restClient = new RestClient(JobConfigURI); RestRequest restRequest = new RestRequest(Method.POST); byte[] ua = Encoding.ASCII.GetBytes(Properties.Settings.Default.UserID + ":" + Properties.Settings.Default.UserPassword); restRequest.AddHeader("authorization", "Basic " + Convert.ToBase64String(ua)); string[] crumbSplit = JenkinCrumb.Split(':'); restRequest.AddHeader(crumbSplit[0], crumbSplit[1]); restRequest.AddParameter("text/xml", JobConfig.InnerXml, ParameterType.RequestBody); IRestResponse restResponse = restClient.Execute(restRequest); string resp = restResponse.Content; }
curl -v -X POST https://jenkinsurl.fr:8443/job/jobname/config.xml --data-binary "@config.xml" -u "jenkinsusername:yourjenkinstoken" -H "Content-Type: application/xml"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.