Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #59 from ikedam/feature/JENKINS-26694_ParametersBu…
…ildFilterForWorkflow

[JENKINS-26694] Make ParameterBuildFilter applicable to WorkflowJobs.
  • Loading branch information
ikedam committed Mar 1, 2015
2 parents ec41cbd + 1308f8d commit 7802e45
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Expand Up @@ -24,8 +24,11 @@
package hudson.plugins.copyartifact;

import hudson.EnvVars;
import hudson.model.ParameterValue;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.StringParameterValue;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,6 +82,22 @@ public boolean isSelectable(Run<?,?> run, EnvVars env) {
} catch (Exception ex) {
return false;
}
if(!(run instanceof AbstractBuild)) {
// Abstract#getEnvironment(TaskListener) put build parameters to
// environments, but Run#getEnvironment(TaskListener) doesn't.
// That means we can't retrieve build parameters from WorkflowRun
// as it is a subclass of Run, not of AbstractBuild.
// We need expand build parameters manually.
// See JENKINS-26694 for details.
for(ParametersAction pa: run.getActions(ParametersAction.class)) {
// We have to extract parameters manally as ParametersAction#buildEnvVars
// (overrides EnvironmentContributingAction#buildEnvVars)
// is applicable only for AbstractBuild.
for(ParameterValue pv: pa.getParameters()) {
pv.buildEnvironment(run, otherEnv);
}
}
}
for (StringParameterValue spv : filters) {
if (!spv.value.equals(otherEnv.get(spv.getName()))) {
return false;
Expand Down
Expand Up @@ -23,13 +23,24 @@
*/
package hudson.plugins.copyartifact;

import static org.junit.Assert.*;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.model.Cause.UserCause;
import hudson.plugins.copyartifact.testutils.CopyArtifactUtil;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
Expand Down Expand Up @@ -60,6 +71,58 @@ public void test_simpleUntriggeredCopy() throws Exception {
assertArtifactInArchive(b);
}

/**
* Test filtering on parameters works to copy from workflow jobs.
*/
@Bug(26694)
@Test
public void testFilterByParametersForWorkflow() throws Exception {
WorkflowJob copiee = createWorkflow("copiee",
"writeFile text: \"${PARAM}\", file:'artifact.txt';"
+ "archive includes:'artifact.txt';"
);
copiee.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("PARAM", "")
));
copiee.setDefinition(new CpsFlowDefinition(
"node {"
+ "writeFile text: \"${PARAM}\", file:'artifact.txt';"
+ "archive includes:'artifact.txt';"
+ "}",
true
));

FreeStyleProject copier = jenkinsRule.createFreeStyleProject();
copier.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("PARAM_TO_COPY", "")
));
copier.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(
copiee.getFullName(),
"PARAM=${PARAM_TO_COPY}",
new LastCompletedBuildSelector(),
"artifact.txt",
"",
false,
false
));

// #1: PARAM=foo
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0, new ParametersAction(
new StringParameterValue("PARAM", "foo")
)));
// #2: PARAM=bar
jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0, new ParametersAction(
new StringParameterValue("PARAM", "bar")
)));

FreeStyleBuild build = copier.scheduleBuild2(0, new UserCause(), new ParametersAction(
new StringParameterValue("PARAM_TO_COPY", "foo")
)).get();
jenkinsRule.assertBuildStatusSuccess(build);

assertEquals("foo", build.getWorkspace().child("artifact.txt").readToString());
}

private void assertArtifactInArchive(Run b) {
List<WorkflowRun.Artifact> artifacts = b.getArtifacts();
Assert.assertEquals(1, artifacts.size());
Expand Down

0 comments on commit 7802e45

Please sign in to comment.