자격 증명으로 Jenkins Pipeline Git SCM을 확인 하시겠습니까? Could not read from remote repository. Please

나는 다음과 같은 한 이 자습서를 :

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

그러나 자격 증명을 추가하는 방법을 알려주지 않습니다. Jenkins에는 사용자 user & pass를 정의한 다음 작업에서 사용할 ID를 얻는 특정 “Credentials”섹션이 있습니다.하지만 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.

파이프 라인에서 자격 증명을 구성하는 방법이 있습니까, 아니면 Jenkin의 Linux 사용자의 .ssh / authorized_keys 파일에 SSH 키를 넣어야합니까?

이상적인 세계에서는 파이프 라인 작업 및 리포지토리 키에 대한 리포지토리를 확보 한 다음 Docker Jenkins를 시작하고 Jenkins 콘솔에서 아무것도 구성하지 않고도 이러한 작업과 키를 동적으로 추가하고 싶습니다.



답변

파이프 라인에서 다음을 사용할 수 있습니다.

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

ssh URL을 사용하는 경우 자격 증명은 사용자 이름 + 개인 키 여야합니다. ssh 대신 https 복제 URL을 사용하는 경우 자격 증명은 사용자 이름 + 비밀번호 여야합니다.


답변

특정 자격 증명을 사용하여 명시 적으로 체크 아웃하려면

    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 작업에서 구성된 자격 증명을 기반으로 체크 아웃하려면

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

단일 Jenkins 파일 내에서 두 단계를 모두 사용할 수 있습니다.


답변

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

답변

git 플러그인 GitSCM을 사용하여 빠른 예제 추가 :

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

파이프 라인에서

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

답변

토론에 추가 할 가치가있는 것은 … 내가 한 일이 결국 도움이되었습니다 … 파이프 라인이 실행될 때마다 정리되는 도커 이미지 내의 작업 공간 내에서 실행되기 때문입니다. 파이프 라인 내의 리포지토리에서 필요한 작업을 수행하는 데 필요한 자격 증명을 가져와 .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...
    '''
}

답변

그것은 나를 위해 해결되었습니다.

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