JenkinsFile 默认工作区名称太长



我目前正在用bitbucket设置jenkins。 我已经创建了一个新的 jenkins 项目作为多分支项目。

JenkinsFile 托管在 git 存储库中。 如何强制 jenkins 生成比默认分支名称更短的分支名称。

E:jenkinsworkspacereposName-BrancheName-ZKIZ7BNGL6RTDKLQAQ7QR4FKZMOB3DDAVZ564BLWT2BY5ZV652VA

如何乘坐ZKIZ7BNGL6RTDKLQAQ7QR4FKZMOB3DDAVZ564BLWT2BY5ZV652VA

这是我的詹金斯文件

#!/usr/bin/env groovy
env.PATH = env.PATH + ";c:\Windows\System32"
def call(String label = null, Closure body) {
node(label) {
String path = pwd()
String branchName = env.BRANCH_NAME
if (branchName) {
path = path.split(Pattern.quote(File.separator))
def workspaceRoot = path[0..<-1].join(File.separator)
def currentWs = path[-1]
String newWorkspace = env.JOB_NAME.replace('/', '-')
newWorkspace = newWorkspace.replace(File.separator, '-')
newWorkspace = newWorkspace.replace('%2f', '-')
newWorkspace = newWorkspace.replace('%2F', '-')
if (currentWs =~ '@') {
newWorkspace = "${newWorkspace}@${currentWs.split('@')[-1]}"
}
path = "${workspaceRoot}${File.separator}${newWorkspace}"
}
ws(path) {
body()
}
}
}
pipeline 
{
} // pipeline

有没有办法强制詹金斯生成一个更短的名字?

您可以在 jenkins 的脚本控制台中更改jenkins.branch.WorkspaceLocatorImpl.PATH_MAX=20的值。

如果重新启动 Jenkins 服务器,更改将丢失。要使更改永久化,请将此 java 属性添加到-Djenkins.branch.WorkspaceLocatorImpl.PATH_MAX=20

这不是修复它的最佳方法,但它正在:)

首先创建一个方法来获取当前工作区,并像这样重新设计最终路径:

def GetWorkspace()
{
node
{
String path = pwd()
String branchName = env.BRANCH_NAME
if(branchName)
{
path = path.split(Pattern.quote(File.separator))
def workspaceRoot = path[0..<-1].join(File.separator)
def currentWs = path[-1]
// Here is where we make branch names safe for directories -
// the most common bad character is '/' in 'feature/add_widget'
// which gets replaced with '%2f', so JOB_NAME will be
// ${PR}}OJECT_NAME}%2f${BRANCH_NAME}
String newWorkspace = env.JOB_NAME.replace('/', '-')
newWorkspace = newWorkspace.replace(File.separator, '-')
newWorkspace = newWorkspace.replace('%2f', '-')
newWorkspace = newWorkspace.replace('%2F', '-')
// Add on the '@n' suffix if it was there
if (currentWs =~ '@') 
{
newWorkspace = "${newWorkspace}@${currentWs.split('@')[-1]}"
}
path = "E:\Jenkins\workspace\${File.separator}${newWorkspace}"
}
return path
}
}

然后,您必须像这样将其设置为我们的代理

pipeline {
environment {
//Your Env Setup
}
agent { //Global Agent.
node {
label 'AgentName'
customWorkspace GetWorkspace()
}
}

相关内容

最新更新