Skip to content

Commit

Permalink
[FIXED JENKINS-14545] Correct Env variables
Browse files Browse the repository at this point in the history
Extend and correct Enviroment variables that are provided when
using block until other projects are completed, in the trigger/call
other projects buildstep.

Extended vaiables to provide the full list of projects triggered, and
the full list of builds triggered when using the parameter factories.
  • Loading branch information
cjo9900 committed Nov 11, 2012
1 parent 1658624 commit 2a27692
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2011, Jørgen P. Tjernø <jorgenpt@gmail.com>
* Copyright (c) 2011-2, Jørgen P. Tjernø <jorgenpt@gmail.com> Chris Johnson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,14 +25,18 @@
package hudson.plugins.parameterizedtrigger;

import java.util.Map;
import java.util.Arrays;

import hudson.EnvVars;
import hudson.Util;
import hudson.model.EnvironmentContributingAction;
import hudson.model.AbstractBuild;

public class BuildInfoExporterAction implements EnvironmentContributingAction {
public static final String JOB_NAME_VARIABLE = "LAST_TRIGGERED_JOB_NAME";
public static final String ALL_JOBS_NAME_VARIABLE = "TRIGGERED_JOB_NAMES";
public static final String BUILD_NUMBER_VARIABLE_PREFIX = "TRIGGERED_BUILD_NUMBER_";
public static final String ALL_BUILD_NUMBER_VARIABLE_PREFIX = "TRIGGERED_BUILD_NUMBERS_";

private String buildName;
private int buildNumber;
Expand All @@ -57,7 +61,27 @@ public String getUrlName() {


public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
env.put(JOB_NAME_VARIABLE, buildName);
env.put(BUILD_NUMBER_VARIABLE_PREFIX + buildName, Integer.toString(buildNumber));
String sanatizedBuildName = buildName.toUpperCase().replaceAll("[^A-Z0-9]+", "_");
// Note: this will only indicate the last project in the list that is ran
env.put(JOB_NAME_VARIABLE, sanatizedBuildName);
// All Triggered job names
String originalvalue = env.get(ALL_JOBS_NAME_VARIABLE);
if(originalvalue == null) {
env.put(ALL_JOBS_NAME_VARIABLE, sanatizedBuildName);
} else {
String[] items = Util.tokenize(originalvalue, ",");
if(! Arrays.asList(items).contains(sanatizedBuildName))
env.put(ALL_JOBS_NAME_VARIABLE, originalvalue+","+sanatizedBuildName);
}
env.put(BUILD_NUMBER_VARIABLE_PREFIX + sanatizedBuildName, Integer.toString(buildNumber));

// handle case where multiple builds are triggered
String buildVariable = ALL_BUILD_NUMBER_VARIABLE_PREFIX + sanatizedBuildName;
originalvalue = env.get(buildVariable);
if(originalvalue == null) {
env.put(buildVariable, Integer.toString(buildNumber));
} else {
env.put(buildVariable, originalvalue+","+Integer.toString(buildNumber));
}
}
}
Expand Up @@ -41,4 +41,12 @@
case, the build will be considered as unstable based on the result of the
triggered builds and the value of this option.</li>
</ul>
With this option enabled the builds for the projects triggered are avalible as Env variables for future build steps<br/>
<ul>
<li><b>LAST_TRIGGERED_JOB_NAME</b>="Last project started"</li>
<li><b>TRIGGERED_JOB_NAMES</b>="Comma seperated list of all triggered projects"</li>
<li><b>TRIGGERED_BUILD_NUMBER_&lt;project name&gt;</b>="Last build number triggered"</li>
<li><b>TRIGGERED_BUILD_NUMBERS_&lt;project name&gt;</b>="Comma seperated list of build numbers triggered"</li>
</ul>
All Project names are converted into upper case and characters not A-Z or 0-9 are replaced by _(multiple characters are condensed into a single _).
</div>

0 comments on commit 2a27692

Please sign in to comment.