How to build jenkinsfile from a repo which triggered jenkins job when using multiple repositories in SCM in one jenkins pipeline? - jenkins

I have multiple repositories which require a cd pipeline and all the repositories have their own jenkinsfile with same name. I have configured one jenkins pipeline for all the repositories by adding multiple repositories in "pipeline script from SCM" section.
When any of the repository is updated the pipeline triggers but it uses jenkinsfile of the first repository mentioned in the list of repos.
Is their anyway we can change this behaviour and make the Jenkins job run Jenkinsfile from a repository which triggered the build.

This can be done by shared library https://www.jenkins.io/doc/book/pipeline/shared-libraries/
https://www.tutorialworks.com/jenkins-shared-library/
You can define various logic and functions to manage multiple repositories and their variables.
Also, try to make generic script which can behave based on the supplied parameters.

Related

Jenkins / Poll SCM : How to retrieve which SVN repository change triggered build job in declarative pipeline

I'm currently trying to set up a build job connected to multiple (3) svn repositories with single pipeline, with Poll SCM scheme. All the contents from the repositories build into a single artifact. Repositories are, e.g., say:
https://myrepo.com/mainSrc/main
https://myrepo.com/libs/library1
https://myrepo.com/libs/library2
When I set all this repos inside Pipeline configuration, the build job successfully checkouts all the paths to the workspace.
However, the point is, I need to track which repository started the build job, to write some conditional steps. Is there a solution for this? I googled for it, there are some ways to obtain svn revision for a path, however it's not what I'm looking for.

Jenkins pipeline for multiple repo

I am working on micro-service base Jenkins pipeline. There is similar code pattern / structure in Bitbucket repository for Java and angular based code. Can I manage multiple repositories with single pipeline with dynamic approach which can provide filter option (like AJAX in java) in drop down like I can get more than one repositories in drop down option and I can select any repository, based on this repository, I can get branches from this repository in next drop down and I can execute Jenkins pipeline.
Do we have any Jenkins plugin which can provides filter option for Bitbucket repositories in Jenkins.
Presently I am using Git parameter option in Jenkins pipeline (Build with parameter) and then created several pipelines.
Are you saying that you want a job in which you can select a repository and then select a branch from that repository and hit GO and build that branch of that repo? If this is the case then the answer is no.
You would have one Job per repository, and if that job is a multibranch pipeline then you would be able to select the branch to build from there.
You can enable all the repos and branches to use the "same" pipeline that you then submit parameters to using a Shared Library

Jenkins Build Multiple projects in same repository

Previously we had a single monolith in GitLab repository and we used to build project in Jenkins using Jenkinsfile.
Now we are migrating it into multiple microservices and all reside in same GitLab repository. Is it possible to create pipelines for this type of setup or do we need to have each microservice in separate repository. If it is possible please point me to appropriate resources.
Each microservice can have its own Jenkinsfile, you have to tell to the Jenkins job the path of the Jenkinsfile if it is not located at the root path.
In your pipeline configuration job choose "Pipeline script from SCM" and set the "Script Path".
To only checkout the microservice you need, you can use in the "SCM" then "Additional Behaviours" the "Check out to a sub-directory" (then if the Jenkinsfile is now at the pseudo root path, you won't need to change the default "Script Path").
Yes it is possible to create pipelines to build/test/deploy from a single repository.
Use Declarative Pipeline in Jenkins.
You can have your microservices separated in different directories in a single repo and can build them all using a single pipeline using the stages & dir() option in Jenkinsfile. We're building close to 15 components from a single pipeline and push it to it's relevant artifactory from the same job. You can build non-dependent microservice components parallely too.
Documentation --> Jenkins Pipeline,Jenkinsfile

Jenkins multibranch with Jenkinsfile in different repo

If use a multibranch pipeline in Jenkins, I want to have a build for every specific branch that is made inside my code repo. This works like a charm. But I don't want to provide a Jenkinsfile inside my code repo. Instead I want to define a different CI repo which is providing my CI pipeline scripts.
The problem is that the usual config is not containing the scm option like in the normal config.
Normal pipeline config:
Multibranch pipeline config:
Can somebody tell me how to separate the Jenkinsfile from my source code using a multibranch pipeline?
Just in case someone stunmles over the same problem,
there is a Jenkins plug-in which brings remote Jenkinsfiles for mutlibranch pipleines: https://plugins.jenkins.io/remote-file/

Create Jenkins Multi-Branch Pipeline Job from a Job DSL Factory

Is it possible to create Multi-Branch Pipeline Job by a Job DSL which defines the Job by "Pipeline Script" instead of Jenkinsfile contained by each Git Repository?
We wanna avoid to generate and maintain the same Jenkinsfile (except some parameters) in each of our 100 Git Repositories.
At the moment we are using Pipeline Jobs with Job DSL seeded by a Factory Job, but we are limited at them moment with Multi-Branch Builds (Feature Branches). So we wanna switch to Multi-Branch Pipeline Jobs, but there we are limited in seeding them.
I know we could use a Jenkinsfile (Git Repo of Project) which includes other common Jenkinsfiles from the Jenkins, but that is just a workaround.
Only pipeline jobs can have the pipeline defined inline. Multi-branch jobs can't and JobDSL can't change anything about that.
The probably better alternative is using a shared library. You can configure Jenkins to automatically load this library so that the particular Jenkinsfiles in all the repos only have to call a function out of that.
You can e.g. have a look at a Jenkinsfile of a Jenkins plugin - it only calls a function from the shared library:
buildPlugin()
In your case (as you wrote about "except some parameters"), this function could have some parameters that could differ by the different jobs. The buildPlugin function is implemented here in https://github.com/jenkins-infra/pipeline-library/blob/master/vars/buildPlugin.groovy.
While this would still require you to update all your repos, it is probably the better starting point to introduce pipelines in your organisation.

Resources