6

I am currently setting up jenkins with bitbucket. I have create a new jenkins project as multibranch project.

The JenkinsFile is hosted inside the git repository. How can I force jenkins to generate a shorter branch name than the default one.

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

How can I get ride of ZKIZ7BNGL6RTDKLQAQ7QR4FKZMOB3DDAVZ564BLWT2BY5ZV652VA

This is my jenkinsFile

#!/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

Is there a way to force Jenkins to generate a shorter name?

pix
  • 1,264
  • 19
  • 32

2 Answers2

9

You can change value of jenkins.branch.WorkspaceLocatorImpl.PATH_MAX=20 in jenkins's script console.

Changes will be lost if you restart jenkins server. To make the changes permanent, add this java property -Djenkins.branch.WorkspaceLocatorImpl.PATH_MAX=20

vishesh
  • 113
  • 1
  • 7
  • This does not work for me because I am not able to change any parameters as @Vishesh Jindal suggest it. If I can not test it, I will not accept this answer even if it is working for the others. sorry – pix Oct 31 '18 at 14:45
  • @pix you need to have ssh access to your jenkins instance for this or privileges to administer on Jenkins to use script console. – vishesh Nov 27 '18 at 10:37
  • @VisheshJindal I do not have any admin acess or ssh access – pix May 07 '19 at 09:21
3

This is not the best way to fix it, but it is working :)

First create a method to get the current workspace and rework the final path like this:

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
    }
}

Then you have to set it up to our agent like this

pipeline {

environment {
   //Your Env Setup
  }

  agent { //Global Agent.
    node {
      label 'AgentName'
      customWorkspace GetWorkspace()
    }
  }
pix
  • 1,264
  • 19
  • 32