Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git feature branches work with jenkins-workflow?

I'm trying to setup jenkins-workflow to do our integration tests. Our integration tests work like this:

Someone makes a change to LibraryA in a feature branch in git. We would like jenkins to run the unit tests against the code in the feature branch, then we would like to install the code from this feature branch into client1 and client2 (which are users of LibraryA) and run their tests.

I was able to setup a workflow to do everything except get the right commit to the feature branch of LibraryA. Instead, my setup just pulls a commit from some (seemingly random) branch of LibraryA.

We have lots of feature branches, so hardcoding a particular branch in the workflow setup doesn't see appropriate. It seems like there should be some way to get the hash of the commit that triggers the workflow job (even when using SCM polling).

My setup looks like this:

currentBuild.setDisplayName("#" + env.BUILD_NUMBER)

node {
  git credentialsId: '033df7f1-7752-46bd-903d-8a70e613eed0', url: '[email protected]:mycompany/myrepo.git'
  sh '''
echo `git rev-parse HEAD` > libraryA_version.txt
sudo docker run --rm=true -e LANG=en_US.UTF-8 -a stdout -i -t mycompany/libraryA run_tests
'''
  archive 'libraryA_version.txt'
}

def integration_jobs = [:]

integration_jobs[0]={
  node{
    ws {
      unarchive mapping: ['libraryA_version.txt':'.']
      sh 'sudo docker run -t --rm mycompany/client1:v1 bash run_tests.sh "`cat libraryA_version.txt`"'
    }
  }
}

integration_jobs[1] = {
  node{
    ws {
      unarchive mapping: ['libraryA_version.txt' : '.']
      sh 'sudo docker run -t --rm mycompany/client2 run_tests.sh "`cat libraryA_version.txt`" '
    }
  }
}

parallel integration_jobs

So, my current question is how can I setup the git repo/polling to get the right commit to run in the first test, which will be used in libraryA_version.txt in subsequent tests?

Alternatively, should I go about this process in a completely different way?

like image 486
Robert Avatar asked Sep 28 '22 09:09

Robert


1 Answers

As @maybeg hinted, what you are most likely looking for is a multibranch project, available by installing Pipeline: Multibranch. That lets you define the script in a Jenkinsfile which simply calls checkout scm one or more times in your build, avoiding the need for libraryA_version.txt.

In the meantime, I am left wondering about your project setup. Your git step is using the default of branch: 'master', meaning it should only be polling on the master branch to begin with, and only checking out this branch. The Git plugin is rather complicated, so perhaps this is broken somehow. Pending proper multibranch support, you could always use the checkout step with a GitSCM configured to use a wildcard branch spec, in which case an arbitrary branch head not previously built will be selected for checkout, and your git-rev-parse trick (I guess you independently rediscovered the workaround for JENKINS-26100) ought to work as is.

like image 89
Jesse Glick Avatar answered Nov 01 '22 13:11

Jesse Glick