如何在github组织Jenkins工作流中检出ssh remote并在Jenkinsfile中使用ssh凭据

Gab*_*oux 7 git github jenkins github-organizations jenkins-pipeline

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
    '''
  }
Run Code Online (Sandbox Code Playgroud)

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

致命:无法读取" https://github.com "的用户名:没有此类设备或地址

git remote -v来自上面的命令Jenkinsfile输出类似的东西origin https://github.com/MY_ORGANIZATION/MY_REPO.

我的Github Organizationgit配置如下所示: 库来源,GitHub的组织 - 詹金斯,配置

我发现了一些相关的问题:

Gab*_*oux 10

事实证明,该Github Organization项目仅使用https凭据Scan credentials(如上图所示).

解决方案是点击Advanced按钮,并sshCheckout credentials下拉列表中实际选择凭据而不是默认值- Same as scan credentials -.

高级 - 视图 - 结帐凭证

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

这样,checkout scm将使用ssh而且sshagent([jenkinsCredsId]) {块将按预期工作,允许您根据您的权限创建标记,获取和推送.:)