Autoincrementer - increment only after code change

Hi

I have tried the autoincrementer plugin to share build number (4th number in the version string) across branches of the same project.

However, it has the drawback to increment the build number even when no change has been made in code. I would like the build number to increment only after a commit has been made. Then multiple builds with name build number will refer to the same instant of code development.

Is there an easy way to do this, or would this be possible to do in a new custom plugin?

Best regards,

Arni

4 comments
Comment actions Permalink

I have changed the autoincrementer plugin so it increments only if there are changes, and applies the incremented build number to the current build.

However, if there are no changes I would like to apply the previous build number of the same branch. How do I retrieve that number, or the context of the previous build?

0
Comment actions Permalink

I tried the following code but it does not seem to work?

 

SFinishedBuild previousBuild = build.getPreviousFinished();
Map<String, String> previousParams = previousBuild != null
? previousBuild.getBuildOwnParameters()
: new HashMap<String,String>();
0
Comment actions Permalink

I found out that I can access the parameters of previous build with

previousBuild.getParametersProvider()

so I have successfully changed the autoincrementer plugin to fulfil my wishes

0
Comment actions Permalink

Just to answer my own question I will show the changed plugin code here

 

SFinishedBuild previousBuild = build.getPreviousFinished();
Map<String, String> previousParams = previousBuild != null
? previousBuild.getParametersProvider().getAll()
: new HashMap<String,String>();

boolean containsChanges = build.getContainingChanges().size() > 0;

for (String param : autoIncParams)
{
if(!containsChanges && previousParams.containsKey(param))
{
context.addSharedParameter(param, previousParams.get(param));
}
else
{
int value = containsChanges
? myValues.getAndIncrement(param)
: (int) myValues.get(param);

value = (int) myValues.get(param);

context.addSharedParameter(param, String.valueOf(value));
LOG.debug("Processed parameter '" + param + "', set value '" + value + "'");
}
}
1

Please sign in to leave a comment.