简体   繁体   English

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

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

I am trying to clone a repo in a server using credentials stored in Jenkins and I am performing certain operations on that repo.我正在尝试使用存储在 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}
                    '''
                }
            }
        }
    }
}

The migrate.sh contains operations performed on the abc folder (cloned from https://github.com/abc ) while the migrate.sh resides in xyz repo (cloned from https://github.com/xyz ). migrate.sh 包含对abc文件夹(从https://github.com/abc克隆)执行的操作,而 migrate.sh 驻留在xyz库(从https://github.com/xyz克隆)。 In this case, since abc repo is cloned latest, jenkins throws an error that it can't find the migrate.sh script.在这种情况下,由于 abc repo 是最新克隆的,jenkins 会抛出一个错误,说它找不到 migrate.sh 脚本。 Is there any way to avoid this error?有什么办法可以避免这个错误吗? I tried to perform git clone --branch progress https://github.com/abc in the migrate.sh script, but it asks me for credentials.我尝试在git clone --branch progress https://github.com/abc脚本中执行git clone --branch progress https://github.com/abc ,但它要求我提供凭据。 I tried the other way, so that I can store the credentials in Jenkins and also clone the repo.我尝试了另一种方式,这样我就可以将凭据存储在 Jenkins 中并克隆 repo。 Any help?有什么帮助吗?

Thanks to @shane Bishop, here is the solution that finally worked感谢@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