1

我有以下管道文件:

node('git') {
    stage('Set Git Config') {
        sh 'git config --global user.email "jenkins@test.com"'
        sh 'git config --global user.name "jenkins"'
        sh 'git config --global credential.helper cache'
        sh "git config --global credential.helper 'cache --timeout=3600'"
    }
    stage('Set Git Credentials') {
        git credentialsId: 'gitlab1', url: '${GITLAB1_REPO}'
        git credentialsId: 'gitlab2', url: '${GITLAB2_REPO}'
    }
    stage('Synchronize with Gitlab2'){
        sh 'git clone --bare ${GITLAB1_REPO} tfs'
        dir("tfs") {
            //add a remote repository
            sh 'git remote add --mirror=fetch second ${GITLAB2_REPO}'
            // update the local copy from the first repository
            sh 'git fetch origin --tags'

            // update the local copy with the second repository
            sh 'git fetch second --tags'

            // sync back the second repository
            sh 'git push second --all'
            sh 'git push second --tags'
        }
    }
}

第 1 阶段和第 2 阶段工作完美。第 3 阶段失败,权限被拒绝。

我觉得这很奇怪,因为在第 2 阶段,我已经可以看到最后一次提交是什么,因此它表明凭据确实有效。他们为什么不在第三阶段工作?

这是我看到的错误:

git clone --bare git@bitbucket.test/test.git tfs 克隆到裸存储库“tfs”...权限被拒绝(公钥)。致命:无法从远程存储库中读取。

在第 2 阶段,我看到:

git config core.sparsecheckout # timeout=10 git checkout -f 30f1a7d1b77ef64e1cd44eab11a6ef4541c23b43 git branch -a -v --no-abbrev # timeout=10 git branch -D master # timeout=10 git checkout -b master 30f1a7d1b77ef64e1cd44eab143a6ef4541c2 Commit message: "test commitc2 "

4

1 回答 1

2

第 1 阶段 - 在 shell 中添加一些设置到本地 git

第 2 阶段 - 您指向要使用的实际凭据并使用 Jenkins 插件 - 这将正常工作

Satge 3 - 返回 shell,jenkins 没有提供凭据,因此上下文是从属/本地 jenkins 用户。

解决方案是withCredentials用于用户名和密码或sshagent(credentials...)私钥

// credentialsId here is the credentials you have set up in Jenkins for pushing
// to that repository using username and password.
withCredentials([usernamePassword(credentialsId: 'git-pass-credentials-ID', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
    sh("git tag -a some_tag -m 'Jenkins'")
    sh('git push https://${GIT_USERNAME}:${GIT_PASSWORD}@<REPO> --tags')
}

// For SSH private key authentication, try the sshagent step from the SSH Agent plugin.
sshagent (credentials: ['git-ssh-credentials-ID']) {
    sh("git tag -a some_tag -m 'Jenkins'")
    sh('git push <REPO> --tags')
}
于 2019-05-17T10:49:00.667 回答