How to get only the Full name of previous Jenkins build - jenkins

I would like to send the full name of the previous build that was received by using the following:
Run previousBuild = steps.currentBuild.rawBuild.getPreviousBuildInProgress()
in order to send to getItemByFullName as you can see below,
Jenkins.instance.getItemByFullName(previousBuildPath)
I tried to find it in the Run class hudson Documentation with no success.
while printing previousBuild I got the name with the build number like:
previousBuild- > project_neme/build_name #100
But I want to get only the name with no String substring cutting.

You are looking for display name property. Display name is the name given to each build (if you update it during execution) followed by a build number. The display name will only return the build name.
Jenkins.instance.getItemByFullName('JobName').getLastBuild().displayName
Read here
https://javadoc.jenkins.io/hudson/model/Job.html

Related

Python string as build parameter to Jenkins Trigger mail doesn't read the entire string. Instead cuts half of the string while reading

This is inside my trigger email
SQLScripts=["Query1","Query2","Query3","Query4","Query5","Query6"]
What gets read in string parameter for Jenkins build is following
This line of code executes
echo %SQLScripts%
Prints
echo ["Query1","Query2","Query3",
Initially I thought this could be some problem with how I wrote variable name, I tried $SQLScripts and "$SQLScripts". But problem is with reading the variable from email.
As I have manually added value inside jenkins build configuration and echo printed the entire value.
Please any help is appreciated.

Display the username who ran the build in Build History

How can I display the username who executed the build next to the build number? like in the image below
Try entering Started by ([\S]+) in the section "Post-build Actions ->
Set build description -> Regular expression" of your job config.
You can add user name to build description.
currentBuild.description = currentBuild.getBuildCauses().shortDescription[0]
Then you will get something like bellow
* #1 Feb 24, 2020 10:00 AM
| Started by user max
If you want use user only then
currentBuild.description = currentBuild.getBuildCauses().userId[0]
Data structure is :
[{"_class":"hudson.model.Cause$UserIdCause","shortDescription":"Started by user max","userId":"max","userName":"max"}]
See, install and enable for the job
https://plugins.jenkins.io/build-user-vars-plugin/
Then change name of build using BUILD_USER_ID variable, like
#${BUILD_NUMBER}: ${GIT_REVISION,length=8} (${GIT_BRANCH}) by ${BUILD_USER_ID}
Add Groovy Postbuild and user build vars plugin
edit job configure,check 【Set jenkins user build variables】,then you can get env variables 'BUILD_USER'
Post-build Actions, add 【Groovy Postbuild】, Groovy Script change to
manager.addShortText(manager.envVars['BUILD_USER'])

Get all logs of the current Jenkins build with currentBuild.rawBuild.getLog

I want all my jenkins logs of my current build in groovy
env.logs = currentBuild.rawBuild.getLog(1000).join('\n')
This works, but the problem here is I have to specify the amount of lines.
When I use:
env.logs = currentBuild.rawBuild.getLog().join('\n')
env.logs is empty. What is the right command to get all the logs without specifying the amount of lines. Is this possible?
currentBuild.rawBuild.log seems to work but is deprecated?
You can use the getLog method with a max value getLog(Integer.MAX_VALUE)
This is working for me - it reads the actual log file instead of using the API:
logFileContent = new File("${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_NUMBER}/log").collect {it}

Why doesn't the default attribute for number fields work for Jenkins jelly configurations?

I'm working on a Jenkins plugin where we make a call out to a remote service using Spring's RestTemplate. To configure the timeout values, I'm setting up some fields in the global configuration using the global.jelly file for Jenkins plugins using a number field as shown here:
<f:entry title="Read Timeout" field="readTimeout" description="Read timeout in ms.">
<f:number default="3000"/>
</f:entry>
Now, this works to save the values and retrieve the values no problem, so it looks like everything is setup correctly for my BuildStepDescriptor. However, when I first install the update to a Jenkins instance, instead of getting 3000 in the field by default as I would expect, instead I am getting 0. This is the same for all the fields that I'm using.
Given that the Jelly tag reference library says this attribute should be the default value, why do I keep seeing 0 when I first install the plugin?
Is there some more Java code that needs to be added to my plugin to tie the default in Jelly back to the global configuration?
I would think that when Jenkins starts, it goes to get the plugin configuration XML and fails to find a value and sets it to a default of 0.
I have got round this in the past by setting a default in the descriptor (in groovy) then this value will be saved into the global config the first time in and also be available if the user never visits the config page.
#Extension
static class DescriptorImpl extends AxisDescriptor {
final String displayName = 'Selenium Capability Axis'
String server = 'http://localhost:4444'
Boolean sauceLabs = false
String sauceLabsName
Secret sauceLabsPwd
String sauceLabsAPIURL =
'http://saucelabs.com/rest/v1/info/platforms/webdriver'
String sauceLabsURL = 'http://ondemand.saucelabs.com:80'
from here

Add BuildNumber in GitTemplate.12.xaml

In the related TfvcTemplate.12.xaml the solution is to add the build number like so:
<mtbwa:MSBuild CommandLineArguments="[String.Format("/p:SkipInvalidConfigurations=true
/p:BuildNumber={1} {0}", MSBuildArguments, BuildDetail.BuildNumber)]"
In the Git template the arguments have slightly changed, but doing the same results in the following error
Compiler error(s) encountered processing expression
"String.Format("/p:SkipInvalidConfigurations=true /p:BuildNumber={1} {0}",
AdvancedBuildSettings.GetValue(Of String)("MSBuildArguments", String.Empty),
BuildDetail.BuildNumber)".
'Microsoft.TeamFoundation.Build.Client.BuildDetail' is not accessible in
this context because it is 'Friend'.
What is the correct way to expose the BuildNumber in this template?
There's two steps I had to go through to make this work.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/49f11ed9-9fa8-4c20-952a-d39ee7e71051/can-no-longer-user-builddetaildroplocation-for-copydirectory-with-tfs-2013-using-build-process?forum=tfsbuild
Within the same template you modified in step 1 click on the "Run MSBuild" activity, view properties and open "CommandLineArguments". I'm using OctoPack for Octopus Deploy so here's what my arguments look like:
String.Format("/p:SkipInvalidConfigurations=true /p:BuildNumber={1} {0}
/p:OctoPackPackageVersion={1}", AdvancedBuildSettings.GetValue(Of
String)("MSBuildArguments", String.Empty), BuildDetail.BuildNumber)
As you can see, BuildNumber is specified there so you can just remove the Octopus property I added. Finally within your msbuild file (.csproj for example) you'd use build number like so $(BuildNumber)

Resources