溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Jenkins怎么批量修改任務

發(fā)布時間:2021-03-16 16:05:59 來源:億速云 閱讀:258 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)使用Jenkins怎么批量修改任務,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

進入菜單:系統(tǒng)管理 --> 腳本命令行

在輸入框中,粘貼如下代碼:

import jenkins.model.Jenkins
import hudson.model.Job
import jenkins.model.BuildDiscarderProperty
import hudson.tasks.LogRotator
// 遍歷所有的任務
Jenkins.instance.allItems(Job).each { job ->

if ( job.isBuildable() && job.supportsLogRotator() && job.getProperty(BuildDiscarderProperty) == null) {
 println " \"${job.fullDisplayName}\" 處理中"

 job.addProperty(new BuildDiscarderProperty(new LogRotator (2, 10, 2, 10)))
 println "$job.name 已更新"
}
}
return;

/**

LogRotator構(gòu)造參數(shù)分別為:
daysToKeep: If not -1, history is only kept up to this days.
numToKeep: If not -1, only this number of build logs are kept.
artifactDaysToKeep: If not -1 nor null, artifacts are only kept up to this days.
artifactNumToKeep: If not -1 nor null, only this number of builds have their artifacts kept.
**/

腳本

腳本命令行介紹

腳本命令行(Jenkins Script Console),它是 Jenkins 的一個特性,允許你在 Jenkins master 和 Jenkins agent 的運行時環(huán)境執(zhí)行任意的 Groovy 腳本。這意味著,我們可以在腳本命令行中做任何的事情,包括關(guān)閉 Jenkins,執(zhí)行操作系統(tǒng)命令 rm -rf /(所以不能使用 root 用戶運行 Jenkins agent)等危險操作。

除了上文中的,使用界面來執(zhí)行 Groovy 腳本,還可以通過 Jenkins HTTP API:/script執(zhí)行。具體操作,請參考 官方文檔。

問題:代碼執(zhí)行完成后,對任務的修改有沒有被持久化?

當我們代碼job.addProperty(new BuildDiscarderProperty(new LogRotator (2, 10, 2, 10)))執(zhí)行后,這個修改到底有沒有持久化到文件系統(tǒng)中呢(Jenkins 的所有配置默認都持久化在文件系統(tǒng)中)?我們看下 hudson.model.Job 的源碼,在addProperty方法背后是有進行持久化的:

public void addProperty(JobProperty<? super JobT> jobProp) throws IOException {
((JobProperty)jobProp).setOwner(this);
properties.add(jobProp);
save();
}

看完上述內(nèi)容,你們對使用Jenkins怎么批量修改任務有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI