Skip to content

Commit 7da5ac7

Browse files
committed
Additional fix for handling folder paths with custom workspaces
1 parent 8148243 commit 7da5ac7

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

core/src/main/kotlin/com/code42/jenkins/pipelinekt/core/Pipeline.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,23 @@ data class Pipeline(
7777
} else if (useMultibranchWorkspace) {
7878
writer.writeln("// Calculate custom workspace for multibranch pipelines")
7979
writer.writeln("// This prevents workspace path truncation and branch conflicts")
80-
writer.writeln("// by using a relative path that includes the sanitized job name")
80+
writer.writeln("// by using a relative path that includes the sanitized job path")
8181
writer.writeln("def customWorkspacePath = null")
8282
writer.writeln("if (env.BRANCH_NAME) {")
8383
val innerWriter = writer.inner()
84-
innerWriter.writeln("// Decode URL-encoded characters (e.g., %2F -> /) from job name and sanitize for safe filesystem paths")
85-
innerWriter.writeln("def decodedJobName = java.net.URLDecoder.decode(env.JOB_NAME, 'UTF-8')")
86-
innerWriter.writeln("def safeJobName = decodedJobName.replaceAll(/[^A-Za-z0-9._-]/, '_')")
87-
innerWriter.writeln("customWorkspacePath = \"./workspace//\${safeJobName}\"")
84+
innerWriter.writeln("// Preserve folder hierarchy from JOB_NAME and sanitize only the final segment")
85+
innerWriter.writeln("def rawParts = env.JOB_NAME.tokenize('/')")
86+
innerWriter.writeln("def folderParts = rawParts.size() > 1 ? rawParts[0..-2] : []")
87+
innerWriter.writeln("def rawLeaf = rawParts ? rawParts[-1] : env.JOB_NAME")
88+
innerWriter.writeln("")
89+
innerWriter.writeln("// Decode last segment (handles %2F etc); then replace '/' and non-safe chars with '_'")
90+
innerWriter.writeln("def decodedLeaf = java.net.URLDecoder.decode(rawLeaf, 'UTF-8')")
91+
innerWriter.writeln("def safeLeaf = decodedLeaf.replaceAll('/', '_').replaceAll(/[^A-Za-z0-9._-]/, '_')")
92+
innerWriter.writeln("")
93+
innerWriter.writeln("// Sanitize folder parts but keep '/' between them")
94+
innerWriter.writeln("def safeFolderPath = folderParts.collect { it.replaceAll(/[^A-Za-z0-9._-]/, '_') }.join('/')")
95+
innerWriter.writeln("")
96+
innerWriter.writeln("customWorkspacePath = safeFolderPath ? \"./workspace//\${safeFolderPath}/\${safeLeaf}\" : \"./workspace//\${safeLeaf}\"")
8897
writer.writeln("}")
8998
writer.writeln("")
9099
}

core/src/test/kotlin/com/code42/jenkins/pipelinekt/core/PipelineWorkspaceTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ class PipelineWorkspaceTest {
3030
println("=== Generated Jenkinsfile with useMultibranchWorkspace=true ===")
3131
println(result)
3232

33-
// Verify it contains the runtime check
33+
// Verify it contains the runtime check and path building logic
3434
assertTrue(result.contains("if (env.BRANCH_NAME)"), "Should check for BRANCH_NAME to detect multibranch")
35-
assertTrue(result.contains("def decodedJobName = java.net.URLDecoder.decode(env.JOB_NAME, 'UTF-8')"), "Should decode URL-encoded job name")
36-
assertTrue(result.contains("def safeJobName = decodedJobName.replaceAll(/[^A-Za-z0-9._-]/, '_')"), "Should sanitize job name")
35+
assertTrue(result.contains("def rawParts = env.JOB_NAME.tokenize('/')"))
36+
assertTrue(result.contains("def decodedLeaf = java.net.URLDecoder.decode(rawLeaf, 'UTF-8')"))
37+
assertTrue(result.contains("def safeLeaf = decodedLeaf.replaceAll('/', '_').replaceAll(/[^A-Za-z0-9._-]/, '_')"))
38+
assertTrue(result.contains("def safeFolderPath = folderParts.collect { it.replaceAll(/[^A-Za-z0-9._-]/, '_') }.join('/')"))
3739
assertTrue(result.contains("customWorkspacePath"), "Should define customWorkspacePath")
3840
assertTrue(result.contains("customWorkspace customWorkspacePath"), "Should use customWorkspacePath in agent")
3941
}

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.21.14
1+
0.21.15

0 commit comments

Comments
 (0)