简体   繁体   English

使用凭据结帐 Jenkins Pipeline Git SCM?

[英]Checkout Jenkins Pipeline Git SCM with credentials?

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: 'git@bitbucket.org: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 控制台中配置任何内容。

You can use the following in a pipeline:您可以在管道中使用以下内容:

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

If you're using the ssh url then your credentials must be username + private key.如果您使用的是 ssh url,那么您的凭据必须是用户名 + 私钥。 If you're using the https clone url instead of the ssh one, then your credentials should be username + password.如果您使用的是 https 克隆网址而不是 ssh 网址,那么您的凭据应该是用户名 + 密码。

To explicitly checkout using a specific credentials使用特定凭据显式结帐

    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"
        }
    }

To checkout based on the configured 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 文件中使用这两个阶段。

If you want to use ssh credentials,如果要使用 ssh 凭据,

  git(
       url: 'git@github.com<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}"
    )

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
        }
    }
}

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.对于值得添加到讨论中的内容......我所做的最终帮助了我......因为管道在每次运行时清理的 docker 映像内的工作区中运行。 I grabbed the credentials needed to perform necessary operations on the repo within my pipeline and stored them in a .netrc file.我获取了对管道中的 repo 执行必要操作所需的凭据,并将它们存储在 .netrc 文件中。 this allowed me to authorize the git repo operations successfully.这使我能够成功授权 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...
    '''
}

It solved for me using它为我解决了使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM