Checkout Jenkins Pipeline Git SCM with credentials?

后端 未结 6 1692
情深已故
情深已故 2020-12-04 08:20

I was following this tutorial:

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

However it doe

相关标签:
6条回答
  • 2020-12-04 08:45

    If you want to use ssh credentials,

      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.

        git(
           url: 'https://github.com/<repo_name>.git',
           credentialsId: 'xpc',
           branch: "${branch}"
        )
    
    0 讨论(0)
  • 2020-12-04 09:00

    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. If you're using the https clone url instead of the ssh one, then your credentials should be username + password.

    0 讨论(0)
  • 2020-12-04 09:02

    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 configred credentials in the current Jenkins Job

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

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

    0 讨论(0)
  • 2020-12-04 09:02

    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.

    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...
        '''
    }
    
    0 讨论(0)
  • 2020-12-04 09:07

    Adding you a quick example using git plugin 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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:08

    It solved for me using

    checkout scm: ([
                        $class: 'GitSCM',
                        userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
                        branches: [[name: 'refs/tags/${project_tag}']]
                ])
    
    0 讨论(0)
提交回复
热议问题