使用凭据结帐 Jenkins Pipeline Git SCM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38461705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:22:57  来源:igfitidea点击:

Checkout Jenkins Pipeline Git SCM with credentials?

gitjenkinssshjenkins-pipeline

提问by Render

I was following this tutorial:

我正在关注本教程

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

However it doesn't tell how to add credentials. Jenkins does have specific "Credentials" section where you define user user&pass, and then get ID for that to use in jobs, but how do I use that in Pipeline instructions?

但是,它没有说明如何添加凭据。Jenkins 确实有特定的“凭据”部分,您可以在其中定义用户用户和密码,然后获取要在作业中使用的 ID,但是我如何在流水线指令中使用它?

I tried with:

我试过:

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

no luck:

没运气:

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.

Is there a way configure the creds in pipeline, or do I have to put SSH-keys to Jenkin's Linux user's .ssh/authorized_keys file?

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

In ideal world I'd like to have a repository for pipeline jobs and repo-keys, then launch Docker Jenkins, and dynamically add these jobs and keys there without having to configure anything in Jenkins Console.

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

回答by Serban Constantin

You can use the following in a pipeline:

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

git branch: 'master',
    credentialsId: '12345-1234-4696-af25-123455',
    url: 'ssh://[email protected]:company/repo.git'

If you're using the ssh url then your credentials must be username + private key. If you're using the https clone url instead of the ssh one, then your credentials should be username + password.

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

回答by Upul Doluweera

To explicitly checkout using a specific credentials

使用特定凭据显式结帐

    stage('Checkout external proj') {
        steps {
            git branch: 'my_specific_branch',
                credentialsId: 'my_cred_id',
                url: 'ssh://[email protected]/proj/test_proj.git'

            sh "ls -lat"
        }
    }

To checkout based on the configred credentials in the current Jenkins Job

根据当前 Jenkins 作业中的配置凭据结帐

    stage('Checkout code') {
        steps {
            checkout scm
        }
    }

You can use both of the stages within a single Jenkins file.

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

回答by f-society

If you want to use ssh credentials,

如果要使用 ssh 凭据,

  git(
       url: '[email protected]<repo_name>.git',
       credentialsId: 'xpc',
       branch: "${branch}"
    )

if you want to use username and password credentials, you need to use http clone as @Serban mentioned.

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

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

回答by avivamg

Adding you a quick example using git plugin GitSCM:

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

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

in your pipeline

在你的管道中

stage('checkout'){
    steps{
        script{
            checkout
        }
    }
}

回答by madeyejm

For what it's worth adding to the discussion... what I did that ended up helping me... Since the pipeline is run within a workspace within a docker image that is cleaned up each time it runs. I grabbed the credentials needed to perform necessary operations on the repo within my pipeline and stored them in a .netrc file. this allowed me to authorize the git repo operations successfully.

对于值得添加到讨论中的内容......我所做的最终帮助了我......因为管道在每次运行时清理的 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...
    '''
}

回答by Sarang

It solved for me using

它为我解决了使用

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