繁体   English   中英

使用 Jenkins 的凭据在 shell 脚本中执行 git clone

[英]git clone in shell script using credentials from Jenkins

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

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 驻留在xyz库(从https://github.com/xyz克隆)。 在这种情况下,由于 abc repo 是最新克隆的,jenkins 会抛出一个错误,说它找不到 migrate.sh 脚本。 有什么办法可以避免这个错误吗? 我尝试在git clone --branch progress https://github.com/abc脚本中执行git clone --branch progress https://github.com/abc ,但它要求我提供凭据。 我尝试了另一种方式,这样我就可以将凭据存储在 Jenkins 中并克隆 repo。 有什么帮助吗?

感谢@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}
                    '''
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM