How can I set Jenkins build description while trig

2020-07-06 09:03发布

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:

5条回答
姐就是有狂的资本
2楼-- · 2020-07-06 09:15

I had the same need - set the build description as soon as the build starts.
Note that the Build Description Setter plugin is activated as a post-build action which is too late for me.
The way I solved it is by a minor change to the job configuration and a Python script (but can be in any language):

  • Add a UUID parameter in the job configuration
  • Created a script to submit & set description

The script does as follows:

  1. When submitting a build, generate a uuid value (unique, right?) and populate the UUID parameter
  2. Poll the Jenkins job (get its JSON via REST API), loop on all the running builds, find mine (via the known value of UUID). Limit the polling by a timeout, so we don't hang forever
  3. Use the Jenkins Java CLI to set the description (command 'set-build-description')

Works all the time, except when the build is queued (no free executors are available) and the timeout set above expires - there's nothing I can do.

查看更多
贪生不怕死
3楼-- · 2020-07-06 09:24

Another solution, with "Execute Groovy System script" :

def currentBuild = Thread.currentThread().executable
def FOO = build.buildVariableResolver.resolve('FOO')
currentBuild.setDescription(FOO)
查看更多
爷的心禁止访问
4楼-- · 2020-07-06 09:31

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)

查看更多
Root(大扎)
5楼-- · 2020-07-06 09:33

For those interested in using the Jenkins UI, I'm trying:

  1. https://wiki.jenkins-ci.org/display/JENKINS/Build+Name+Setter+Plugin
  2. https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin

The Postbuild plugin is much more powerful, but requires Groovy tinkering and perms.

查看更多
Ridiculous、
6楼-- · 2020-07-06 09:39

my download:

String urlDownload = "https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip?token_hash=AAGD-XcBL8C3flflkmxjbzdr7_2W_i6CZ_3rM5zQpUCYaw&dl=1";     
DownloadManager.Request request = new  DownloadManager.Request(Uri.parse(urlDownload)); 
request.setDescription("Testando"); request.setTitle("Download"); 
request.allowScanningByMediaScanner(); 
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip"); 
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
final long downloadId = manager.enqueue(request); 
final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1); 

new Thread(new Runnable() { 

    @Override 
    public void run() { 
        boolean downloading = true; 
        while (downloading) { 
            DownloadManager.Query q = new DownloadManager.Query(); 
            q.setFilterById(downloadId); 
            Cursor cursor = manager.query(q); 
            cursor.moveToFirst(); 
            int bytes_downloaded = cursor.getInt(cursor .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
            int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
            if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) { 
                downloading = false; 
            } 
            final double dl_progress = (bytes_downloaded / bytes_total) * 100; 
            runOnUiThread(new Runnable() { 
                @Override 
                public void run() {
                    mProgressBar.setProgress((int) dl_progress); 
                } 
            }); 
            Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor)); 
            cursor.close(); 
        } 
    } 
}).start();

my statusMessage method:

private String statusMessage(Cursor c) { 
    String msg = "???"; 
    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) { 
        case DownloadManager.STATUS_FAILED: 
            msg = "Download failed!"; 
            break; 
        case DownloadManager.STATUS_PAUSED: 
            msg = "Download paused!"; 
            break; 
       case DownloadManager.STATUS_PENDING: 
            msg = "Download pending!"; 
            break; 
       case DownloadManager.STATUS_RUNNING: 
            msg = "Download in progress!"; 
            break; 
       case DownloadManager.STATUS_SUCCESSFUL: 
            msg = "Download complete!"; 
            break; 
       default: 
            msg = "Download is nowhere in sight"; 
            break; 
   } 
   return (msg); 
}
查看更多
登录 后发表回答