Skip to content

Commit

Permalink
Merge pull request #47 from ikedam/feature/JENKINS-16028_EnvActionWhe…
Browse files Browse the repository at this point in the history
…nNeeded

[JENKINS-16028] [JENKINS-18762] Adds EnvAction when it's needed
  • Loading branch information
ikedam committed Oct 11, 2014
2 parents 22eb37c + 581c57e commit c11b826
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 17 deletions.
20 changes: 4 additions & 16 deletions src/main/java/hudson/plugins/copyartifact/CopyArtifact.java
Expand Up @@ -289,9 +289,11 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
}
// Add info about the selected build into the environment
EnvAction envData = build.getAction(EnvAction.class);
if (envData != null) {
envData.add(getItemGroup(build), expandedProject, src.getNumber());
if (envData == null) {
envData = new EnvAction();
build.addAction(envData);
}
envData.add(getItemGroup(build), expandedProject, src.getNumber());
if (target.length() > 0) targetDir = new FilePath(targetDir, env.expand(target));
expandedFilter = env.expand(filter);
if (expandedFilter.trim().length() == 0) expandedFilter = "**";
Expand Down Expand Up @@ -497,20 +499,6 @@ private static List<CopyArtifact> getCopiers(AbstractProject<?,?> project) throw
}
}

// Listen for new builds and add EnvAction in any that use CopyArtifact build step
@Extension
public static final class CopyArtifactRunListener extends RunListener<Build> {
public CopyArtifactRunListener() {
super(Build.class);
}

@Override
public void onStarted(Build r, TaskListener listener) {
if (((Build<?,?>)r).getProject().getBuildersList().get(CopyArtifact.class) != null)
r.addAction(new EnvAction());
}
}

private static class EnvAction implements EnvironmentContributingAction {
// Decided not to record this data in build.xml, so marked transient:
private transient Map<String,String> data = new HashMap<String,String>();
Expand Down
69 changes: 68 additions & 1 deletion src/test/java/hudson/plugins/copyartifact/CopyArtifactTest.java
Expand Up @@ -33,8 +33,11 @@
import hudson.matrix.TextAxis;
import hudson.matrix.MatrixRun;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.*;
import hudson.model.Cause.UserCause;
import hudson.plugins.copyartifact.testutils.FileWriteBuilder;
import hudson.plugins.copyartifact.testutils.WrapperBuilder;
import hudson.security.ACL;
import hudson.security.Permission;
import hudson.security.AuthorizationMatrixProperty;
Expand Down Expand Up @@ -152,7 +155,7 @@ private MatrixProject createMatrixArtifactProject() throws IOException {
return p;
}

private static void assertFile(boolean exists, String path, Build<?,?> b)
private static void assertFile(boolean exists, String path, AbstractBuild<?,?> b)
throws IOException, InterruptedException {
if (b.getWorkspace().child(path).exists() != exists)
assertEquals(path + ": " + getLog(b), exists, !exists);
Expand Down Expand Up @@ -900,6 +903,70 @@ public void testEnvData() throws Exception {
assertEquals("4", envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_MY_TEST_JOB"));
}

@Bug(16028)
public void testEnvDataInMavenProject() throws Exception {
FreeStyleProject upstream = createFreeStyleProject("upstream");
upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
upstream.getPublishersList().add(new ArtifactArchiver("**/*", "", false, false));
FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get();
assertBuildStatusSuccess(upstreamBuild);

MavenModuleSet downstream = setupMavenJob();
downstream.getPrebuilders().add(new CopyArtifact(
"upstream",
"",
new SpecificBuildSelector(Integer.toString(upstreamBuild.getNumber())),
"**/*",
"",
"",
false,
false,
false
));
CaptureEnvironmentBuilder envStep = new CaptureEnvironmentBuilder();
downstream.getPrebuilders().add(envStep);

MavenModuleSetBuild downstreamBuild = downstream.scheduleBuild2(0).get();
assertBuildStatusSuccess(downstreamBuild);
assertFile(true, "artifact.txt", downstreamBuild);
assertEquals(
Integer.toString(upstreamBuild.getNumber()),
envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_UPSTREAM")
);
}

@Bug(18762)
public void testEnvDataWrapped() throws Exception {
FreeStyleProject upstream = createFreeStyleProject("upstream");
upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
upstream.getPublishersList().add(new ArtifactArchiver("**/*", "", false, false));
FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get();
assertBuildStatusSuccess(upstreamBuild);

FreeStyleProject downstream = createFreeStyleProject();
downstream.getBuildersList().add(new WrapperBuilder(new CopyArtifact(
"upstream",
"",
new SpecificBuildSelector(Integer.toString(upstreamBuild.getNumber())),
"**/*",
"",
"",
false,
false,
false
)));
CaptureEnvironmentBuilder envStep = new CaptureEnvironmentBuilder();
downstream.getBuildersList().add(envStep);

FreeStyleBuild downstreamBuild = downstream.scheduleBuild2(0).get();
assertBuildStatusSuccess(downstreamBuild);
assertFile(true, "artifact.txt", downstreamBuild);
assertEquals(
Integer.toString(upstreamBuild.getNumber()),
envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_UPSTREAM")
);
}

/**
* Test filtering on parameters, ie. last stable build with parameter FOO=bar.
*/
Expand Down
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright (c) 2014 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.plugins.copyartifact.testutils;

import java.io.IOException;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.tasks.Builder;

/**
* A builder wraps another buillder.
*/
public class WrapperBuilder extends Builder {
private final Builder builder;

public WrapperBuilder(Builder builder) {
this.builder = builder;
}

public Builder getBuilder() {
return builder;
}

@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
return builder.prebuild(build, listener);
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
return builder.perform(build, launcher, listener);
}
}

0 comments on commit c11b826

Please sign in to comment.