Search code examples
jenkinsjenkins-pipelinejenkins-cli

Best way to clone or pull gitlab code using Jenkins to avoid merge issues


What is the best way to clone or pull gitlab code using Jenkins, I have this pipeline. However i am seeing merge issues popping up and then it ignored other builds. What is the best approach to do this. Below is my pipeline and errors:

pipeline {
    agent any

    environment {
        APPIUM_PORT_ONE= 4723
        APPIUM_PORT_TWO= 4724
    }

  tools {nodejs "node"}

  stages {
        stage('Checkout App 1') {
            steps {
                dir("/Users/Desktop/app1") {
                sh 'git pull ###'
                }
                echo "Building.."

            }
        }

        stage('Checkout App 2') {
            steps {
                dir("/Users//Desktop/app2") {
                echo "Building.."
                sh 'git pull ###'
                }
            }
        }

        stage('Checkout Mirror') {
            steps {
                echo "Building.."
            }
        }

        stage('Checkout End to End Tests') {
            steps {
                dir("/Users/Desktop/qa-end-to-end/") {
                sh 'git pull ###'
            }

            }
        }

        stage('Starting Appium Servers') {
            steps {
                parallel(
                    ServerOne: {
                    echo "Starting Appium Server 1"
                    dir("/Users/Desktop/qa-end-to-end/") {
                    }
                  },
                    ServerTwo: {
                    echo "Starting Appium Server 2"
                  })
                  }
              }

          stage('Starting End to End Tests') {
            steps {
                echo "Starting End to End Tests"
                dir("/Users/Desktop/qa-end-to-end/") {
                sh './tests.sh'
                echo "Shutting Down Appium Servers"
            }
          }
        }

        stage('Publish Report') {
            steps {
                echo "Publishing Report"
            }
        }
    }
}  

Should i clone from scratch instead of doing pull?. Any documentation would be helpful.


Solution

  • Unless the repos are large and time consuming to clone from scratch then I would do that. Then you are certain that you have clean correct code to run with

    checkout([$class: 'GitSCM',
      branches: [[name: '*/master']],
      doGenerateSubmoduleConfigurations: false,
      extensions: [[$class: 'CleanCheckout']],
      submoduleCfg: [],
      userRemoteConfigs: [[credentialsId: 'GIT', url: 'git@git.com:repo.git']]])
    

    Can either run this in you DIR block or add the extension to checkout to a subdirectory

    extensions: [[$class: 'RelativeTargetDirectory', 
            relativeTargetDir: 'checkout-directory']]
    

    Dont forget to delete the old checkouts if you are persisting workspaces across builds.