JenkinsFile default workspace name is too long
Asked Answered
I

2

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?

Ibeam answered 11/5, 2018 at 14:37 Comment(5)
Why exactly is it a problem? Are you getting an error somewhere?Partee
@Partee : yes, the file path is too long on a windows server :/ limitation from the 80's :pIbeam
ahhh, I do not have the answer unfortunately. Other than switch to linux ;)Partee
ah, use ws() to set your own workspace. See https://mcmap.net/q/604431/-what-does-a-ws-block-do-in-jenkinsPartee
A really long path can also break Python virtualenvs. Just running pip gives really strange errors, which hint at the shebang line not working correctly.Windflower
I
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()
    }
  }
Ibeam answered 23/5, 2018 at 8:20 Comment(2)
This seems like a more work than necessary compared to just setting a limit with a JVM argumentDeciliter
I wish I could change the JVM argument.... @Deciliter but thx for this helpfull comment :)Ibeam
Q
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

Quell answered 13/9, 2018 at 8:30 Comment(3)
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. sorryIbeam
@Ibeam you need to have ssh access to your jenkins instance for this or privileges to administer on Jenkins to use script console.Quell
@VisheshJindal I do not have any admin acess or ssh accessIbeam
I
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()
    }
  }
Ibeam answered 23/5, 2018 at 8:20 Comment(2)
This seems like a more work than necessary compared to just setting a limit with a JVM argumentDeciliter
I wish I could change the JVM argument.... @Deciliter but thx for this helpfull comment :)Ibeam

© 2022 - 2024 — McMap. All rights reserved.