Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get git commits for multiple repos using Multiple-SCM-Plugin in Jenkins

So, I have configured a jenkins job which checks out master branch from 3 repos. Now I want to get the latest changes for all the three repos.

Currently, the GIT_COMMIT env variable only gives the commit has for the last repo added in the configuration and not for all three of them.

Is there any way to get the previous commit and the current git commit for all the three repositories ?

like image 314
Jason Avatar asked Aug 06 '15 01:08

Jason


People also ask

How to use multiple SCM in Jenkins?

In the SCM section of the Jenkins job configuration screen, choose 'Multiple SCMs'. You'll then see a drop-down list of the available SCM plugins which can be configured similar to the way build steps are configured for a freestyle job. When chosen, each SCM plugin will present its normal set of configuration options.

How do I add multiple Git repository to Jenkins?

create a different repository entry for each repository you need to checkout (main project or dependancy project. for each project, in the "advanced" menu (the second "advanced" menu, there are two buttons labeled "advanced" for each repository), find the "Local subdirectory for repo (optional)" textfield.

Can we use multiple Git repos in a single Jenkins job?

Both Backend and Frontend are different Git repositories, initially managed by different teams. Both needs to be checked to the same root folder, so the building process will properly run. In order to do the it on Jenkins, Multiple SCMs Plugin Jenkins plugin can be used.


1 Answers

I ran into the same issue and decided to fork the Multiple SCMs plugin in order to fix it: https://github.com/JakeStoeffler/multiple-scms-plugin

If you wish, simply clone my repo and run mvn to build the HPI file (located at target/multiple-scms.hpi), which you can upload manually and install in Jenkins. If you'd rather do the tweaking yourself, clone the original repo directly, open up MultiSCM.java, and replace the code in the buildEnvVars() method with something like the following:

@Override
public void buildEnvVars(AbstractBuild<?,?> build, Map<String, String> env) {
    // Add each SCM's env vars, appending indices where needed to avoid collisions
    for (int i = 0; i < scms.size(); i++) {
        try {
            EnvVars currScmVars = new EnvVars();
            scms.get(i).buildEnvVars(build, currScmVars);
            for (Entry<String, String> entry : currScmVars.entrySet()) {
                if (env.containsKey(entry.getKey())) {
                    // We have a collision; append the index of this SCM to the env var name
                    env.put(entry.getKey() + "_" + i, entry.getValue());
                } else {
                    // No collision; just put the var as usual
                    env.put(entry.getKey(), entry.getValue());
                }
            }
        }
        catch(NullPointerException npe) {}
    }
}

Hopefully the comments are pretty self-explanatory there. Basically, the bug in the original code is that when you have multiple SCMs with the same environment variable names, the variables get overwritten as they're being iterated through. We work around this by preventing these overwrites and instead appending an index to the variable name.

Here's an example of how to use it: if our project has 3 Git SCMs configured, we can now access the latest commit hash of each Git repo individually by using the env vars GIT_COMMIT, GIT_COMMIT_1, and GIT_COMMIT_2. The appended indices correspond to the order of the SCMs in the project configuration in Jenkins.

Obviously this is a quick and dirty solution, but it works for what I needed to do. Feel free to customize it to fit your needs.

like image 125
Jake Stoeffler Avatar answered Sep 20 '22 03:09

Jake Stoeffler