Jenkins多分支管道

问题描述 投票:0回答:1

我有一个叫做feature / xyz的分支。现在,我必须将文件名从filename.exe命名为filename_ $ BRANCH_NAME.exe

但是问题出在这里,因为我的分支名称前有斜杠,所以会抛出错误。

所以如何将我的文件命名为filename_feature_xyz?

jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli
1个回答
0
投票

下面的代码示例。本质上,您只可以使用字符串替换功能。但是,它进一步满足了未知的文件名,该文件名符合您在示例中列出的约定。

#!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}"
© www.soinside.com 2019 - 2024. All rights reserved.