Jenkins pipeline to build multiple projects in sequence? - jenkins

I am a novice when it comes to Jenkins. I would like to be able to take different projects and build them in sequence with each other with a Pipeline. I have noticed that Jenkins can run various stages in sequence or in parallel via a Pipeline. Is there a way for this to happen with running projects in sequence or in parallel that exists on the same node?

I know this question is a bit old. But in case there's someone out there that might be searching for an answer, follow these steps (Remember the names of the Jobs as you need to type them)
Create a "Multijob Project"
In the "Build" section, type the Name of your first Job.
Again In the "Build" section, "Add build step" type the next Job name.
In both builds, "Block until the triggered projects finish" should be ticked.
Save & Build your Multijob Project.

Yes, you can use build step to trigger any project (job) you want in Jenkins. See https://jenkins.io/doc/pipeline/steps/pipeline-build-step/

Related

Jenkins - Job A sets the build number for Job B without reloading project configuration from disk

I want to have one Jenkins job control the build number of another job but without the inconvenience of reloading the entire project configuration from disk. I have seen that it's easily possible to directly update the nextBuildNumber file of the target job (I can do this as a build step of Job A) but this does not take effect immediately. Restarting Jenkins or even reloading the Jenkins configs from disk takes way too long and can only be done when there are no builds in progress.
I have tried the groovy script mentioned in the below post by running it from the Manage Jenkins > Script Console. The same post also suggests the script can be saved as a file and run from the CLI. Can it be run from a build step?
I want Job A to determine Job B's next build number and set it so that Job B can run (later in the same day) with the desired build number.
https://stackoverflow.com/a/20077362/4306857
Perhaps I should clarify. I'm not familiar with Groovy so I'm looking at the various build step options like "Execute Windows batch command" which I have a lot of experience with. I can see an "Invoke Gradle script" option so I was wondering if there may be a plugin that can run Groovy scripts perhaps?
The reason this requirement has arisen is that we are compiling a product for two different platforms. We want to compile the codebase almost simultaneously for both platforms with two jobs (A & B) which will both update the JIRA cases included in the builds. We feel it will be less confusing to have both these jobs running with the same build number so that when we talk about a particular issue being addressed in build #75, say, we won't have to qualify that by stating the full job name. If JOB-A build #75 and JOB-B build #75 are both compiled on the same day from the same codebase we can test and compare the results of both builds with far less confusion than if the build numbers go out of sync.
Obviously, in the short term we will use the Set Next Build Number plugin to manually keep the build numbers in step but we want to automate this if possible.
Depends on whether or not you are using Version Number plugin:
[ X ] Create a formatted version number
Build Display Name [ X ] Use the formatted version number for build display name.
Assuming you are NOT, this groovy script will do:
def NextNumber=42
job=Jenkins.instance.getItemByFullName('path/to/jobName')
job.nextBuildNumber = NextNumber
job.save();
You will need groovy plugin for that. Place that in an "Execute system Groovy script" step. Make sure to choose system groovy. That will execute on the master, where the job config and metadata is stored so you have access to the Jenkins internals and data.
I'd suggest you should really be using the above options rather than relying on "keeping both jobs in sync" via a script or manually. You can then pass the label to be used from the first job as a parameter to the second job. That would also require Parameterized Trigger as well as Version Number plugins.
You can even use ${BUILD_DATE_FORMATTED} or ${BUILD_TIMESTAMP}, etc.
Postdate: thinking about the problemspace from a different perspective, that of running 2+ builds on different platforms (simultaneously), there's a plugin for that: Matrix project. You can run it as a freeatyle job on multiple nodes or is excellently described as Matrix building in scripted pipeline. Not sure how that would tie in to JIRA.

Is it possible to trigger Multibranch pipeline job from a regular Jenkins job under "Build other Projects"?

I have 2 jobs. One is a regular freestyle job on Jenkins that is supposed to trigger another job which is a Multibranch pipeline job.
The issue is whenever I enter the name of the Multibranch job in the "Projects to Build column", I get an error - "x is not buildable". But the Multibranch job works perfectly well on its own and there are no problems with it.
Is "Build other projects" post build action (downstream project) not compatible with Jenkins pipelines? What am I missing here?
I have found a temporary solution, where it is required that I need to mention 'Multibranch_Pipeline_Job/Sub_Job_Name', where Sub_Job_Name is the name of one of the many jobs in the Multibranch job.
Although this works out well, it is not a very feasible solution. If a Multibranch job has many branches containing Jenkinsfiles, it is required that all these sub-jobs be mentioned separately in "Projects to Build" column.
If there is more viable solution where it is possible to execute the Multibranch pipeline job entirely (all the sub-jobs), please answer here. It would be much appreciated.

Pipeline to use artifacts from 2 projects associated by the same git branch name

the company where I work for is evaluating jenkins 2.71, in particular the pipeline and blue ocean plugins. We already tested also GoCD and we need, as in GoCD, a way for a pipeline to automatically fetch the artifacts from 2 other pipelines (taking the last successful result of each one of them), here our case.
We have these initial pipelines (build & run tests), which reflect 2 projects:
frontend, ~ 15 minutes
backend, ~10 minutes
I created a pipeline called configure (~1 minute), with e.g. a parameter called customer-name, which takes backend and frontend files and puts them together, then applies specific customer specific configurations and customizations and produces deployable artifacts. Instead of "customer-name" I could also parallelize this job to create all the artifacts for each customer at once, separated in different directories.
The next pipeline would be to deploy them on different test servers separated for each customer. This could be also part of the same configure pipeline, we still have to see how to put things together in jenkins...
Ideally, I need configure pipeline to be triggered automatically (or also on demand) after each frontend or backend success and take as input the last successful artifacts from these 2 pipelines, but not just having the last successful build, we need as dependency the git branch name.
E.g. we have:
backend branches:
master
release/2017.2
frontend braches:
master
release/2017.2
In the pipeline editor, I found a Build Triggers option and set it as follows: Build after other projects are built > Projects to watch: frontend, backend > Check Trigger only if build is stable or better in my test environment full of failures Trigger even if the build is unstable.
Searching further, I found Copy Artifact Plugin
But now the big question, how to fetch the last successful artifacts from these pipelines with the same git branch name?
Because we don't want to mix e.g. a backend build of "release/2017.2" with frontend "master", it has to find as the last successful build having the same relationship or parameter or whatever you wanna call it, in our case the association is the git branch name.
Is it possible to achieve this? If yes, how?
The copy artifact plugin seems to work in a freestyle project. Would it work in a pipeline? That's also a concern...
Thanks
Yes, the Copy Artifact plugin does work in both freestyle and pipeline projects; pipeline uses the copyArtifact function that I referenced in my comment. Note that if you go to the Pipeline Syntax link, it's kind of hidden: you have to first select "step: General Build Step" from the drop-down, then it will give you the Copy Artifact pipeline command builder.
I'm going to assume that your frontend and backend projects are built as multi-branch pipelines, as that would probably be easiest to maintain so that you don't have to keep creating new projects for every release. You can reference these projects from other projects by referencing <project name>/<branch name> (sometimes I've had to replace the / with %2f instead, I think mostly on freestyle projects). You could then set up your configure project as a parameterized build (either pipeline or freestyle), say with a string parameter of PROJECT_BRANCH_NAME. Then put in the following in your frontend/backend project pipeline scripts to trigger a build of your configure project
build job: 'configure', parameters: [[$class: 'StringParameterValue', name: 'PROJECT_BRANCH_NAME', value: ${env.BRANCH_NAME}]]
Then you should just be able to make your configure project reference the frontend/%PROJECT_BRANCH_NAME% and backend/%PROJECT_BRANCH_NAME% (or ${env.PROJECT_BRANCH_NAME} in a pipeline script) when copying the artifacts.
Also, is there a particular reason why you're evaluating specifically Jenkins 2.7? 2.7 is a year old now, and there have been a few new LTS releases since then. I'd recommend staying reasonably up-to-date unless you know there's a specific reason you want 2.7.

In Jenkins, how to specify build phase nodes but restrict where overall build job runs?

I currently have a MultiJob (https://wiki.jenkins-ci.org/display/JENKINS/Multijob+Plugin) which kicks off a series of build phases. What I want to do is start the MultiJob on one node but then run the next two build phases on a selected amount of nodes.
Using the NodeLabel plugin (https://wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin) lets me specify the desired nodes and pass them to my build steps but selecting multiple nodes also causes the MultiJob to run multiple times. Is there an easy way to pass parameters but restrict where the overall MultiJob runs? Using a NodeLabel ignores the "Restrict where this project can be run" flag.
If there isn't a way to do this on the MultiJob itself my second plan is to have a job where you select nodes, populate a settings file, trigger the MultiJob on one node, and then have all build phases read the settings file from a known location to run their nodes.
Use groovy plugin and scripts to select Multijob phase.
For doing this you should also need to install Multijob plugin jenkins.
It provide many options to pass your node or any parameters.
you can restrict or allow specific Multijob phase by using these plugins.

Jenkins Build Distribution among Slaves

I need to get some advice about the way to control the way Jenkins Slaves are used / Jobs are being triggered.
Background / Constraints:
I have a sequence of 10 jobs that run one after each other using the "Trigger parameterized build on other projects" option Parameterized Trigger Plugin.
Each Build of these jobs must run on the same node (I am doing it by using "Build on the same node" which is also configured in the parameterized build plugin and comes from the NodeLabel Plugin).
I have 5 Slaves (current number of executors per slave is 1 but i am open for suggestions here...)
Once Slave is occupied by a Build Sequence, no other job can run on it. When I had only 1 slave, the way I enforced it was using the "Block build when downstream project is building"
The way I configured the slave to be chosen when the first Job is triggered is one of the following: (None of them solved my problem)
a. Using the "Restrict where this project can be run" and put there a label that all relevant slaves will point to.
b. Using the option of "This build is parameterized" (Parameterized Trigger Plugin) and then add a "Node" parameter with the list of Slaves that the User can choose from.
What I want to achieve?
When a User triger the build of the First Job in the Build Sequence, this Build will be done on once of the idle Slaves. (I mean a slave that is doing nothing at the moment)
If there are no idle slaves, then it will join a queue of one of them (doesn't matter which)
Any suggestions how to solve it?
Thanks!
Try passing ${NODE_NAME} as a NodeLabel as a post-build trigger to the downstream jobs. If that works, you may need to pass it every job.
Try node-label parameter plugin. you can make Jobs to run slave node which is free at that movement

Resources