Jenkins environment variable for 2 different repositories

Hello,

I am having a Jenkins build setup, where I use 2 PlasticSCM repositories. I am able to get the info through the environment variables for 1 repository, but is there a way to get information from the other repository also?

I am basically ticking the use multiple workspaces option.

Thanks for support!

In a Jenkins job, when you use multiple workspaces, each workspace is associated with a different SCM repository. Jenkins provides environment variables for the SCM repository that triggered the build, but it doesn’t provide environment variables for other SCM repositories used in the job.

However, I think you could use Jenkins Pipeline’s checkout step to manually checkout code from the other repository and then extract the information you need. :thinking:

Here’s an untested example:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                script {
                    def scmVars1 = checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/user/repo1.git']]])
                    def scmVars2 = checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/user/repo2.git']]])

                    echo "Repo1 Branch: ${scmVars1.GIT_BRANCH}, Commit: ${scmVars1.GIT_COMMIT}"
                    echo "Repo2 Branch: ${scmVars2.GIT_BRANCH}, Commit: ${scmVars2.GIT_COMMIT}"
                }
            }
        }
    }
}

Here, checkout is used to checkout code from two different Git repositories. The checkout step returns a map of SCM-related variables, which you should be able to use in your pipeline.

Thanks for the information. I will try it out!

1 Like