jenkinsfile 由于 git safe.directory 要求而出错

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

团队, 任何提示我如何解决这个问题,因为我什至尝试了根据错误日志的建议,但没有任何帮助。

基本上,我需要解决这个问题

git config --global --add safe.directory /home/jenkins/agent/workspace/test-pipeline

16:50:00  + git diff-tree --no-commit-id --name-only -r 66a80a67670f9cdfe73e637882e19693bfb4
16:50:00  + grep -v OWNERS
16:50:00  + grep -v gitignore
16:50:00  fatal: detected dubious ownership in repository at '/home/jenkins/agent/workspace/test-pipeline'
16:50:00  To add an exception for this directory, call:
16:50:00  
16:50:00    git config --global --add safe.directory /home/jenkins/agent/workspace/test-pipeline

我做了下面的事情,但没有运气

def gitCheckout(String gitRepository, String gitRefSpec, String jenkinsCredentialId=DEFAULT_JENKINS_GIT_CREDENTIALS){
        checkoutInfo = checkout([
          $class: 'GitSCM',
          branches: [[name: 'FETCH_HEAD']],
          doGenerateSubmoduleConfigurations: false,
          extensions: [
            [$class: 'CleanBeforeCheckout'],
            [$class: 'CloneOption', shallow: true, noTags: true, depth: 2, honorRefspec: true]
          ],
          submoduleCfg: [],
          userRemoteConfigs: [[
            credentialsId: jenkinsCredentialId,
            name: '',
            refspec: gitRefSpec,
            url: gitRepository]]
        ])
        return checkoutInfo.GIT_COMMIT
}
stages
{
    stage('Checkout') {
        steps {
            container('main') {
            script {
                git config --global --add safe.directory "/home/jenkins/agent/workspace/test-pipeline"
                workspace = pwd()

                if (params.GERRIT_BRANCH != "main"){
                echo "INFO: Changes will be processed from git hash"
                }
                git_revision = gitCheckout(params.GERRIT_REPO, params.GERRIT_BRANCH)
                echo "GitRevision: ${git_revision}"

                if (params.GERRIT_BRANCH != "main" && ! params.TF_TEMPLATE_PATH){
                    echo "INFO: Changes will be processed from git hash"
                    changed_files = sh(script: "git diff-tree --no-commit-id --name-only -r ${git_revision} |grep -v 'OWNERS' |grep -v 'gitignore'", returnStdout: true).split("\n")
                    echo "Changed Files: ${changed_files}"
                }
            }
            }
        }
    }
git jenkins-pipeline jenkins-groovy jenkins-job-dsl
1个回答
0
投票

你可以做

AFTER CHECKOUT/after actual files from git is on filesystem

git config --global --add safe.directory "*"

发生此错误的原因是您与一个用户一起检出目录(您尚未提供完整的管道),然后尝试与另一用户一起使用文件

container('first') {
git checkout
}

container('second') {
 git add . or anything else 
}

first
镜像中的容器在 dockerfile 中有用户“BOB”

second
镜像中的容器在 dockerfile 中有用户“NICK”

git 修复了安全性,因此当尝试对当前用户不拥有的文件执行 git 操作时,您必须执行

git config --global --add safe.directory "$(pwd)"

更多信息这里

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