简体   繁体   中英

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.

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 ). In this case, since abc repo is cloned latest, jenkins throws an error that it can't find the migrate.sh script. 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. I tried the other way, so that I can store the credentials in Jenkins and also clone the repo. Any help?

Thanks to @shane Bishop, here is the solution that finally worked

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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