如何通过curl触发构建时设置Jenkins构建描述?

时间:2015-02-13 16:06:37

标签: curl jenkins jenkins-cli

我试图设置我触发的构建的构建描述,因为我开始构建,到目前为止我没有运气。

我遇到了一个解决方案(Adding text to the page of a build triggered by the Jenkins remote API),我有点按照这种方式工作(第一个命令将启动构建,第二个命令将设置最后一个构建的描述):

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

然而,问题在于,如果我刚刚开始的构建排队等待/不立即踢," lastBuild"不会引用我刚刚开始的构建,而是它之前的构建(仍在构建)。

所以我尝试过这样的事情:

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

但它并没有真正设定说明。

有关如何实现这一目标的任何想法?

我找到的其他解决方案,但我并不满意:

4 个答案:

答案 0 :(得分:11)

您始终可以拥有变量并将构建描述传递给初始调用中的变量。然后在构建结束时,将变量输出到控制台并使用 Description Setter plugin 进行捕获。

修改以澄清:

  • 安装 Description Setter plugin
  • 在作业配置中,配置一个String参数,将其命名为" MyDescription ",将默认值保留为空。
  • 在构建步骤的某个地方," 执行Shell "或" 执行Windows批处理命令"键入echo Desc: $MyDescriptionecho Desc: %MyDescription%,具体取决于您的操作系统。
  • 在制作后的步骤中,选择" 设置构建说明"。
    • 正则表达式设置为^Desc: (.*)
    • 说明设为\1
  • 从命令行触发:

curl -v -X POST --data-urlencode "MyDescription=This is my desc" "http://[myServer]/job/[jobName]/buildWithParameters"
(以上是一行)

答案 1 :(得分:0)

对于那些对使用Jenkins UI感兴趣的人,我正在尝试:

  1. https://wiki.jenkins-ci.org/display/JENKINS/Build+Name+Setter+Plugin
  2. https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
  3. Postbuild插件功能更强大,但需要Groovy修补和烫发。

答案 2 :(得分:0)

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

def currentBuild = Thread.currentThread().executable
def FOO = build.buildVariableResolver.resolve('FOO')
currentBuild.setDescription(FOO)

答案 3 :(得分:-4)

我的下载:

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

我的statusMessage方法:

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