如何在通过curl触发build时设置Jenkins build description?

如何在通过curl触发build时设置Jenkins build description?,curl,jenkins,jenkins-cli,Curl,Jenkins,Jenkins Cli,我正在尝试设置我正在触发的构建的构建描述,因为我正在启动构建,到目前为止我没有运气 我遇到了一个解决方案(),并以这种方式工作(第一个命令将启动构建,第二个命令将设置上一个构建的描述): 但是,问题是,如果我刚刚启动的构建进入队列/没有立即启动,“lastBuild”将不会引用我刚刚启动的构建,而是引用它之前的构建(仍然是构建) 所以我试过这样的方法: payload='json={""description"":""test description""}' curl -v -X POST -H

我正在尝试设置我正在触发的构建的构建描述,因为我正在启动构建,到目前为止我没有运气

我遇到了一个解决方案(),并以这种方式工作(第一个命令将启动构建,第二个命令将设置上一个构建的描述):

但是,问题是,如果我刚刚启动的构建进入队列/没有立即启动,“lastBuild”将不会引用我刚刚启动的构建,而是引用它之前的构建(仍然是构建)

所以我试过这样的方法:

payload='json={""description"":""test description""}'
curl -v -X POST -H "Content-Type: application/json" -d $payload "http://[myServer]/job/[jobName]/build"
但它实际上并没有设定描述

有什么办法可以做到这一点吗

我找到了其他解决方案,但我不太满意:

  • -这也是一种“触发后解决方案”,其工作可靠性与仅在触发后设置描述不同
  • -我想我可以建立一个检查,只在描述为空时修改它,这样我就不会覆盖它,但这看起来相当复杂。我想一定有更简单的解决办法,不是吗

您始终可以拥有一个变量,并在初始调用时将构建描述传递到该变量中。然后在构建结束时,将变量输出到控制台并使用捕获

编辑以澄清:

  • 安装
  • 在作业配置中,配置一个字符串参数,将其命名为“MyDescription”,将默认值保留为空
  • 在构建步骤中的某个位置,根据您的操作系统,“执行Shell”或“执行Windows批处理命令”键入
    echo Desc:$MyDescription
    echo Desc:%MyDescription%
  • 在生成后步骤中,选择“设置生成描述””。
    • 将正则表达式设置为
      ^Desc:(.*)
    • 将说明设置为
      \1
  • 从命令行触发器:
curl-v-X POST--data urlencode“MyDescription=这是我的描述”http://[myServer]/job/[jobName]/buildWithParameters“


(上面是一行)

对于那些对使用詹金斯用户界面感兴趣的人,我正在尝试:


  • Postbuild插件功能更强大,但需要Groovy修补和烫发。

    我也有同样的需求-在构建开始时设置构建描述
    请注意,生成描述设置程序插件作为生成后操作被激活,这对我来说太晚了。
    我解决这个问题的方法是对作业配置和Python脚本(但可以是任何语言)进行一个小的更改:

    • 在作业配置中添加UUID参数
    • 创建脚本以提交并设置说明(&S)
    脚本的作用如下:

  • 提交构建时,生成uuid值(unique,right?)并填充uuid参数
  • 轮询Jenkins作业(通过RESTAPI获取其JSON),在所有正在运行的构建上循环,查找我的(通过UUID的已知值)。通过超时限制轮询,这样我们就不会永远挂起
  • 使用Jenkins Java CLI设置描述(命令“set build description”)
  • 一直工作,除非构建排队(没有可用的执行器)并且上面设置的超时过期-我无能为力。

    另一个解决方案,使用“执行Groovy系统脚本”:


    我的下载:

    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();
    
    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); 
    }
    
    我的状态消息方法:

    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();
    
    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); 
    }
    

    对不起,我听不懂。。。您能否详细说明一下:“您可以始终拥有一个变量,并在初始调用时将构建描述传递到该变量中”?我该怎么做?澄清一下:每个触发的构建的构建描述都需要不同。@mac在回答中澄清了这一点,谢谢!尽管我必须说,我找到了一种方法,在触发远程构建时如何“正确”使用“cause”参数,所以现在我只是用它来代替描述(原因也可以包含http链接,这对我来说也很有趣)@mac