sorin
sorin

Reputation: 170778

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

While using Groovy based pipelines in Jenkins you can trigger child jobs using the the build stage.

Still the documentation above states nothing regarding what kind of return object you would get and what attributes it has.

The only thing I found so far is that I can use build.getResult() to obtain the result of the triggered job.

Still, I do want to obtain the URL of this job.

Upvotes: 12

Views: 27487

Answers (3)

user3638751
user3638751

Reputation: 113

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')

Upvotes: 2

user3638751
user3638751

Reputation: 113

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

Upvotes: 2

Daniel Beck
Daniel Beck

Reputation: 6513

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.

Upvotes: 18

Related Questions