How to I get the url of build triggered with build step on Jenkins?

ぐ巨炮叔叔 提交于 2019-11-29 05:09:14

From the documentation for the build step in /pipeline-syntax (waitFormCompletion argument, the original has better formatting):

You may ask that this Pipeline build wait for completion of the downstream build. In that case the return value of the step is an object on which you can obtain the following read-only properties: so you can inspect its .result and so on.

  • 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

If you do not wait, this step succeeds so long as the downstream build can be added to the queue (it will not even have been started). In that case there is currently no return value.

The job URL doesn't exist (it's a build, after all), but absoluteUrl gives you the build URL.

rawBuild should let you access e.g. rawbuild.parent.url (untested), but it's generally unsafe and discouraged to leave the sandbox.

The previous answer is fine only for passed Jobs. Failing ones return null from build step. There is a corresponding Bug https://issues.jenkins-ci.org/browse/JENKINS-48475. It's set as RESOLVED because such behavior is expected, but IMO that's not correct as there is not alternative to get info from failed jobs

user3638751

set propagate to false - build step will pass and it will return the output with result field

Here is the code:

def success = "SUCCESS"
def build_run
build_run = build job: 'pass', propagate: false
println build_run.result
if (build_run.result == success)
    println('job1 passed')
else
    println('job1 failed')
build_run = build job: 'fail', propagate: false
println build_run.result
if (build_run.result == success)
    println('job2 passed')
else
    println('job2 failed')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!