Jenkins multibranch pipeline with subfolders each with own Jenkinsfile - jenkins-pipeline

I have Jenkins MultiBranch Pipeline created. I have single repository
library-project (root project) has readme file and sub projects; no Jenkinsfile
profit (gradle project) - has own Jenkinsfile i.e profit/Jenkinsfile
cost (gradle project) - has own Jenkinsfile i.e cost/Jenkinsfile
I tried to setup a Jenkins MultiBranch Pipeline. The first issue is Jenkinsfile in sub projects could not be scanned. For Build Configuration I chose Mode by Jenkinsfile and Script Path I put Jenkinsfile as a path. Discover Branches section for a git project I did try to regular expression filter by branch name (i.e profit and cost branch), but it does not find the Jenkinsfile so no build happens.
How can a single multibranch pipeline successfully scan the two subprojects Jenkinsfile? Is there a way to trigger only subproject that is changed?

Related

Configure Maven project as Jenkins Multibranch Pipeline

How can I configure a Jenkins Multibranch Pipeline as Maven project?
For a Maven project in Jenkins I get the option.:
Build Triggers
Build whenever a SNAPSHOT dependency is built
But for a Jenkins Multibranch Pipeline I don't get that option.
How to get that option for a Jenkins Multibranch Pipeline?
dEE.
I think it is not possible. But I can share workaround with you.
Create maven project job, configure webhooks between jenkins and gitlab. In Source Code Management add as much branches as you want.
You can create a multibranch pipeline in Jenkin by choosing "Multibranch pipeline " in the option
There is no poll SCM but you can use Scan Multibranch Pipeline Triggers instead. Also take advantage of WebHook if the SCM you use gives that option
and about Build whenever a SNAPSHOT dependency is built this option - I dont think there is a way to do this with multibranch pipeline because this option comes with maven-plugins and can only available in Maven Project. But with out this you can achive the same scenario in different way by following good practice.
Build the dependent project seperately and keep the generate artifact in Nexus or Artifactory, so that the other job will pull independently.
Always make a good practice to use RELEASE version not SNAPSHOT version

Read pom.xml from Pipeline A to Pipeline B in jenkins

I have two Jenkins Pipeline, say Pipeline A and Pipeline B. Here, Pipeline B is the subset of Pipeline A i.e. first Pipeline A will run then it will invoke Pipeline B.
Pipeline A is building the maven project using pom.xml.
Pipeline B will then get invoked, which will deploy the .war of maven project to artifactory.
I want to read the pom.xml in Pipeline B which will be passed as parameter from Pipeline A.
Can anyone help me with the way how we can read the pom.xml in Pipeline B?
Note: I am using declarative pipeline code.
You need to archive your file pom.xml in the pipeline A (with step archiveArtifacts). And then copy this archived file from pipeline A into your pipeline B (using Copy Artifact Plugin).
Something like this :
Pipeline A :
stage('Archive pom.xml'){
steps {
archiveArtifacts artifacts: 'pom.xml'
}
}
Pipeline B :
stage('Get pom.xml'){
steps {
copyArtifacts projectName: 'pipeline-A', filter: 'pom.xml'
}
}
Correct answer I believe would be to use an artifact repository manager to store the pom from the pipeline A, which you grab from the artifact repository manager during the pipeline B execution.

Jenkins Maven Integration Plugin building all existing branches

We are using Jenkins with multi-branch pipelines and the Maven Integration plugin. I configured the multi-branch pipeline to "Build whenever a SNAPSHOT dependency is built". When our dev branch was built, it seemed to build every branch of all dependent repos. How may we limit it to only build branch X when branch X is built?

How to build dependent projects using Bit Bucket pipeline

I am trying to get my build working with pipeline using maven . I have two bit bucket repositories for two maven projects. repository1 -> project1 repository2 -> project2. project2 has dependency on project1. Now I dont have problem in building project1 as it doesn't has dependency on any projects. But when I try to build project2 using pipeline build is failing because maven is not finding the project1 artifact.
I got to know that every pipeline runs within a docker image. So my guess is that pipelines for project1 and project2 are running in 2 separate docker images. Because of this when I run pipeline for project2 maven is not finding project1 artifact in local repository. One way to fix this is hosting a maven remote repo for my project artifacts and adding the repo in POM of project2. But i don't want to host a maven repo. I want maven to pick the artifact from local repo. How to get this working?
I'm sorry that no one ever answered this question for you. Setting up BitBucket Pipelines to use private maven repositories requires you to create a custom settings.xml file in the pipeline and then invoking maven with that file specified.
You can't just put a settings.xml file in your source code repository as it would put your credentials at risk. Instead, you can create a settings.xml file and set credentials from BitBucket Pipelines Secure environment variables.
It's pretty straightforward once you see it in action. I actually wrote an extensive guide on how to completely setup BitBucket Pipelines with Maven repositories that shows specifically how to do this in a secure manner.

How to run a maven plugin without a POM in Jenkins?

I have a plugin which can run either using a pom.xml or without (depends upon the version of the artifact we're building: new versions go without a pom. Strange, I know).
I want to have that plugin run in Jenkins.
But when creating a maven project, I have to set a pom (or as a default, Jenkins suppose there is one in the base folder given).
Question: Is it possible to configure Jenkins to not use a pom when there is none?
As per my comment, you should use a Jenkins freestyle project build in this case, in order to have more flexibility and avoid the default assumptions of a Jenkins Maven build.
In such a build, you can then configure a build step executing a shell or a Windows command (depending on the Jenkins server OS).
Indeed, in the Jenkins Maven build, a pom file is always required, as mentioned in the help support of the Configuration > Build > Root Pom entry
If your workspace has the top-level pom.xml in somewhere other than the 1st module's root directory, specify the path (relative to the module root) here, such as parent/pom.xml.
If left empty, defaults to pom.xml

Resources