Jenkins Multibranch pipeline fails if branch does not have jenkinsfile - jenkins

I have a legacy build system on which I want to incrementally add multibranch pipeline support to branches so all branches will build and test for Continuous Integration. However ... old branches do not have a jenkinsfile present and a multibrach pipeline build fails with FileNotFoundException.
The pipeline works just fine on my new branches. It finds the jenkinsfile and runs the build fine. Can I have the pipeline filter out or ignore older branches that do not have a jenkinsfile?

Related

Scan Multibranch Pipeline - how to trigger on PR creation from jenkinsfile?

I have a project in Bitbucket with setup web-hook and Jenkins multibranch project.
Web-hooks work okay for the branches, that are already scanned and are presented in the multi-branch project: when the PR is created for any of them, the build is triggered - ok.
But when a PR is created for a new branch (which is not in the list of scanned branches yet), the Scan multibranch pipeline is not triggered and the branch does not appear in the list.
I have the following configuration for the job:
The questions are:
Why that scan procedure is not triggered on a PR for new branch with the options from above?
How can I set this scan trigger strategy directly in the pipeline jenkins file?

Can I specific a single Jenkinsfile for multibranch pipeline in Jenkins?

By default, Jenkins multibranch pipeline discovers branches of my GIT repository. It can execute the related Jenkinsfile for the target branch. However, this design force me to create one version of Jenkinsfile for one branch and this cause Jenkinsfile maintenance overhead.
If I can ensure I will have only 1 version of Jenkinsfile, is it possible for multibranch pipeline to point to such Jenkinsfile instead of referencing Jenkinsfile of the target branch?

Where to store Jenkinsfile for downstream pipeline without its own repo

I'm in the process of setting up Jenkins pipeline jobs for our build. We previously used Freestyle jobs to build each of our repos, with various extra manually triggered jobs at the packaging and deployment stages.
We have the following Pipeline setup so far:
repo A/Jenkinsfile - Pipeline A - build, runs unit tests and produces
artifact
repo B/Jenkinsfile - Pipeline B - build, runs unit tests and
produces artifact
Whenever Pipeline A or B completes I want to run Pipeline C.
Pipeline C should take the artifacts of the latest A and B pipelines, package them together as a docker image, run integration tests, deploy to staging environment.
Where should I store the Jenkinsfile for Pipeline C since it does not have it's own source repo?
To me this logically sits as a separate pipeline that is downstream from A and B so assume this belongs in a separate Jenkinsfile but then where would that go in source control. To clarify I would like to have all my pipeline files under source control.
Alternatively should I try to define the whole pipeline (including integration, etc.) in just one of the parent Pipelines, skipping the build part when it is triggered from the other pipeline? Leaving the other as a simple build. For example
Pipeline A (full pipeline)
build
unit tests
artifact
(get latest B artifact)
integration test
deploy to staging
Pipeline B (simple build that then uses full pipeline)
build
unit tests
artifact
trigger Pipeline A, skipping everything up to integration test step (potentially using when blocks on those stages that look at the build trigger?)
I can find resources on either building or triggering one pipeline from another like this: https://metamorphant.de/blog/posts/2019-03-11-jenkins-101-downstream-projects/
but no information on structuring the above kind of dependencies and pipelines in Jenkins Pipeline.
When you define a 'new item' in Jenkins and select the 'Pipeline' project type, you get a form when the Pipeline is defaulted to 'Pipeline script'.
If you have a Jenkinsfile, then you switch to use 'Pipeline script from SCM' and sets it to point to the GIT location of the Jenkinsfile, but if you leave it to be 'Pipeline script' as defaulted, you can put the content of the Pipeline inside the form, and it will be saved inside the job's config.xml file, without the need to store it in source control.

Jenkins BlueOcean only to build using a single branch for build?

When I build a multibranch pipeline job using BlueOcean it looks into my repository and reads all of my Jenkinsfile for each branch. to initiates multiple build, but I only want to be able to build a job based on a certain branch. Is there a way to only build blueocean?
Thanks
I sounds like your job is a Multibranch Pipeline.
If you create a job that is just a Pipeline (rather than Multibranch Pipeline), you can specify a single specific branch to build.

How to fool Jenkins into thinking all branches already ran in multibranch pipeline?

I'm using multibranch pipelines to build a project in a git repo with many (mostly dormant) branches. The various Jenkinsfiles are all stored in the git repo.
The multibranch pipeline jobs are all defined using the job-dsl plugin. I have a problem when I run the job-dsl scripts; even when a job-dsl job definition wasn't updated (and the multibranch pipeline job wasn't updated), Jenkins queues all branches for build. Due to the large number of branches (and possibly some jobs/branches that run on the master itself), this eventually causes the master to crash.
How can I mark all branches as "already built" and avoid this massive redundant build event when I run my job-dsl generation job?
Jenkins 2.89.4
job-dsl 1.68
Specifying an id for the SCM source solved that problem a while ago for me:
multibranchPipelineJob {
branchSources {
github {
id('some-id')
// ..
}
}
}

Resources