Search code examples
curljenkinsjenkins-cli

How can I set Jenkins build description while triggering build via curl?


I'm trying to set the build description of a build I'm triggering, as I'm kicking off the build, and I have no luck so far.

I came across a solution (Adding text to the page of a build triggered by the Jenkins remote API), and I kind of got it to work this way (first command will kick off the build, second one will set the description of the last build):

curl -v -X POST "http://[myServer]/job/[jobName]/build"
curl -v -X POST "http://[myServer]/job/[jobName/lastBuild/submitDescription" --data-urlencode "description=test description"

However, the problem is that if the build I just kicked off gets queued / doesn't kick of right away, "lastBuild" will not reference the build I just kicked off, but the one before it (that is still building).

So I tried something like this:

payload='json={""description"":""test description""}'
curl -v -X POST -H "Content-Type: application/json" -d $payload "http://[myServer]/job/[jobName]/build"

But it doesn't actually set the description.

Any ideas how this can be achieved?

Other solutions I found, but I'm not really happy with:


Solution

  • You can always have a variable and pass the build description into the variable on the initial invocation. Then at the end of your build, output the variable to console and catch with Description Setter plugin.

    Edit to clarify:

    • Install Description Setter plugin.
    • In the Job Configuration, configure a String parameter, call it "MyDescription", leave defaults blank.
    • Somewhere in the build steps, either "Execute Shell" or "Execute Windows Batch Command" type echo Desc: $MyDescription or echo Desc: %MyDescription%, depending on your OS.
    • In the Post-Build steps, select "Set Build Description".
      • Set Regular expression as ^Desc: (.*)
      • Set Description as \1
    • From command line trigger by:

    curl -v -X POST --data-urlencode "MyDescription=This is my desc" "http://[myServer]/job/[jobName]/buildWithParameters"
    (that above is one line)