Jenkins管道共享库-使用Groovy .contains(git ls-remote)

问题描述 投票:0回答:1

[目前在尝试匹配git ls-remote的结果时遇到问题。当前正在测试一个回购的存在,但是在测试期间,我有6个回购来测试一个不存在的回购。但是,在运行代码时,git ls-remote报告未找到不存在的存储库,但该函数仍被视为true。

我已经尝试了很多事情,包括将模式和git ls-remote的输出设置为小写。以及修剪两个结果。以前我在脚本末尾没有“ || true”,但是当它找不到存储库时,它会在到达if语句之前失败。

def is_repository(repository_name)
{
    sshagent(['03876f99-2a9d-4980-b543-f8b478d4f1ec'])
    {
        String repository = sh( script: "git ls-remote [email protected]:<company_name>/${repository_name}.git || true", returnStdout: true)
        println(repository)
        String pattern = 'and the repository exists.'
        if(repository.contains(pattern))
        {
            println("Repository: " + repository_name + " not found")
            return false
        }
        else
        {
            println("Repository: " + repository_name + " found")
            return true
        }
    }
}

预期的输出,如果它在git ls-remote的输出中找到“未找到”,则返回false。

jenkins jenkins-groovy
1个回答
0
投票

这是一个简单的。使用jenkins sh()调用时,可以定义returnStdout但不能定义Stderror,因为Stderror只是打印到日志中而不传递给变量。

© www.soinside.com 2019 - 2024. All rights reserved.