Jenkins pipeline clone and publish Git commit

I have been in a situation when a need to trigger CI/CD build on let's say repository B, on successful run of CI/CD build of repository A. It is worth to mention that CI/CD pipeline is built on OpenShift layer and executed by Jenkins server.
So, what we have repository a with data science models, and repository B with microservice logic to execute these models (prediction, recommendation). To avoid data scientist to merge code from one repo to another I decided to use a Jenkins pipeline.

Let's start from the beginning:
the first step is to clone you second repository (B), if you will do it with command like:

// Clone repo
git url: 'https://github.com/jglick/simple-maven-project-with-tests.git' ...

It won't work. Yes it will clone a repository, however it will overwrite you cloned repository A even, if you try to do it from different folder.

That's why for cloning I used checkout functionality:


// Clone repo
checkout([$class: 'GitSCM', 
                branches: [[name: '*/master']], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [[$class: 'RelativeTargetDirectory', 
                              relativeTargetDir: '../created_for_repo_dir']], 
                submoduleCfg: [], 
                userRemoteConfigs: [[url: 'https://some_remote_address_of_B.git', 
                                     credentialsId: context.credentialsId]]])


Ok my stuff is there, I do my modiffication on it. How to push it pack?

Difficulties: Jenkins Publisher plugin does not support pipeline logic so there is a need to use a fix.

The fix is next:



// Push
withCredentials([[$class  : 'UsernamePasswordMultiBinding', credentialsId: context.credentialsId,
                      variable: 'CREDENTIALS', 
                      usernameVariable: 'USERNAME', 
                      passwordVariable: 'PASSWORD']]) {
        sh '''
             git config --local credential.username some_user
             git config --local credential.email "some_user@email.com"
             git config --local credential.helper 'store --file=${env.CREDENTIALS}'
                


             // my logic ...commit
             git -c core.askpass=true push "https://${USERNAME}:${PASSWORD}@bitbucket......git" --all
             git config --local --remove-section credential
           '''
}

Comments

Popular posts from this blog

Install Kubeflow locally

RabbitMQ and OpenShift