24
24
package hudson.matrix;
26
import hudson.EnvVars;
26
27
import hudson.FilePath;
27
28
import hudson.model.AbstractBuild;
29
import hudson.model.Build;
30
import hudson.model.Node;
28
31
import hudson.slaves.WorkspaceList;
29
32
import hudson.slaves.WorkspaceList.Lease;
30
import static hudson.matrix.MatrixConfiguration.useShortWorkspaceName;
31
import hudson.model.Build;
32
import hudson.model.Node;
33
33
import org.kohsuke.stapler.Ancestor;
34
34
import org.kohsuke.stapler.Stapler;
35
35
import org.kohsuke.stapler.StaplerRequest;
145
145
public void run() {
146
run(new RunnerImpl());
146
execute(new MatrixRunExecution());
149
protected class RunnerImpl extends Build<MatrixConfiguration,MatrixRun>.RunnerImpl {
149
private class MatrixRunExecution extends BuildExecution {
150
protected Lease getParentWorkspaceLease(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
151
MatrixProject mp = getParent().getParent();
153
String customWorkspace = mp.getCustomWorkspace();
154
if (customWorkspace != null) {
155
// we allow custom workspaces to be concurrently used between jobs.
156
return Lease.createDummyLease(n.getRootPath().child(getEnvironment(listener).expand(customWorkspace)));
158
return wsl.allocate(n.getWorkspaceFor(mp), getParentBuild());
151
162
protected Lease decideWorkspace(Node n, WorkspaceList wsl) throws InterruptedException, IOException {
152
// Map current combination to a directory subtree, e.g. 'axis1=a,axis2=b' to 'axis1/a/axis2/b'.
154
if(useShortWorkspaceName) {
155
subtree = getParent().getDigestName();
157
subtree = getParent().getCombination().toString('/','/');
160
String customWorkspace = getParent().getParent().getCustomWorkspace();
161
if (customWorkspace != null) {
162
// Use custom workspace as defined in the matrix project settings.
163
FilePath ws = n.getRootPath().child(getEnvironment(listener).expand(customWorkspace));
164
// We allow custom workspaces to be used concurrently between jobs.
165
return Lease.createDummyLease(ws.child(subtree));
167
// Use default workspace as assigned by Hudson.
168
Node node = getBuiltOn();
169
FilePath ws = node.getWorkspaceFor(getParent().getParent());
170
// Allocate unique workspace (not to be shared between jobs and runs).
171
return wsl.allocate(ws.child(subtree));
163
MatrixProject mp = getParent().getParent();
165
// lock is done at the parent level, so that concurrent MatrixProjects get respective workspace,
166
// but within MatrixConfigurations that belong to the same MatrixBuild.
167
// if MatrixProject is configured with custom workspace, we assume that the user knows what he's doing
168
// and try not to append unique random suffix.
169
Lease baseLease = getParentWorkspaceLease(n,wsl);
171
// resolve the relative path against the parent workspace, which needs locking
172
FilePath baseDir = baseLease.path;
174
// prepare variables that can be used in the child workspace setting
175
EnvVars env = getEnvironment(listener);
176
env.put("COMBINATION",getParent().getCombination().toString('/','/')); // e.g., "axis1/a/axis2/b"
177
env.put("SHORT_COMBINATION",getParent().getDigestName()); // e.g., "0fbcab35"
178
env.put("PARENT_WORKSPACE",baseDir.getRemote());
179
env.putAll(getBuildVariables());
181
// child workspace need no individual locks, whether or not we use custom workspace
182
String childWs = mp.getChildCustomWorkspace();
183
return Lease.createLinkedDummyLease(baseDir.child(env.expand(childWs)),baseLease);