Soo Wei Tan
Soo Wei Tan

Reputation: 3371

Dynamically generating Hudson custom workspace path

I'm trying to get a Hudson job to get built in a custom workspace path that is automatically generated using yyyyMMdd-HHmm. I can get the $BUILD_ID variable expanded as mentioned in bug 3997, and that seems to work fine. However, the workspace path is incorrect as it is of the format yyyy-MM-dd_HH-mm-ss. I've tried using the ZenTimestamp plugin v2.0.1, which changes the $BUILD_ID, but this only seems to take effect after the workspace is created.

Is there a method of defining a custom workspace in the manner that I want it?

Upvotes: 0

Views: 836

Answers (1)

Chalz
Chalz

Reputation: 26

You can use a groovy script to achieve that.

import hudson.model.*;
import hudson.util.*;
import java.util.*;
import java.text.*;
import java.io.*;

//Part 1 : Recover build parameter
AbstractBuild currentBuild = (AbstractBuild) Thread.currentThread().executable;
def envVars= currentBuild.properties.get("envVars");
def branchName = envVars["BRANCH_NAME"];

//Part 2 : Define new workspace Path
def newWorkspace = "C:\\Build\\"+branchName;

//Part 3 : Change current build workspace
def newWorspaceFilePath = new FilePath(new File(newWorkspace));
currentBuild.setWorkspace(newWorspaceFilePath);

Upvotes: 1

Related Questions