1

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?

4
  • Can you expose the credentials in an environment variable? If so, you can use this entry from the Git FAQ. Commented Jul 2, 2021 at 0:50
  • You say that your pipeline throws an error saying that it can't find migrate.sh, but then you say later that when you add a new line to migrate.sh, then you are asked for credentials. How do you manage to run migrate.sh if the file isn't found? Commented Jul 2, 2021 at 1:07
  • 1
    I think what you need to do is cd to the xyz directory before you run the migrate.sh script. Or alternatively you could cp the migrate.sh script to your working directory, and then run it. Commented Jul 2, 2021 at 1:09
  • Thank you @ShaneBishop, this was so simple and it worked Commented Jul 2, 2021 at 14:03

1 Answer 1

1

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}
                    '''
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.