如何在内联 groovy 脚本中添加 git 克隆操作

How can I add git clone operation in inline groovy script

我想在 jenkins 中使用内联 groovy 脚本克隆 repo。如何执行 git 克隆并使用 groovy.

构建应用程序

如果您使用的是 Jenkins 管道,请参阅官方 documentation 中的示例,例如:

node {
    stage('Clone sources') {
        git url: 'https://github.com/jfrogdev/project-examples.git'
    }
}

对于简单的 Groovy 脚本,您可以尝试类似的操作:

 ["git", "clone", "https://github.com/jfrogdev/project-examples.git"].execute()

我也做了类似的事情:

jenkinsfile/或管道脚本代码:

node
{
    stage('Checkout')
    {
        GitManager.clone(this, "https://github.com/jfrogdev/project-examples.git", "*/master", "myGitUserID");
    }
}

实用程序class代码(来自sharedLib):

class GitManager
{
    public static void clone(Script scriptRef, String gitURLString, String branchID, String gitUserID)
    {
        scriptRef.checkout([
            $class: 'GitSCM', 
            branches: [[name: branchID]], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'CleanCheckout']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: gitUserID, url: gitURLString]]
        ])
    }
}