jenkins - 如何使用 Jenkins Pipeline 并选择电子邮件通知的触发器?

标签 jenkins continuous-integration jenkins-plugins jenkins-pipeline

在 Pipeline 之前,我使用 Email-ext plugin定义发送电子邮件的触发器(第一次失败时以及恢复成功时)。

但是在Pipeline版本中,我无法控制触发器,我只能“始终发送”,或者根据当前构建结果发送。

如何根据更一般的触发器在管道中发送电子邮件? (第一次失败、第一次成功等)

谢谢!

最佳答案

在设置 jenkins 管道时,您有一个 Pipeline Syntax Groovy 脚本编辑器下方的链接如下图所示。

enter image description here

单击链接即可进入代码片段生成器,如下图所示。 enter image description here

这里我选择了 emailext :扩展插件选项。您也可以从下拉列表中选择简单邮件选项并生成代码段。

为了确定当前作业的构建状态,Jenkins 提供了您在编写 Groovy 脚本时可以使用的环境变量。您可以通过单击“代码片段生成器”页面末尾提供的链接找到全局环境变量的列表。 enter image description here

下面是 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, or FAILURE (may be null for an ongoing build)
currentResult
typically SUCCESS, UNSTABLE, or FAILURE. Will never be null.
resultIsBetterOrEqualTo(String)
Compares the current build result to the provided result string (SUCCESS, UNSTABLE, or FAILURE) 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, or FAILURE) 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 a commitId, timestamp, msg, author, and affectedFiles each of which has an editType and path; the value will not generally be Serializable 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 be Serializable so you may only access it inside a method marked @NonCPS
Additionally, for this build only (but not for other builds), the following properties are writable:

  • <b>result</b>
  • <b>displayName</b>
  • <b>description</b>

希望这有帮助。祝你好运!

关于jenkins - 如何使用 Jenkins Pipeline 并选择电子邮件通知的触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49596220/

相关文章:

jenkins - 如何从 Jenkins 管道中的函数返回 'allOf' 的值

continuous-integration - 运行构建步骤Azure资源组部署时,StorageAccountAlreadyExists

Jenkins 管道 sh 步骤返回错误 "process apparently never started"

jenkins - 使用 GitHub/Jenkins/Kubernetes 实现 CI/CD 管道时的最佳实践

python - 如何避免在源代码中保留版本号?

postgresql - 通过 jenkins 部署 postgres - 持续集成/部署

jenkins - Swagger 和 Jenkins 集成

java - 如何获取 Jenkins 进程的 PID 和端口号

Android 支持存储库和持续集成

svn - Jenkins 在成功构建后提交