jenkins - 如何在 Jenkins 管道语法中对文件的最后修改时间使用`when`条件
问题描述
我正在创建一个 Jenkins 管道,我希望仅当特定日志文件(日志文件位于所有阶段都将运行的服务器节点中)最后修改日期在管道作业启动后更新时触发某个阶段,我知道我们需要使用“何时”条件,但不确定如何实现它。
尝试引用一些与管道相关的门户,但无法找到答案
有人可以帮我解决这个问题吗?
提前致谢!
解决方案
To get data about file is quite tricky in a Jenkins pipeline when using the Groovy sandbox since you're not allowed to do new File(...).lastModified
. However there is the findFiles
step, which basically returns a list of wrapped File
objects with a getter for last modified time in millis, so we can use findFiles(glob: "...")[0].lastModified
.
The returned array may be empty, so we should rather check on that (see full example below).
The current build start time in millis is accessible via currentBuild.currentBuild.startTimeInMillis
.
Now that we git both, we can use them in an expression
:
pipeline {
agent any
stages {
stage("create file") {
steps {
touch "testfile.log"
}
}
stage("when file") {
when {
expression {
def files = findFiles(glob: "testfile.log")
files && files[0].lastModified < currentBuild.startTimeInMillis
}
}
steps {
echo "i ran"
}
}
}
}
推荐阅读
- mysql - 我将从 SQL 中获得一些数字,但想将其打印为其他数字
- r - 在同一时刻选择具有最大缺失值数的几列
- bash - 使用 find 命令复制结果条目
- swift - 如何在 Swift 中获取屏幕中某个像素的 RGBA 值?
- android - 我为 Android Studio 购买了多少 GB RAM?
- php - 更改 ajax 成功的谷歌地图中心
- javascript - 导航选项在 React Native 中不起作用
- json - 将 JSON url 图像解析为 Scrollview - Unity
- angular - 角度打字稿自定义命名空间
- r - R 中 value 和 par 与 optim 函数的解释问题