Program Club

자격 증명으로 Jenkins Pipeline Git SCM을 확인 하시겠습니까?

proclub 2020. 10. 18. 19:49
반응형

자격 증명으로 Jenkins Pipeline Git SCM을 확인 하시겠습니까?


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

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을 사용하는 경우 자격 증명은 사용자 이름 + 비밀번호 여야합니다.


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

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

    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 파일 내에서 두 단계를 모두 사용할 수 있습니다.


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

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

파이프 라인에서

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

참고URL : https://stackoverflow.com/questions/38461705/checkout-jenkins-pipeline-git-scm-with-credentials

반응형