Reputation: 1887
I have a Git repository with code I'd like to build but I'm not "allowed" to add a Jenkinsfile
in its root (it is a Debian package so I can't add files to upstream source). Is there a way to store the Jenkinsfile
in one repository and have it build code from another repository? Since my code repository has several branches to build (one for each Debian release) this should be a multibranch pipeline. Commits in either the code or Jenkinsfile
repositories should trigger a build.
Bonus complexity: I have several code/packaging repositories like this and I'd like to reuse the same Jenkinsfile
for all of them. Thus it should somehow dynamically fetch the right Git URL to use. The branches to build have the same names across all repositories.
Upvotes: 29
Views: 36988
Reputation: 163
The best way I have found is to use the Remote Jenkinsfile Provider plugin. https://plugins.jenkins.io/remote-file/
This will add an option "by Remote Jenkinsfile Provider plugin" under Build Configuration>Mode then you can point to another repo where the Jenkinsfile is. I find this to be a much better solution than the Pipeline Multibranch Defaults Plugin, which makes you store the Jenkins file in Jenkins itself, rather than in source control.
Upvotes: 14
Reputation: 151
U can make use of this plugin https://github.com/jenkinsci/pipeline-multibranch-defaults-plugin/blob/master/README.md Where we need to configure the jenkinsfile on jenkins rather than having it on each branch of your repo
Upvotes: 3
Reputation: 1
In my case, i have an escenario whith a gitlab project based on gradle who has dependencies on another gitlab preject based on gradle too (same dashboard, but differents commits, differents developers).
I have added the following lines into my Jenkinsfile (the one which depends)
stage('Build') {
steps {
git branch: 'dev', credentialsId: 'jenkins-generated-ssh-key', url: '[email protected]:root/coreProject.git'
sh './gradlew clean'
}
}
Note: Be awark on the order on the sentences. If you have doubt on how to create jenkins-generated-ssh-key please ask me
Upvotes: -1
Reputation: 12985
I have version 2.121 and you can do this two ways:
Way 1
In the multibranch pipeline configuration > Build Configuration > Mode > Select "Custom Script" and put in "Marker File" below the name of a file you will use to identify branches that you want to have builds for.
Then, below that in Pipeline > Definition select "Pipeline Script from SCM" and enter the "SCM" information for how to find the "Jenkinsfile" that holds the script you want to run. It can be in the same repo you are finding branches in to create the jobs (if you put in the same GitHub repo's info) but I can't find a way to indicate that you just use the same branch for the file.
Way 2
Same as above, in the multibranch pipeline configuration > Build Configuration > Mode > Select "Custom Script" and put in "Marker File" below the name of a file you will use to identify branches that you want to have builds for.
Then, below that in Pipeline > Definition select "Pipeline Script" and put a bit of Groovy in the text box to load whatever you want or to run some script that already got loaded into the workspace.
Upvotes: 0
Reputation: 7880
Short answer is : you cannot do that with a multibranch pipeline
. Multibranch pipelines are only designed (at least for now) to execute a specific pipeline in Pipeline script from SCM
style, with a fixed Jenkinsfile
at the root of the project.
You can however use the Multi-Branch Project plugin made for multibranch freestyle projects. First, you need to define your multibranch freestyle configuration
just like you would with a multibranch pipeline configuration
.
Select this new item like shown below :
This type of configuration will behave exactly same as the multibranch pipeline
type, i.e. it will create you a folder with the name of your configuration and a sub-project for each branch it automatically detected.
The implementation should then be a piece of cake :
SCM
repository in the multibranch configurationbuild-job
) and give it your repository information, i.e. Git URL and current branch (you can use the pre-defined variables $GIT_URL
and $GIT_BRANCH
for this purpose)build-job
, just define either an inline pipeline or a pipeline script checked out from SCM, and inside this script do a SCM checkout and go on with the steps you need to build. Example of build-job
pipeline content :.
node() {
stage 'Checkout'
checkout scm: [$class: 'GitSCM', branches: [[name: '*/${GIT_BRANCH}']], userRemoteConfigs: [[url: '${GIT_URL}']]]
stage 'Build'
// Build steps...
}
Of course if your different multibranches projects need to be treated a bit differently, you could also use intermediate projects (let's say build-project-A
, build-project-B
, ...) that would in turn call the generic build-job
pipeline)
The one, major drawback of this solution is that you will only have one job responsible for all of your builds, making it harder to debug. You would still have your multibranch projects going blue/red in case of success/error but you will have to go back to called build-job
to find the real problem of your build.
Upvotes: 15