Skip to content
Permalink
Browse files
- Exposed subbuild info through remote API and environment variables.
- Corrected how environment variables were being injected
- Minor tweaks to the envrionment variables that are injected
  • Loading branch information
dkichler committed Jul 26, 2013
1 parent 0c8bacc commit 18e18888a3978b29138ecc2818bd4f7597706001
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
@@ -5,6 +5,7 @@
import hudson.model.Build;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Api;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
@@ -20,20 +21,23 @@

import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

@ExportedBean(defaultVisibility=999)
public class MultiJobBuild extends Build<MultiJobProject, MultiJobBuild> {

public MultiJobBuild(MultiJobProject project) throws IOException {
super(project);
}

MultiJobChangeLogSet changeSets = new MultiJobChangeLogSet(this);

@Override
public ChangeLogSet<? extends Entry> getChangeSet() {
return super.getChangeSet();
}

public void addChangeLogSet(ChangeLogSet<? extends Entry> changeLogSet) {
this.changeSets.addChangeLogSet(changeLogSet);
}
@@ -150,12 +154,14 @@ public void addSubBuild(String parentJobName, int parentBuildNumber,

private List<SubBuild> subBuilds;

@Exported
public List<SubBuild> getSubBuilds() {
if (subBuilds == null)
subBuilds = new ArrayList<SubBuild>();
return subBuilds;
}

@ExportedBean(defaultVisibility=999)
public static class SubBuild {

private final String parentJobName;
@@ -214,10 +220,12 @@ public int getParentBuildNumber() {
return parentBuildNumber;
}

@Exported
public String getJobName() {
return jobName;
}

@Exported
public int getBuildNumber() {
return buildNumber;
}
@@ -226,6 +234,7 @@ public void setResult(Result result) {
this.result = result;
}

@Exported
public Result getResult() {
return result;
}
@@ -1,6 +1,8 @@
package com.tikal.jenkins.plugins.multijob;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.console.HyperlinkNote;
@@ -20,6 +22,7 @@
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.scm.ChangeLogSet;
import hudson.scm.SCM;
import hudson.scm.ChangeLogSet.Entry;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
@@ -33,12 +36,15 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.servlet.ServletException;

import net.sf.json.JSONObject;
import org.jenkinsci.plugins.envinject.EnvInjectBuilderContributionAction;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.jenkinsci.lib.envinject.EnvInjectLogger;
import org.jenkinsci.plugins.envinject.EnvInjectBuilderContributionAction;
import org.jenkinsci.plugins.envinject.service.EnvInjectActionSetter;
import org.jenkinsci.plugins.envinject.service.EnvInjectEnvVars;
import org.jenkinsci.plugins.envinject.service.EnvInjectVariableGetter;

public class MultiJobBuilder extends Builder implements DependecyDeclarer {

@@ -53,7 +59,7 @@ public MultiJobBuilder(String phaseName, List<PhaseJobsConfig> phaseJobs,
this.phaseJobs = Util.fixNull(phaseJobs);
this.continuationCondition = continuationCondition;
}

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
@@ -131,7 +137,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
failed = true;
}
addSubBuild(thisBuild, thisProject, jobBuild);
addBuildEnvironmentVariables(thisBuild, jobBuild);
addBuildEnvironmentVariables(thisBuild, jobBuild, listener);
projectList.remove(project);
futuresList.remove(future);
break;
@@ -169,7 +175,7 @@ private void addSubBuild(MultiJobBuild thisBuild,
}

@SuppressWarnings("rawtypes")
private void addBuildEnvironmentVariables(MultiJobBuild thisBuild, AbstractBuild jobBuild) {
private void addBuildEnvironmentVariables(MultiJobBuild thisBuild, AbstractBuild jobBuild, BuildListener listener) {
// Env variables map
Map<String, String> variables = new HashMap<String, String>();

@@ -180,11 +186,8 @@ private void addBuildEnvironmentVariables(MultiJobBuild thisBuild, AbstractBuild

// These will always reference the last build
variables.put("LAST_TRIGGERED_JOB_NAME", jobName);
variables.put("TRIGGERED_BUILD_NUMBER_" + jobNameSafe, buildNumber);
variables.put("TRIGGERED_BUILD_RESULT_" + jobNameSafe, buildResult);

// Per-Build Variables
variables.put("TRIGGERED_BUILD_RESULT_" + jobNameSafe + "RUN" + buildNumber, buildResult);
variables.put(jobNameSafe + "_BUILD_NUMBER", buildNumber);
variables.put(jobNameSafe + "_BUILD_RESULT", buildResult);

if (variables.get("TRIGGERED_JOB_NAMES") == null) {
variables.put("TRIGGERED_JOB_NAMES", jobName);
@@ -193,13 +196,6 @@ private void addBuildEnvironmentVariables(MultiJobBuild thisBuild, AbstractBuild
variables.put("TRIGGERED_JOB_NAMES", triggeredJobNames);
}

if (variables.get("TRIGGERED_BUILD_NUMBERS_" + jobNameSafe) == null) {
variables.put("TRIGGERED_BUILD_NUMBERS_" + jobNameSafe, buildNumber);
} else {
String triggeredBuildNumbers = variables.get("TRIGGERED_BUILD_NUMBERS_" + jobNameSafe) + "," + buildNumber;
variables.put("TRIGGERED_BUILD_NUMBERS_" + jobNameSafe, triggeredBuildNumbers);
}

if (variables.get("TRIGGERED_BUILD_RUN_COUNT_" + jobNameSafe) == null) {
variables.put("TRIGGERED_BUILD_RUN_COUNT_" + jobNameSafe, "1");
} else {
@@ -209,8 +205,37 @@ private void addBuildEnvironmentVariables(MultiJobBuild thisBuild, AbstractBuild


//Set the new build variables map
thisBuild.addAction(new EnvInjectBuilderContributionAction(variables));
addEnvVars(thisBuild, listener, variables);
}

private void addEnvVars(AbstractBuild<?,?> build, BuildListener listener, Map<String,String> incomingVars) {

EnvInjectLogger logger = new EnvInjectLogger(listener);
FilePath ws = build.getWorkspace();
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(ws);
EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger);

try {

EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
Map<String, String> previousEnvVars = variableGetter.getEnvVarsPreviousSteps(build, logger);

//Get current envVars
Map<String, String> variables = new HashMap<String, String>(previousEnvVars);

//Resolve variables
final Map<String, String> resultVariables = envInjectEnvVarsService.getMergedVariables(variables, incomingVars);

//Set the new build variables map
build.addAction(new EnvInjectBuilderContributionAction(resultVariables));

//Add or get the existing action to add new env vars
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction(build, resultVariables);
} catch (Throwable throwable) {
listener.getLogger().println("[MultiJob] - [ERROR] - Problems occurs on injecting env vars as a build step: " + throwable.getMessage());
}
}


@SuppressWarnings("rawtypes")
private void prepareActions(AbstractBuild build, AbstractProject project,

0 comments on commit 18e1888

Please sign in to comment.