【问题标题】:Jenkins git credentials work in stage, not in the next one詹金斯 git 凭证在阶段工作,而不是在下一个
【发布时间】:2019-05-17 08:58:07
【问题描述】:

我有以下管道文件:

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 阶段,我已经可以看到最后一次提交是什么,因此这表明凭据确实有效。他们为什么不在第 3 阶段工作?

这是我看到的错误:

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 30f1a7d1b77ef64e1cd44eab11a6ef4541c23b43 提交信息:“测试提交”

【问题讨论】:

  • 添加更多信息。您在第 3 阶段无法访问的部分以及有关错误的更多信息。

标签: jenkins jenkins-pipeline


【解决方案1】:

第 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')
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多