Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #22 from moniker-dns/JENKINS-17535
Provide env vars detailing triggered builds.
  • Loading branch information
hagzag committed Apr 10, 2013
2 parents b8cde9e + 447cdd3 commit d6679f5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -61,6 +61,12 @@
<artifactId>parameterized-trigger</artifactId>
<version>2.12</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>envinject</artifactId>
<version>1.83</version>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -36,7 +36,7 @@
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;

Expand Down Expand Up @@ -131,6 +131,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
failed = true;
}
addSubBuild(thisBuild, thisProject, jobBuild);
addBuildEnvironmentVariables(thisBuild, jobBuild);
projectList.remove(project);
futuresList.remove(future);
break;
Expand Down Expand Up @@ -167,6 +168,50 @@ private void addSubBuild(MultiJobBuild thisBuild,
phaseName, jobBuild);
}

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

String jobName = jobBuild.getProject().getName();
String jobNameSafe = jobName.replaceAll("[^A-Za-z0-9]", "_").toUpperCase();
String buildNumber = Integer.toString(jobBuild.getNumber());
String buildResult = jobBuild.getResult().toString();

// 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);

if (variables.get("TRIGGERED_JOB_NAMES") == null) {
variables.put("TRIGGERED_JOB_NAMES", jobName);
} else {
String triggeredJobNames = variables.get("TRIGGERED_JOB_NAMES") + "," + jobName;
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 {
String runCount = Integer.toString(Integer.parseInt(variables.get("TRIGGERED_BUILD_RUN_COUNT_" + jobNameSafe)) + 1);
variables.put("TRIGGERED_BUILD_RUN_COUNT_" + jobNameSafe, runCount);
}


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

@SuppressWarnings("rawtypes")
private void prepareActions(AbstractBuild build, AbstractProject project,
PhaseJobsConfig projectConfig, BuildListener listener,
Expand Down

0 comments on commit d6679f5

Please sign in to comment.