惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
Help Net Security
Help Net Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
S
Securelist
V2EX - 技术
V2EX - 技术
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Cloudbric
Cloudbric
GbyAI
GbyAI
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
P
Proofpoint News Feed
SecWiki News
SecWiki News
T
Tailwind CSS Blog
腾讯CDC
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
T
Troy Hunt's Blog
G
Google Developers Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
博客园 - 叶小钗
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Know Your Adversary
Know Your Adversary
T
Tor Project blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
Y
Y Combinator Blog
H
Help Net Security
Latest news
Latest news

mafeifan 的编程技术分享

mafengwo-mp3-downloader | mafeifan 的编程技术分享 示例页面 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 查看 default namespace 下的 default service account名称 mafeifan 的编程技术分享 检查日志 | mafeifan 的编程技术分享 bridge fdb show dev flannel.1 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 数据收集原理分析 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 几种方法 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 check the export on the very top of this document so we can use $do mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 延伸 -- 关于前端路由 xdebug 3 和 2 的区别
mafeifan 的编程技术分享
2026-01-16 · via mafeifan 的编程技术分享

原文:http://showme.codes/2019-02-23/jenkins-script-console-in-practice/

通过脚本命令行批量修改Jenkins任务 ​

最近,笔者所在团队的 Jenkins 所在的服务器经常报硬盘空间不足。经查发现很多任务没有设置“丢弃旧的构建”。通知所有的团队检查自己的 Jenkins 任务有没有设置丢弃旧的构建,有些不现实。

一开始想到的是使用 Jenkins 的 API 来实现批量修改所有的 Jenkins 任务。笔者对这个解决方案不满意,经 Google 发现有同学和我遇到了同样的问题。

他使用的更“技巧”的方式:在 Jenkins 脚本命令行中,通过执行 Groovy 代码操作 Jenkins 任务。

Script Console 需要对Java和Jenkins相关的API比较熟悉,除了进行管理操作还可以诊断调式Jenkins。建议执行危险操作前先备份好数据。

总的来说,就两步:

  1. 进入菜单:系统管理 –> 脚本命令行

image.png

  1. 在输入框中,粘贴如下代码:

image.png

java

 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构造参数分别为:
 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 的运行时环境执行任意的 Groovy 脚本。

这意味着,我们可以在脚本命令行中做任何的事情,包括关闭 Jenkins,执行操作系统命令rm -rf /(所以不能使用 root 用户运行 Jenkins agent)等危险操作。

除了上文中的,使用界面来执行 Groovy 脚本,还可以通过 Jenkins HTTP API:/script执行。

具体操作,请参考官方文档

问题:代码执行完成后,对任务的修改有没有被持久化? ​

当我们代码job.addProperty(new BuildDiscarderProperty(new LogRotator (2, 10, 2, 10)))执行后,这个修改到底有没有持久化到文件系统中呢 (Jenkins 的所有配置默认都持久化在文件系统中)?我们看下 hudson.model.Job 的源码,在addProperty方法背后是有进行持久化的:

java

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

小结 ​

本文章只介绍了批量修改“丢弃旧的构建”的配置,如果还希望修改其它配置,可以参考 hudson.model.Job 的源码

不得不提醒读者朋友,Jenkins 脚本命令行是一把双刃剑,大家操作前,请考虑清楚影响范围。如果有必要,请提前做好备份。