git - 如何在 github 组织 Jenkins 工作流中 check out ssh 远程并在 Jenkinsfile 中使用 ssh 凭据

标签 git github jenkins jenkins-pipeline github-organizations

我在 jenkins 中有一个 Github Organisation 项目。在我存储库的根目录下,我的 Jenkinsfile 看起来像这样:

node {

  def jenkinsCredsId = 'xxxxxxxx-yyyy-zzzz-aaaa-bbbbbbbbbbbb'

  stage 'Checkout'
  checkout scm
  // I also tried the following:
  // checkout scm: [$class: 'GitSCM', source: 'ssh://git@github.com:MY_ORGANISATION/jenkins-testing-temp.git', clean: true, credentialsId: jenkinsCredsId]

  stage 'Build'
  // generate some artefact (dist.zip)

  stage 'Release'

  sshagent([jenkinsCredsId]) {
    sh '''
    git remote -v // show remotes
    ssh-add -l // show currently loaded ssh keys fingerprints

    git fetch --all --tags // IT FAILS HERE

    CURRENT_BUILD_TAG="some_build/${BUILD_NUMBER}"
    git tag ${CURRENT_BUILD_TAG}

    git push --tags

    github-release release \
    --security-token ${GITHUB_RELEASE_TOKEN} \
    --user MY_ORGANIZATION \
    --repo MY_REPO \
    --tag ${CURRENT_BUILD_TAG} \
    --name ${CURRENT_BUILD_TAG}

    github-release upload \
    --security-token ${GITHUB_RELEASE_TOKEN} \
    --user MY_ORGANIZATION \
    --repo MY_REPO \
    --tag ${CURRENT_BUILD_TAG} \
    --name ${CURRENT_BUILD_TAG} \
    --file dist.zip
    '''
  }

这里有几行用于测试存储库访问,目前在 git fetch 部分失败,出现以下错误:

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

来自上述 Jenkinsfilegit remote -v 命令输出类似于 origin https://github.com/MY_ORGANIZATION/MY_REPO 的内容。

我的 Github 组织 git 配置如下所示: repository-sources-github-organization-jenkins-config

我发现了几个相关的问题:

最佳答案

事实证明,Github Organization 项目仅使用 https 凭据作为 Scan credentials(如上图)。

解决方案是点击 Advanced 按钮,并在 Checkout credentials 下 pull 列表中实际选择一个 ssh 凭证,而不是默认的 - 与扫描凭据相同 -

advanced-view-checkout-credentials

请注意,带星号的凭据是用户/密码条目,这就是我发现问题的方式。

这样,checkout scm 将使用 ssh 而不是 sshagent([jenkinsCredsId]) { block 将按预期工作,让您创建标签、获取和推送根据您的权利。 :)

关于git - 如何在 github 组织 Jenkins 工作流中 check out ssh 远程并在 Jenkinsfile 中使用 ssh 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618449/

相关文章:

Git 权限在 Docker 容器内被拒绝

github - Gollum Wiki - Redcarpet :Module 的未定义方法 `new'

jenkins - 如何在 Jenkins 中上传更新日志作为 Hockey 应用程序的发行说明

java - Maven 不接受加密的 Nexus 密码

linux - 什么 git 操作将我的更改从内核 3.14.y 移植到 3.15.y?

git - 在对 master 进行更改后创建一个 git 分支

android - 在此 GitHub 路径下找不到存储库

github 桌面不允许同步到远程存储库 - 无法访问或存储库被删除或重命名

unit-testing - Jenkins 和 JUnit 注释

git - 如何获取Git命令的运行时间?