首页 > 解决方案 > 使用来自 Jenkins 的凭据在 shell 脚本中进行 git clone

问题描述

我正在尝试使用存储在 Jenkins 中的凭据克隆服务器中的存储库,并且我正在对该存储库执行某些操作。

pipeline {
    options {
        skipDefaultCheckout()
        timestamps()
    }
    parameters {
        string(name: 'FILENAME', defaultValue: 'tmp', description: 'Enter the file name that needs to be copied')
        string(name: 'DB_NAME', defaultValue: 'tmp_db', description: 'Enter the database name that needs to be created')
        string(name: 'VERSION', defaultValue: '1', description: 'Enter the DB name version')
        choice(name: 'RUN', choices: 'Migrate Data', description: 'Data migration')
    }
    agent {
        node { label 'myserver' }
    }
    triggers {
        pollSCM('H/5 * * * *')
    }
    stages {
        stage('Clean & Clone') {
            steps {
                cleanWs()
                git(branch: 'jenkinsfilr-branch',
                        credentialsId: 'lsdeploy-github',
                        url: 'https://github.com/xyz')
            }
        }
        stage('Run the shell script On-prem'){
            steps {
                git(branch: 'progress',
                        credentialsId: 'lsdeploy-github',
                        url: 'https://github.com/abc')
                 }
                configFileProvider([configFile(fileId: 'env-on-prem', targetLocation: '.env')]) {
                    sh  '''
                            bash -x ./ops/database-migration/migrate.sh ${FILENAME} ${DB_NAME} ${VERSION}
                    '''
                }
            }
        }
    }
}

migrate.sh 包含对abc文件夹执行的操作(从https://github.com/abc克隆),而 migrate.sh 驻留在xyzrepo 中(从https://github.com/xyz克隆)。在这种情况下,由于 abc repo 是最新克隆的,所以 jenkins 会抛出找不到 migrate.sh 脚本的错误。有没有办法避免这个错误?我尝试git clone --branch progress https://github.com/abc在 migrate.sh 脚本中执行,但它要求我提供凭据。我尝试了另一种方式,以便我可以将凭据存储在 Jenkins 中并克隆存储库。有什么帮助吗?

标签: gitshelljenkinsgithubjenkins-pipeline

解决方案


感谢@shane Bishop,这是最终奏效的解决方案

pipeline {
    options {
        skipDefaultCheckout()
        timestamps()
    }
    parameters {
        string(name: 'FILENAME', defaultValue: 'tmp', description: 'Enter the file name that needs to be copied')
        string(name: 'DB_NAME', defaultValue: 'tmp_db', description: 'Enter the database name that needs to be created')
        string(name: 'VERSION', defaultValue: '1', description: 'Enter the DB name version')
        choice(name: 'RUN', choices: 'Migrate Data', description: 'Data migration')
    }
    agent {
        node { label 'myserver' }
    }
    triggers {
        pollSCM('H/5 * * * *')
    }
    stages {
        stage('Clean & Clone') {
            steps {
                cleanWs()
                git(branch: 'jenkinsfilr-branch',
                        credentialsId: 'lsdeploy-github',
                        url: 'https://github.com/xyz')
            }
        }
        stage('Run the shell script On-prem'){
            steps {
                sh 'cp ./ops/database-migration/on-prem/migrate.sh ./'
                git(branch: 'progress',
                        credentialsId: 'lsdeploy-github',
                        url: 'https://github.com/abc')
                 }
                configFileProvider([configFile(fileId: 'env-on-prem', targetLocation: '.env')]) {
                    sh  '''
                            bash -x migrate.sh ${FILENAME} ${DB_NAME} ${VERSION}
                    '''
                }
            }
        }
    }
}

推荐阅读