如何在Jenkins Git插件中指定Job DSL的checkout超时时间?

11

可以使用以下方法指定克隆超时时间:

git {
    ...
    cloneTimeout(60)
}

60代表超时时间为60分钟。我看到结账超时时间也可以指定,但是我找不到详细信息。使用checkoutTimeout(...)timeout(...)都会报错。

编辑

我可以通过Jenkins GUI设置结账超时时间(配置--> SCM--> Git--> 附加行为--> 高级结账行为--> 超时)。我想在为Jenkins生成Docker配置的Groovy脚本中执行相同的操作:

...
public class DockerJob {
...
    multiscm {
        git {
            remote {
                url(...)
                branch(...)
                ...
            }
            shallowClone()
            cloneTimeout(60)
            // Add "checkout timeout" here...
        }
        ...
    }
    ...
}
...

你是在问如何通过Jenkins UI的作业配置来完成这个操作吗? - rgulia
5个回答

17

由于CheckoutOption对我来说无法工作,我不得不通过管道将其更改为以下方式:

extensions: [[$class: 'CloneOption', timeout: 120]]

完整的检出代码

checkout([$class: 'GitSCM', branches: [[name: '*/master']],
            extensions: [[$class: 'CloneOption', timeout: 120]], gitTool: 'Default', 
            userRemoteConfigs: [[credentialsId: key, url: repo]]
        ])

8

经过一些实验,我找到了下面展示的解决方案。

总结

可以通过Jenkins GUI(Configuration --> SCM --> Git --> Additional Behaviors --> Advanced Checkout Behaviors --> Timeout)设置检出超时时间。我想在为Jenkins生成Docker配置的Groovy脚本中执行相同的操作。该脚本已经设置克隆超时时间。

...
public class DockerJob {
...
    multiscm {
        git {
            remote {
                url(...)
                branch(...)
                ...
            }
            shallowClone()
            cloneTimeout(60)
            // Add "checkout timeout" here...
        }
        ...
    }
    ...
}
...

显而易见的是,
...
// "Checkout timeout"
checkoutTimeout(60)
...

没有起作用。通常设置超时时间

...
// "Checkout timeout"
timeout(60)
...

也没有起作用。然后在一个网页上评论会导致:
...
// "Checkout timeout"
extensions {
    checkoutOptions {
        timeout(60)
    }
}
...

那也没有起作用。最后...
解决方案
...
public class DockerJob {
...
    multiscm {
        git {
            remote {
                url(...)
                branch(...)
                ...
            }
            shallowClone()
            cloneTimeout(60)
            // "Checkout timeout"
            configure { node ->
                node / 'extensions' << 'hudson.plugins.git.extensions.impl.CheckoutOption' {
                    timeout 60
                }
            }
        }
        ...
    }
    ...
}
...

2
使用工作流插件,像这样做怎么样?
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CheckoutOption', timeout: 100]], submoduleCfg: [], userRemoteConfigs: [[]]])

为了简单起见,我希望有一个类似于克隆超时已经存在的解决方案(用于结账超时)。 - Petr Vepřek

0

添加这个扩展对我有用。

extensions: [[$class: 'CloneOption', timeout: 60]]

0

在Jenkins管道脚本中,以下的结账配置对我来说非常完美。我们使用Stash1作为内部Git服务器,就像GitHub一样。请将其替换为您自己的。

stage('Checkout') {
            steps {
                echo "Running checkout stage"
                checkout([$class: 'GitSCM', branches: [
                    [name: "*/${params.branch}"]
                ], doGenerateSubmoduleConfigurations: false, extensions: [
                    [$class: 'CleanCheckout'], [$class: 'CloneOption', timeout: 30, shallow: true]
                ], submoduleCfg: [], userRemoteConfigs: [
                    [credentialsId: 'ink_bot', url: "ssh://git@stash1.XYZ.com:7999/int_sparktp/${params.repo}.git"]
                ]])
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接