在 Pipeline 之前,我使用 Email-ext plugin定义发送电子邮件的触发器(第一次失败时以及恢复成功时)。
但是在Pipeline版本中,我无法控制触发器,我只能“始终发送”,或者根据当前构建结果发送。
如何根据更一般的触发器在管道中发送电子邮件? (第一次失败、第一次成功等)
谢谢!
最佳答案
在设置 jenkins 管道时,您有一个 Pipeline Syntax
Groovy 脚本编辑器下方的链接如下图所示。
这里我选择了 emailext :扩展插件选项。您也可以从下拉列表中选择简单邮件选项并生成代码段。
为了确定当前作业的构建状态,Jenkins 提供了您在编写 Groovy 脚本时可以使用的环境变量。您可以通过单击“代码片段生成器”页面末尾提供的链接找到全局环境变量的列表。
下面是 currentBuild
的属性列表环境变量供您引用。
The currentBuild
variable may be used to refer to the currently running build.
It has the following readable properties:
number
- build number (integer)
result
- typically
SUCCESS
,UNSTABLE
, orFAILURE
(may be null for an ongoing build) currentResult
- typically
SUCCESS
,UNSTABLE
, orFAILURE
. Will never be null. resultIsBetterOrEqualTo(String)
- Compares the current build result to the provided result string (
SUCCESS
,UNSTABLE
, orFAILURE
) and returns true if the current build result is better than or equal to the provided result. resultIsWorseOrEqualTo(String)
- Compares the current build result to the provided result string (
SUCCESS
,UNSTABLE
, orFAILURE
) and returns true if the current build result is worse than or equal to the provided result. displayName
- normally
#123
but sometimes set to, e.g., an SCM commit identifier description
- additional information about the build
id
- normally
number
as a string timeInMillis
- time since the epoch when the build was scheduled
startTimeInMillis
- time since the epoch when the build started running
duration
- duration of the build in milliseconds
durationString
- a human-readable representation of the build duration
previousBuild
- another similar object, or null
nextBuild
- similarly
absoluteUrl
- URL of build index page
buildVariables
- for a non-Pipeline downstream build, offers access to a map of defined build variables; for a Pipeline downstream build, any variables set globally on
env
changeSets
- a list of changesets coming from distinct SCM checkouts; each has a
kind
and is a list of commits; each commit has acommitId
,timestamp
,msg
,author
, andaffectedFiles
each of which has aneditType
andpath
; the value will not generally beSerializable
so you may only access it inside a method marked@NonCPS
rawBuild
- a
hudson.model.Run
with further APIs, only for trusted libraries or administrator-approved scripts outside the sandbox; the value will not beSerializable
so you may only access it inside a method marked@NonCPS
<b>result</b>
<b>displayName</b>
<b>description</b>
希望这有帮助。祝你好运!
关于jenkins - 如何使用 Jenkins Pipeline 并选择电子邮件通知的触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49596220/