【问题标题】:How to checkout ssh remote in github organization Jenkins workflow and use ssh credentials in Jenkinsfile如何在 github 组织 Jenkins 工作流程中签出 ssh 远程并在 Jenkinsfile 中使用 ssh 凭据
【发布时间】:2017-03-29 20:14:58
【问题描述】:

我在 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 部分失败,并出现以下错误:

致命:无法读取“https://github.com”的用户名:没有这样的设备或地址

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

我的Github Organization git 配置如下所示:

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

【问题讨论】:

    标签: git github jenkins jenkins-pipeline github-organizations


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 1970-01-01
      • 2023-03-06
      • 2020-09-29
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 2017-11-06
      • 2017-12-09
      相关资源
      最近更新 更多