..

jenkins pipeline 中 clone 一个 git 库

目前找到了 4 种方式

方式1:


stage('git clone') {
    steps {
        sh 'mkdir -p Module1'
        dir("Module1"){
            git branch: "1.0/develop",
            credentialsId: '{credentialsId}',
            url: '{gitrepo}'
        }
        sh 'ls -al Module1'
    }
}

方式2:

在 jenkins 的构建节点上配置好 git 仓库的 ssh 你要,然后使用 ssh 认证的方式来 clone

git clone {repo} Module1

方式3: 使用 https 方式认证,在 url 中擦入账号和密码


stage('拉取devops-deploy') {
    sh "if [ -d ${devops_deploy} ]; then rm -rf ${devops_deploy}; fi"
    withCredentials([usernamePassword(credentialsId: '{credentialsId}', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
        def u = URLEncoder.encode(GIT_USERNAME,'UTF-8')
        def p = URLEncoder.encode(GIT_PASSWORD,'UTF-8')
        sh """
            git clone --branch ${params['deploy_tag']} https://${u}:${p}@hellojukay.cn/irp/SNC/devops-deploy.git ${devops_deploy}
        """
    }
}

方式4:

checkout([
        $class: 'GitSCM',
        branches: [[name: '*/master']],
        doGenerateSubmoduleConfigurations: false,
        extensions: [[$class: 'CleanCheckout']],
        submoduleCfg: [],
        userRemoteConfigs: [[credentialsId: '{credentialsId}', url: '{repo}']]
    ])