Using credentials from Jenkins store in a jenkinsfile

后端 未结 4 1832
轻奢々
轻奢々 2020-12-30 03:52

I made a multibranch pipeline project in Jenkins. I need to use two repositories and both need credentials.

I created a Jenkinsfile in repository1:

n         


        
相关标签:
4条回答
  • 2020-12-30 04:11

    What kind of credentials do you use?

    I suggest that you use SSH credentials (i.e. private/public keys):

    1. Generate a SSH key pair (make sure you generate it for the correct username!)
    2. Add your public SSH key to your Bitbucket account
    3. Configure your Jenkins to use your newly created SSH private key, as shown in the example below:

    Then you need to use SSH URL as connection to your Git your credentials in your pipeline (instead of HTTP URL), as follows:

    checkout([
        $class: 'GitSCM', branches: [[name: '*/master']],
        userRemoteConfigs: [[url:'ssh://BRNTZN@bitbucket.org:BRNTZN/repository2.git'],[credentialsId:'jenkins_ssh_key']]
    ])
    

    Also, note that you might want to set a specific id for your credentials (e.g. jenkins_ssh_key or BRNTZN_ssh_key) to improve readability and simplify pipeline configuration.

    0 讨论(0)
  • 2020-12-30 04:11

    As stated in Pipeline plugin tutorial, for a multibranch project, you don't have to specify the repository in the node. Just use checkout scm.

    0 讨论(0)
  • 2020-12-30 04:12

    Your GitSCM class instantiation is incorrect. You have created two UserRemoteConfig objects - one with a URL of 'git@bitbucket.org:BRNTZN/repository2.git' and one with a credentialsId of 'jenkinsmaster'. Instead you want one object with both properties set.

    checkout([
      $class: 'GitSCM', branches: [[name: '*/master']],
      userRemoteConfigs: [[url: 'git@bitbucket.org:BRNTZN/repository2.git'],[credentialsId:'jenkinsmaster']]
    ])
    

    Should be:

    checkout([
      $class: 'GitSCM', branches: [[name: '*/master']],
      userRemoteConfigs: [[url: 'git@bitbucket.org:BRNTZN/repository2.git',credentialsId:'jenkinsmaster']]
    ])
    

    Notice there are no brackets around the comma in the "userRemoteConfigs" section in the second case.

    I had just ran into the same issue and connected up an Eclipse debugger to Jenkins to find the issue.

    See git-plugin GitSCM does not support ssh credentials when using checkout in a Jenkinsfile (45007).

    0 讨论(0)
  • 2020-12-30 04:21

    I've had the exact same issue: checkout using credentials in a freestyle project works fine, checkout in a shell (as the jenkins user) works fine, and checkout in the pipeline fails. I've updated Jenkins + plugins to the latest version.

    I finally managed to get it to work by placing the correct key in /var/lib/jenkins/.ssh/id_rsa. It looks like the GitSCM plugin completely ignores the provided credentialsId, and just uses the key in /var/lib/jenkins/.ssh/id_rsa. I generated a keypair without passphrase for this.

    It is a workaround, and I suspect that GitSCM has a bug, but at least you can use the pipeline plugin.

    0 讨论(0)
提交回复
热议问题