Is there a way to set/change the changeSet (changelog) content from pipeline script? Needed for preflight type of job - jenkins

I have a preflight job using perforce in which I retrieve a branch, unshelve (apply) a given changelist on it and then build to validate that the change in question has not broken the build. Very similar to what you would do for a GitHub Pull Request type of CI.
I use the official checkout() pipeline call to get the branch as it simplify dealing with the perforce creds, and that causes the jenkins build to include the changelog of that branch in the build. Yet, those are of no interest to me, as my interest is on the changelist I am unshelving on top of that branch.
Can I, from the pipeline script clear and fill the currentBuild.changeSet? If so, would someone have an example and which fields I can set under currentBuild.changeSet.items?
Or doing is only possible by going through the plugin road in the same way the p4/git plugins are doing this?

My advice, don't play with the currentBuild.changeSet. It also contains the changeset of the shared libraries you are using. I personally don't rely on that anymore.
However, here is an article on how to update the changeSet
https://support.cloudbees.com/hc/en-us/articles/217630098-How-to-access-Changelogs-in-a-Pipeline-Job-
Here is an exemple on how to implement that in a pipeline
https://issues.jenkins-ci.org/browse/JENKINS-58441
Finally, in an ideal world, don't share your jenkins with management or non developers/testers, share only a dashboard that is connected to a database that you filled with the relevant information you need. I use influxdb+grafana to do that with the influxdb plugin

Related

Best route to take for a Jenkins job with hundreds of sub jobs

Currently, at my organization we have a few repositories which contain ~500+ projects that need to be built to satisfy unit testing (really integration testing), and I am trying to think of a new way of approaching the situation.
Currently, the pipeline for building the projects is templatized and is stored on our Bitbucket server. All the projects get built in parallel, so once the jobs are queued, they all go to the master node to do a SCM check of the pipeline.
This creates stress on the master node, and for some reason it is not able to utilize every available node and executor on that node to it's fullest potential. Contrary, if the pipeline is not stored on SCM, it does the complete opposite to where it DOES use every possible node with any available executor on that node.
Is there something I am missing about the SCM checkout version that makes it different than storing the pipeline locally on Jenkins? I understand that you need to do an SCM poll, and I am assuming only the master can do the SCM poll for the original Jenkinsfile.
I've tried:
Looking to see if I am potentially throttling the build, but I do not see anything
Disable concurrent builds is not enabled within the pipeline
Lightweight checkout seems to work when I do it with Git plugin, but not the Bitbucket Server Integration plugin; however, Atlassian mentioned this will never be a feature, so this doesn't really matter.
I am trying to see if there is a possible way to change the infrastructure since I don't have much of a choice in how certain programs are setup since they are very tightly coupled.
I could in theory just have the pipeline locally on Jenkins and use that as a template rather than checking it into SCM; however, making changes locally to the template does not change the sub-jobs that uses it (I could implement this feature, but SCM already does it). Plus, having the template pipeline checked into Bitbucket allows a better control, so I am trying to avoid that option.

Jenkins. How to configure tasks for specific git branch?

I have multi-configuration project in Jenkins. My git repository have different branches. For example:
dev
stage
bug/code1
feature/code2
etc...
I want to create different Post build tasks, Publish HTML reports etc for each branch.
What is the problem? I changed configuration for stage branch. All works fine only before Branch Indexing. After this process, custom configuration for each branch replaced by multi-configuration project. It means if I added specific task only for stage branch, after Branch Indexing task will be removed.
Multi-Branch Project Plugin says this:
Sub-projects appear to be configurable, but they will be overwritten
by branch indexing if you manually modify them. There is no clear way
to remove or hide the configuration option on sub-projects (except
maybe with project-based matrix authorization??), though version 0.1.x
of this plugin accomplished that via some trickery that is not
possible in newer versions.
So my question is: How I can create custom configuration for each branch? Or what is the best solution for this? Maybe I should create different projects for stage, dev branches?
Thank you.
We have the similar situation like you, more than 10 branches need to be maintained. Instead of using multi branch plugin, we use job dsl to create the jobs for each branch.
For example, hello_branch1, hello_branch2....
Inside our job dsl project, we save the different json config for the different branches. In your case, you can think we save the post build script, or report to be published....
This will make sure the generated jobs are standalone and will not affect each other.
Br,
Tim

Multibranch Pipeline - configure branch projects

When I create a plain pipeline project I have the option to periodically poll the scm and if changes are detected, the build is run. that worked well for me.
Now I created a multibranch pipeline and added 2 branches. However, in the configuration I can not set the same as in the normal pipeline project, because it tells me I can only view the configurations of the sub-branch-projects.
Maybe I'm also doing it wrong, so I try to tell you what I actually want to achieve.
I have a PHP project inside of a Git repository. There are two branches that I want to be built on new commits (when pushed to the main repository)
The main repository resides on a self hosted version of Bitbucket Server. If possible, I want to avoid hooks and let Jenkins poll for changes on the bitbucket server. So how can I achieve that?
You just need to check the option Periodically if not otherwise run trigger at the multibranch level. This replaces per-job polling, because it also detects new branches and the like.
If I got you right, all you want to do is to build ONLY these 2 branches?
If so, under "Branch Sources" just click the "Advanced" and fill in the textfield "Include branches" your branches e.g. test test2 (note the space between the branch names)
Actually you'll have configure SCM pollig (the way you expect it) in the Jenkinsfile itself. It's the properties DSL elelement that handles the configuration:
properties([
pipelineTriggers([pollSCM('H 20 * * 1-5')])
])
Anyways I highly suggest to have a closer look at the Pipeline Snippet Generator:
<your-jenkins-url>/pipeline-syntax/
it s easy to miss but extremely helpful and it s populated based on your currently installed plugins. There you will also find a comprehensible set of options available for properties

Is there a way to automate Jenkins for Hg Flow/Git Flow

We just switched to using Hg Flow, but one of things that we still haven't quite yet figured out is how to best use Jenkins with it. Ideally we would have a job which builds and tests develop, one that builds and tests default and other jobs which are automatically created when a feature or release branch is created, and ideally also delete the job when the feature or release is finished.
Does anyone know if this can be done, and if so, how exactly to do it?
It would be extra nice if Jenkins was able to read from Hg who it was the created the branch and mark them as the contact person if/when the job builds, but this is really just a nice to have.
Thanks in advance
There is a Job Generator Plugin that could be used here. You'd create a job that watches the entire Hg repo for changes, and when it sees one, you run a task that parses the commit, and creates a new job with the new branch in place of templated fields.
If you can see a history of branch creation, you could use that information only in your job-creating task to fill out contact details too.
I haven't tried it myself, but I found this plugin (for GitFlow):
Jenkins Build Per Git Flow Branch
This script will allow you to keep your Jenkins jobs in sync with your
Git repository (following Git Flow branching model).
Also see the author's blog post on this plugin.

checking diffs in jenkins

This may be unrelated to programming but does helps in it.
We use Jenkins with perforce plugin. Jenkins build is triggered by change. We can also see which files got affect in the specific changelist.
But I was curious if there is any option by which we can also see the diff between the files.
Say a changelist says : hello.cpp 2#2
So is there any way I can see the diff between 1st and 2nd version of hello.cpp file. As if there is such feature it will allow us to review the code and increase the usability.
Set up an scm browser server such as fisheye or p4web, and set up the perforce plugin to talk to it. That will provide links to the diffs for each file in the changelog.

Resources