Jenkins and multiple git branches?

I’m working on a Jenkins job and i need it to build two branches in the same repository. How can i do this?

  • How to find the current git branch in detached HEAD state
  • Opposite of git branch --set-upstream
  • Should I use Git branches to store unrelated code?
  • Git rebasing branch so commits and unstaged changes do not get deleted or lost
  • git push origin tries to push into remote main branch instead of creating a new one
  • How git branches and tags are stored in disks?
  • How can I successfully checkout my coworker's repository?
  • git switch which repository is active
  • Git: text=auto and “warning: LF will be replaced by CRLF”
  • Customize gitk for each repository
  • When deploying to Azure from git, how do I store my secrets?
  • I've git clone, now what?
  • 4 Solutions collect form web for “Jenkins and multiple git branches?”

    Considering that Jenkins cannot execute one job on two versions of a repository (which is what would be getting the content of two different branches), I would suggest making two different jobs, one for each branch (with the Jenkins Git plugin).

    Are you asking if you want to build two branches in the same checked-out workspace directory?

    If in the same workspace directory, all you need to do is create a script that will checkout one branch, build it, and when done, checkout the next branch and then build it.

    This single script will then be the one to be called by the single Jenkins job.

    For example, your build script would look something like this:

    git clone url:repo.git workspace
    cd workspace 
    git checkout branchA
    make
    # now you're done building branchA
    # next checkout branchB and run make
    git checkout branchB
    make
    # now you are done building branchB
    

    Each checkout will install the files for the branch desired. And this will build it accordingly. However, since they will be sharing the same workspace directory, this could mean that new files would be produced by the first build which will then be present when the second build runs. I assume this is the effect that you want because you wanted to build two branches in the same workspace directory.

    Update: Use git clean -xdf if you want to have a fresh workspace for the next build.

    This plugin will build every branch for you: https://wiki.jenkins-ci.org/display/JENKINS/Multi-Branch+Project+Plugin.

    You can also use the GitLab (https://wiki.jenkins-ci.org/display/JENKINS/GitLab+Plugin) / GitHub (https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Plugin) plugins, which can trigger a build when code is commited, and will then build the branch that was comitted to.

    IF you want the above AND want the advanced feature where Jenkins builds any new branch and merges that to master ONLY if the build succeeds(ie. you want two master branches and two builds), you may want to think about just forking the repository and merging changes between repositories(git is built for that kind of thing though I have not quite gotten to that point of usage yet).

    So you would basically have 2 git repositories and two jenkins builds and you can configure the git plugin to build from any branch in each repository and merge into master of that repository. So, each repository is acting like a branch in this configuration.

    That is our plan when we start a new release line 2.x from our 1.x release line at any rate is to fork. We will see how it works in practice.

    later,
    Dean

    Git Baby is a git and github fan, let's start git clone.