如何从groovy

时间:2017-09-22 18:40:19

标签: git jenkins groovy jenkins-pipeline

我有一个文件,其中存储了git push命令的输出。它的输出看起来像这样,

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/...
   99d7..344  <branch> -> <branch>
Branch newBranch set up to track remote branch newBranch from origin.

我将此输出存储在一个变量中,现在我想对该变量进行操作并仅获取URL,

https://githhub.com/...

它是Jenkins管道代码。 谢谢。

1 个答案:

答案 0 :(得分:0)

我确定使用Jenkins或Github提供的某些API有更好的方法,但如果不可用,如果您知道该模式将保持一致,我会建议使用正则表达式

    String foo = "";
    String bar = "Counting objects: 3, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (2/2), done.\nWriting objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.\nTotal 3 (delta 0), reused 0 (delta 0)\nTo https://github.com/...\n   99d7..344  <branch> -> <branch>\nBranch newBranch set up to track remote branch newBranch from origin.";
    Pattern p = Pattern.compile("^to\\s+(.*$)",Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
    Matcher m = p.matcher(bar);
    if(m.find()) {
        foo = m.group(1);
        System.out.println(m.group(1));
    }
    System.out.println(foo);
    Output: https://github.com/...