使用凭据签出 Jenkins Pipeline Git SCM?

Posted

技术标签:

【中文标题】使用凭据签出 Jenkins Pipeline Git SCM?【英文标题】:Checkout Jenkins Pipeline Git SCM with credentials? 【发布时间】:2016-11-22 12:51:43 【问题描述】:

我在关注this tutorial:

node 
  git url: 'https://github.com/joe_user/simple-maven-project-with-tests.git'
  ...

但是它没有说明如何添加凭据。 Jenkins 确实有特定的“凭据”部分,您可以在其中定义用户 user&pass,然后获取 ID 以在作业中使用,但是如何在 Pipeline 指令中使用它?

我试过了:

git([url: 'git@bitbucket.org:company/repo.git', branch: 'master', credentialsId: '12345-1234-4696-af25-123455'])

运气不好:

stderr: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

有没有办法在管道中配置凭据,或者我必须将 SSH 密钥放入 Jenkins 的 Linux 用户的 .ssh/authorized_keys 文件中?

在理想情况下,我希望有一个用于管道作业和 repo-keys 的存储库,然后启动 Docker Jenkins,并在其中动态添加这些作业和密钥,而无需在 Jenkins 控制台中进行任何配置。

【问题讨论】:

【参考方案1】:

您可以在管道中使用以下内容:

git branch: 'master',
    credentialsId: '12345-1234-4696-af25-123455',
    url: 'ssh://git@bitbucket.org:company/repo.git'

如果您使用的是 ssh url,那么您的凭据必须是用户名 + 私钥。如果您使用的是 https 克隆 url 而不是 ssh,那么您的凭据应该是用户名 + 密码。

【讨论】:

解决了,谢谢。我不知道 SSH-url 和 HTTPS-url 需要不同的凭据才能使用! 这很有帮助,但 credentialsId 来自 /var/lib/jenkins/credentials.xml 中的 id,因为我不得不努力弄清楚。 @prayagupd,您应该能够从凭证页面 (http://yourjenkinsinstall/credentials) 获取凭证 ID。无需拖网配置文件。 您知道是否可以重复使用作业中定义的凭据? 对于那些询问“如何生成凭据 ID”的人。在这里如何找到它。 [1。单击 Jenkins 主页上的 Credentials,2。然后您将看到一个包含您创建的所有凭据的表。 3.ID在此表]【参考方案2】:

使用特定凭据明确结帐

    stage('Checkout external proj') 
        steps 
            git branch: 'my_specific_branch',
                credentialsId: 'my_cred_id',
                url: 'ssh://git@test.com/proj/test_proj.git'

            sh "ls -lat"
        
    

根据当前 Jenkins Job 中配置的凭据进行结帐

    stage('Checkout code') 
        steps 
            checkout scm
        
    

您可以在一个 Jenkins 文件中使用这两个阶段。

【讨论】:

如何生成这个credentialsId? 看看 - support.cloudbees.com/hc/en-us/articles/… 我应该在哪里存储凭证文件。 jenkins sais:警告:找不到 CredentialId“jenkins_key”。 @Dinu 凭据是在 Jenkins 中创建的,如果安装了插件,您应该在主菜单中看到它。 support.cloudbees.com/hc/en-us/articles/… 谢谢!一个张贴整个事情的人,而不是在这里一点一点地张贴,并希望人们神奇地知道该放什么。【参考方案3】:

为您添加一个使用 git 插件GitSCM 的快速示例:

    checkout([
        $class: 'GitSCM', 
        branches: [[name: '*/master']], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'CleanCheckout']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
    ])

在您的管道中

stage('checkout')
    steps
        script
            checkout
        
    

【讨论】:

您知道如何为整个团队使用全局凭据吗?或者有没有办法让任何开发者推送到 github,他们可以提供他们的凭据,而不必在 Jenkinsfile 中公开它 您可以在开发团队中管理与您自己的逻辑相关的机制,并为每个组使用不同的凭据密钥。例如:如果 Github 用户在 'backend_developers' 列表中使用 ,如果 Github 用户在 'frontend_developers' 列表中使用 ,请设计与您自己的用例相关的机制。 您会将这些凭据保存在哪里?是 Jenkins Credentials 插件吗? 使用 Jenkins 凭证文档 - jenkins.io/doc/book/using/using-credentials 我已经广泛搜索了一个像这样的简单checkout 示例,谢谢。【参考方案4】:

如果您想使用 ssh 凭据,

  git(
       url: 'git@github.com<repo_name>.git',
       credentialsId: 'xpc',
       branch: "$branch"
    )

如果你想使用用户名和密码凭证,你需要使用@Serban 提到的http clone。

    git(
       url: 'https://github.com/<repo_name>.git',
       credentialsId: 'xpc',
       branch: "$branch"
    )

【讨论】:

如何生成这个credentialsId? 我生成了这样的凭据:help.github.com/en/articles/…,我将公钥添加到了我的 git,但是我必须在哪里存储这个文件。 Jenkins 说:警告:找不到 CredentialId“jenkins_key”。 @DinuNicolae 请参考以下链接Adding new global credentials -&gt; 7.。jenkins.io/doc/book/using/using-credentials 不错的答案。给你点赞。 git(..) 克隆远程存储库。如何提交并将当前工作目录推送到某个存储库、某个分支。我有 credenitalsId 到目标存储库。【参考方案5】:

对于值得加入讨论的内容...我所做的最终帮助了我...因为管道在每次运行时都会清理的 docker 映像内的工作区中运行。我获取了在管道中对 repo 执行必要操作所需的凭据,并将它们存储在 .netrc 文件中。这使我能够成功授权 git repo 操作。

withCredentials([usernamePassword(credentialsId: '<credentials-id>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) 
    sh '''
        printf "machine github.com\nlogin $GIT_USERNAME\n password $GIT_PASSWORD" >> ~/.netrc
        // continue script as necessary working with git repo...
    '''

【讨论】:

【参考方案6】:

它为我解决了使用

checkout scm: ([
                    $class: 'GitSCM',
                    userRemoteConfigs: [[credentialsId: '******',url: $project_url]],
                    branches: [[name: 'refs/tags/$project_tag']]
            ])

【讨论】:

以上是关于使用凭据签出 Jenkins Pipeline Git SCM?的主要内容,如果未能解决你的问题,请参考以下文章

jenkins之-创建第一个pipeline

原jenkins知识点

jenkins 2:用ssh agent插件在pipeline里实现scp和远程执行命令

Linux开发者的CI/CD配置jenkins凭据从github拉取代码

jenkins自动化部署

在 Jenkins 中,如何将项目签出到特定目录中(使用 GIT)