Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JenkinsFile default workspace name is too long

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?

like image 243
pix Avatar asked May 11 '18 14:05

pix


2 Answers

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

like image 79
vishesh Avatar answered Oct 25 '22 04:10

vishesh


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()
    }
  }
like image 29
pix Avatar answered Oct 25 '22 04:10

pix