Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile with two git repositories

Tags:

I'm using the Jenkins pipeline plugin with a Jenkinsfile.

In one repository, called vms.git, I have the Jenkinsfile and an application it builds.

I have another repository called deploy.git, which contains scripts I want to use to deploy the application in vms.git.

At the moment my Jenkinsfile just looks like this

node {   stage 'build'   checkout scm 

and I am defining the vms.git repo in the job configuration.

So what I would like to do is check out both repositories, then use the Jenkinsfile in vms.git to define the rest of the build. I want to reuse the deploy.git scripts in other pipelines so I don't want to put a Jenkinsfile in there.

like image 686
Mark Chorley Avatar asked May 23 '16 16:05

Mark Chorley


People also ask

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.

Can I have multiple Git repositories?

With Git, using multiple repositories is the only way to work efficiently. This enables each team to work independently, and do their work faster. You can also make sure that developers only have access to the repositories they need access to (thus making Git more secure.)


2 Answers

You can checkout multiple directories using checkout, but you have to specify directory where you want checkout this. You can generate snippets using jenkins (Snippet generator bellow script field). Choose checkout, next git repository and in Additional Behaviours choose: checkout into sub directory.

When you will have 2 repositories you can load script from repository you want usin load. Example:

node {     // first repository     checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory1']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo1.git']]])     // second repository     checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo2.git']]])     // run first script     load 'subdirectory1/Jenkinsfile'     // run second script     load 'subdirectory2/Jenkinsfile' } 
like image 68
krynio Avatar answered Oct 05 '22 23:10

krynio


Another elegant solution for handling multiple Git repositories within single pipeline can be found at this thread.

node {     dir('RepoOne') {         git url: 'https://github.com/somewhere/RepoOne.git'     }     dir('RepoTwo') {         git url: 'https://github.com/somewhere/RepoTwo.git'     }      sh('. RepoOne/build.sh')     sh('. RepoTwo/build.sh') } 
like image 41
czerwin Avatar answered Oct 06 '22 00:10

czerwin