1
votes

I've got a parameterized freestyle Jenkins job, and I'd like to include part of one of the parameters in the build name. The parameter that I'm interested in is called FILE_PATH. Due to the file directory structure of the project used by this job, the file path always starts with the same string, which is 9 characters long. So if FILE_PATH="somepath/what/I/want", I would like the name of my build to be what/I/want.

Some things I've tried putting in the "Set Build Name" field provided by the Build Name Setter plugin:

${FILE_PATH:9}

This gave me the following error:

Failed to evaluate name macro:org.jenkinsci.plugins.tokenmacro.MacroEvaluationException: Unrecognized macro 'FILE_PATH' in '${FILE_PATH:9}'

${"FILE_PATH":9}

FILE_PATH was just treated as a string:

New run name is '${"FILE_PATH":9}'

${$FILE_PATH:9}

This just led to the raw expression being included with FILE_PATH being expanded:

New run name is '${somepath/what/I/want:9}'

${ENV,var="FILE_PATH":9}

This didn't get processed at all:

New run name is '${ENV,var="FILE_PATH":9}'

I've got the Build Name Setter and the Token Macro plugins installed. I'd like to do this in the Build Name plugin, if possible, although I can install any other plugins I need to get this to work.

2

2 Answers

1
votes

${ENV:9,var="FILE_PATH"} should do what you are looking for.

An alternative is to also eliminate the beginning of the string, as far as you know what exactly the string is:

${ENV#somepath/,var="FILE_PATH"}

In case you ever need to strip something from the end of the string instead, use % in place of #.

0
votes

I'm assuming you are using a Jenkinsfile pipeline script? If so, you can just use the groovy substring method.

variable.substring(9)

Not sure if you can do something similar using parameterized builds.