groovy - 如何在詹金斯 delarative piepline 中获取会话数据
问题描述
我不想从 CurrentBuild 或 CurrentUser 使用,因为这会返回构建工作的用户信息,但我想获取登录到 jenkins 的用户信息。例如由计时器运行的作业 X 和一个用户将中止这个,我想找到哪个用户中止了这个作业。
解决方案
您可以使用以下代码。请注意,当您要执行此代码时,您必须通过转到“管理 Jenkins/in process script Approval”并批准它们可执行来允许运行这些方法。通过使用此代码,您不仅可以在手动运行作业中也可以在计时器运行中了解谁中止了 Jenkins 管道。
pipeline {
agent any
triggers{cron("*/1 * * * *")}
stages {
stage('Hello') {
steps {
sleep(20000)
echo 'Hello World'
}
}
}
post{
aborted{
script{
def causee = ''
def actions = currentBuild.getRawBuild().getActions(jenkins.model.InterruptedBuildAction)
for (action in actions) {
def causes = action.getCauses()
// on cancellation, report who cancelled the build
for (cause in causes) {
causee = cause.getUser().getDisplayName()
cause = null
}
causes = null
action = null
}
actions = null
echo causee
}
}
}
}
推荐阅读
- javascript - 在每个单词之前添加“@”+每个单词之间有一个空格(JavaScript)?
- c++ - 重载运算符 << 后打印二维数组
- c++ - 给 enum 一个 int 数并在 c++ 中将其相关值作为字符串获取
- python-3.x - 将 Pandas df 中的数据提取到列表中
- sql - 在 Bigquery 中使用 REGEXP_EXTRACT 查找破折号之间的字符串
- unity3d - 缩放水平布局组的子布局元素,同时保持它们的纵横比
- c++ - std::ifstream::open 不一致失败
- python - 优化一个参数,使输出位于两点之间
- python - 返回一个包含每家公司最高销售额的完整行的 python DataFrame
- azure - k8s 无法访问数据库