JenkinsFile默认工作区名称太长

问题描述 投票:3回答:2

我目前正在用bitbucket设置jenkins。我创建了一个新的jenkins项目作为multibranch项目。

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

E:\jenkins\workspace\reposName-BrancheName-ZKIZ7BNGL6RTDKLQAQ7QR4FKZMOB3DDAVZ564BLWT2BY5ZV652VA

我怎么能摆脱qazxsw poi

这是我的jenkinsFile

ZKIZ7BNGL6RTDKLQAQ7QR4FKZMOB3DDAVZ564BLWT2BY5ZV652VA

有没有办法迫使Jenkins生成一个较短的名字?

jenkins-pipeline multibranch-pipeline
2个回答
7
投票

您可以在jenkins脚本控制台中更改#!/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服务器,更改将会丢失。要使更改成为永久更改,请添加此java属性jenkins.branch.WorkspaceLocatorImpl.PATH_MAX=20


-1
投票

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

首先创建一个方法来获取当前工作空间并返回最终路径,如下所示:

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