首页 > 解决方案 > 无法在 Jenkins 作业中进行 git clone

问题描述

我正在尝试仅克隆最后一次提交,但在 jenkins 作业中出现以下错误:

+ git config credential.username ****
+ git config user.name jenkins
+ git config user.email jenkins@comp.com
+ git remote add origin https://bitbucket.org/base/my-repo.git

[Pipeline] sh
+ GIT_ASKPASS=true
+ git clone -b master --single-branch https://bitbucket.org/base/my-repo --depth=1 --bare ./last_commit
Cloning into bare repository './last_commit'...

fatal: could not read Username for 'https://bitbucket.org': No such device or address

这是我用作 Jenkins 共享库的函数:

def call(url, branch, gitCredentialId) {
    try {
        withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: gitCredentialId, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
            sh("""                          
                git init
                git config credential.username ${env.GIT_USERNAME}
                git config user.name \"jenkins\"
                git config user.email \"jenkins@comp.com\"
                git remote add origin ${url}.git
            """)

            sh(returnStdout: false, script: """
                GIT_ASKPASS=true

                git clone -b ${branch} --single-branch ${url} --depth=1 --bare ./last_commit
                """)

            def email = sh(returnStdout: true, script: """
                cd ./last_commit
                git log -1 --format='%ae'
                """)

            return email.trim()
        }
    } finally {
        sh """
            git config --unset credential.username
            git config --unset credential.helper
        """
    }
}

我在这里错过了什么?有没有人遇到过同样的问题?我想做的就是获得最后一封提交者的电子邮件。

请注意,我在这里读过这篇文章致命:无法读取用户名,没有这样的设备

虽然它与提到的错误相同,但不同之处在于,在那篇文章中,OP 面临已签出代码的错误,他们尝试推送,而我在签出代码时没有问题(使用git checkout)。我的问题是我无法克隆存储库并且没有预先存在的源

标签: gitjenkinsgit-clone

解决方案


推荐阅读