Jenkins - Updating build changelog during build step - jenkins

I noticed that if you use Jenkins with the SVN or CVS option, a changelog.xml is created for each build that contains the author and the commit message for that build.
Unfortunately, in my setup, I am not using SVN or CVS, so I am unable to take advantage of the changelog parser. I was wondering if it was possible to create your own changelog with the same format (like the SVN XML changelog) and then point Jenkins to it during the build process. This way, when someone clicks on changes for the build, they'll be able to see what changed and who changed it.
I've tried just creating a changelog.xml and then updating build.xml to use the SVN parser, but two issues that I've noticed:
1) You have to reload configuration files to get it to show up
2) Build.xml doesn't appear to be created until the job is complete
There is some information on the changelog parser, but it doesn't seem that you can just access it during a build step: https://wiki.jenkins-ci.org/display/JENKINS/Change+log

Maybe a system groovy scripts would be a good direction (groovy script plugin). Just add a new script as your build step. You can access your AbstractBuild object by running the following code:
import hudson.model.*
import hudson.util.*
import hudson.scm.*
def thr = Thread.currentThread()
def build = thr?.executable
I'm trying to solve a similar problem currently, but my use case change a bit. I try to copy the changes from Upstream project in the similar way that BlameSubversion plugin does. Unfortunately I can't use the mentioned above SCM plugin because it doesn't work with post-commit-hook, so I have to write my own solution.
Take a look at copyChangeLogFromTriggerJob and copyRevisionFromTriggerJob methods to get know how BlameSubversion does that.
I'm able to copy changes and revision but I'm still fighting with ChangeLogParser.
I would be gladfull for any help as well.

Related

Jenkins change Build History timestamps

I have a Multibranch Pipeline in Jenkins.
My goal is to change the Timestamps of the Builds in the History.
I was able to change the DisplayName (#13 to 'test') as shown in the picture below, but not the timestamp. Is this possible, and if so, how?
After googling for some time I found one wonderful plugin.
https://plugins.jenkins.io/groovy-postbuild/
Groovy Post Build.
This plugin can help you to add custom text, plugin-predefined Icons In Build History side pane after build Timestamp. Like
And this plugin has some other icons also and we can give on hover to those icons.
It's really fun to implement cool stuff there. This plugin has very good documentation and well-defined Examples.
Please have a look, This will resolve your problem.
PS: you need to write some groovy script 😋
[update]
Install plugin:
Jenkins > configure > manage plugin > Avaialble > search for Groovy PostBuild > Install and restart Jenkins
How to use:
Start Configure your project. If the project is FreeStyle then go to post build tab in the configuration page. Like this:
A textArea will open now add your Script as described in Examples.
Now Apply and save. Run the build and tadaaaa.. your message is in build history pane.
I think this could help you to setup and run script

How to use a GitLab link for applying jenkins.yml file for the concept of Jenkins Configuration as Code

I have a local instance of Jenkins. I have previously tried storing the jenkins.yml in my system and giving its path on http://localhost:8080/configuration-as-code. This worked but I want to use a Gitlab repository to store the jenkins.yml file.
I have already tried giving the gitlab link of my jenkins.yml in the path or URL textbox. Some weird things happened, like
1. jenkins broke or huge error console
2. It reapplies the previous configuration(from system path)
jenkins:
systemMessage: "Hello, world"
Your problem as described: you want the job configuration to be saved in GIT and, when a build is triggered, the job should get the current stand of its configuration from there and then, run the build.
Maybe there is a kind of plug-in that does it for you, but I am not aware of any. Maybe anyone?
My suggestion is to define a pipeline job and use a declarative pipeline. It is a file, normally named Jenkinsfile that can be stored in GIT. In the Job, you define the GIT address and when you trigger a build, the file is got from GIT and executed.
There are several flaws in this: pipelines learning curve is not small, you are confronted with groovy (not XML!) and your current XML file is barelly useful.
Maybe someone shows up and tells us about new (for me) plugin that solves your problem using the configuration XML file. In the other hand, pipelines are such a beautyful feature that I encourage you to give it a try

How to delete a build from Jenkins job workspace

I wonder if it is possible to remove only one build (including artifacts) from job workspace.
I tried to "Delete Build" in Build History but all it does is remove build reference from Build History table. I know I can ssh to a server and delete files from the command line but I am looking for a way to do it from Jenkins web interface.
After installing Workspace Cleanup Plugin I am able to wipe out current workspace but I want to keep my other builds in the workspace.
In your Jenkins instance, to be able to have folder/per build - set flag "Use custom workspace" in your job's settings. Here is a brief help info from the setting description:
For each job on Jenkins, Jenkins allocates a unique "workspace directory."
This is the directory where the code is checked out and builds happen.
Normally you should let Jenkins allocate and clean up workspace directories,
but in several situations this is problematic, and in such case, this option
lets you specify the workspace location manually.
One such situation is where paths are hard-coded and the code needs to be
built on a specific location. While there's no doubt that such a build is
not ideal, this option allows you to get going in such a situation.
...
And your custom directory path would look like this:
workspace\$JOB_NAME\$BUILD_NUMBER ~> workspace\my-job-name\123
where $JOB_NAME will be "my-job-name" and $BUILD_NUMBER is the build number, eq. "123".
There is one nasty problem with this approach and this is why I wouldn't recommend to use it - Jenkins will not be able to reclaim disk space for outdated builds. You would have to handle cleanup of outdated builds manually and it is a lot of hassle.
Alternative approach, that gives you more control, tools and is able to keep disk space usage under control (without your supervision) is to use default workspace settings and archive your build output (files, original source code, libraries and etc.) as a post-build action. Very-very handy and gives you access to a whole bunch of great tools like, Copy Artifact Plugin or ArtifactDeployer Plugin in other jobs.
Hope that info helps you make a decision that fits your needs best.
I also use "General/Advanced/Use custom workspace" (as in #pabloduo's answer) on a Windows machine with something like:
C:\${JOB_NAME}\${BUILD_NUMBER}
Just wanted to add a solution for getting rid of the build job's workspaces.
I use Groovy Events Listener Plugin for this.
Using the plug-in's standard configuration I just use the following Groovy script:
if (event == Event.JOB_DELETED){
new File(env.WORKSPACE).deleteDir()
}
And now the custom workspace is deleted when the build job is deleted.
Just be aware that this would also delete non-custom workspaces (because the event is triggered for all jobs on your Jenkins server).

How to collect report from copy artifact plugin

I use Jenkins to build complicated project from different sources. Last build copy artifact from previous builds and create product. I want to get report what exactly were copied(build name, build number, changelog of source build, etc)
Final build have no upstreams and started manualy or by commit into own git repo.
I think groovy postbuild step can help me. But i do not understand how to get access to CopyArtifactPlugin Property.
I find this workaround by parsing build log.
You can see gist here

Hudson/Jenkins PMD Configuration

I am new to Jenkins and just started configuring it. This is what i have done till now:
Installed and configured Jenkins to display the home page. Added PMD plugin.
Set the HUDSON_HOME to a specific directory > C:\Work\Jenkins
Configured a test build to run a simple do-nothing ant script. It runs successfully
Written an independent pmdbuild.xml to run checks on a set of files in C:\myview (I am using clearcase). This xml also copies the output pmd_results.xml to the workspace directory in $HUDSON_HOME/[job-name]/workspace
Now I added the pmdbuild.xml as a step in my primary build. So my build has 2 steps:
a. Run a simple script, do-nothing.
b. Run pmdbuild.xml which generate pmd_results.xml and place it in $HUDSON_HOME/[job-name]/workspace (HARD-CODED as Jenkins PMD plugin expects the file there)
Jenkins picks up the pmd_results.xml automatically with the plugin and displays warnings and everything.
Now the problem:
If I click on a filename in the PMD results, it gives a filenotfound exception as it is looking for the source file in $HUDSON_HOME/[job-name]/workspace.
My java code files are placed in C:\myview (a clearcase snapshot view)
My question is, do I need all my code files to be present inside $HUDSON_HOME/[job-name]/workspace ?? Meaning can't I tell Jenkins to look for the PMD input files in C:\myview or any other directory instead of $HUDSON_HOME/[job-name]/workspace ??
Sorry for the extremely long description.
Jenkins expects that all the code is in the workspace. Usually Jenkins is used to check out a copy of the code into the workspace, and then runs all build steps on the Sources in the Workspace.
Might seem restraining at first, but it saves you a lot of trouble if you need to move Jenkins to another server, or create a slave instance.
So I would suggest you let Jenkins check out your code (there should be a clearcase plugin) into the workspace, and run the analysis on the checked out code.
If there are compelling reasons why your code has to stay where it is (C:\myview in your case) you can still set the workspace of your build to that directory (find this in the job configuration page, you need to click on the 'extended' button to see the option).

Resources