Reputation: 17
I have branch called feature/xyz. Now I have to name a file from filename.exe to filename_$BRANCH_NAME.exe
But problem here is as my branch name has fwd slash it is throwing an error.
So how can I name my file as filename_feature_xyz??
Upvotes: 1
Views: 707
Reputation: 2882
Example of code below. Essentially you can just use the string replace function. But went a bit further to cater for an unknown filename conforming to the convention you laid out in the example.
#!groovy
// Setup vars to replicate your questions specs
env.BRANCH_NAME = "feature/xyz"
String file = 'filename.exe'
// Replace any forward slash with an underscore
String branchName = (env.BRANCH_NAME).replace('/', '_')
// Split apart your current filename
List fileParts = file.tokenize('.')
// Construct the original filename, catering for multiple period usecases
String originalFileName = fileParts[0..-2].join('.')
// Seperate the extension for use later
String originalExtension = fileParts[-1]
// Combine into the desired filename as per your requirements
String newFileName = "${originalFileName}_${branchName}.${originalExtension}"
Upvotes: 1