diff --git a/src/main/java/org/jenkinsci/plugins/registry/notification/EnvContributor.java b/src/main/java/org/jenkinsci/plugins/registry/notification/EnvContributor.java new file mode 100644 index 0000000..3744442 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/registry/notification/EnvContributor.java @@ -0,0 +1,60 @@ +/* + * The MIT License + * + * Copyright (c) 2016, CloudBees, Inc. + * + * 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 org.jenkinsci.plugins.registry.notification; + +import hudson.EnvVars; +import hudson.Extension; +import hudson.model.EnvironmentContributor; +import hudson.model.ParameterValue; +import hudson.model.Run; +import hudson.model.TaskListener; +import org.jenkinsci.plugins.registry.notification.webhook.WebHookCause; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.util.Set; + +/** + * Provides environment variables to builds triggered by the plugin. + * + * @author Robert Sandell <rsandell@cloudbees.com>. + */ +@Extension +@Restricted(NoExternalUse.class) +public class EnvContributor extends EnvironmentContributor { + + @Override + public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) throws IOException, InterruptedException { + WebHookCause cause = (WebHookCause)r.getCause(WebHookCause.class); + if (cause != null) { + Set parameters = cause.getPushNotification().getRunParameters(); + for (ParameterValue parameter : parameters) { + parameter.buildEnvironment(r, envs); + } + } + } +} diff --git a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/JSONWebHook.java b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/JSONWebHook.java index b18356c..78c26a8 100644 --- a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/JSONWebHook.java +++ b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/JSONWebHook.java @@ -123,7 +123,7 @@ public void run() { } private void schedule(@Nonnull final Job job, @Nonnull final PushNotification pushNotification) { - if (new JobbMixIn(job).schedule(pushNotification.getCause(), pushNotification.getJobParamerers())) { + if (new JobbMixIn(job).schedule(pushNotification.getCause())) { logger.info(pushNotification.getCauseMessage()); Coordinator coordinator = Coordinator.getInstance(); if (coordinator != null) { @@ -174,7 +174,7 @@ static class JobbMixIn & ParameterizedJobMixIn.Para private JobT the; - public JobbMixIn(JobT the) { + JobbMixIn(JobT the) { this.the = the; } @@ -183,13 +183,13 @@ protected JobT asJob() { return the; } - public boolean schedule(Cause cause, Set parameters) { + boolean schedule(Cause cause) { if (!asJob().isBuildable()) { return false; } List queueActions = new LinkedList(); - queueActions.add(new ParametersAction(getParameterValues(parameters))); + queueActions.add(new ParametersAction(getParameterValues())); queueActions.add(new CauseAction(cause)); int quiet = Math.max(MIN_QUIET, asJob().getQuietPeriod()); @@ -207,12 +207,11 @@ public boolean schedule(Cause cause, Set parameters) { return i != null && i.getFuture() != null; } - private List getParameterValues(Set parameters) { + private List getParameterValues() { Set result = new HashSet(); if (isParameterized()) { result.addAll(getDefaultParametersValues()); } - result.addAll(parameters); return Collections.unmodifiableList(new LinkedList(result)); } diff --git a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/PushNotification.java b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/PushNotification.java index 30f62f8..9ef2e63 100644 --- a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/PushNotification.java +++ b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/PushNotification.java @@ -23,12 +23,14 @@ */ package org.jenkinsci.plugins.registry.notification.webhook; +import hudson.Util; import hudson.model.Cause; import hudson.model.ParameterValue; import hudson.model.Run; import org.jenkinsci.plugins.registry.notification.webhook.dockerhub.DockerHubPushNotification; import javax.annotation.CheckForNull; +import java.util.Collections; import java.util.Date; import java.util.Set; @@ -73,7 +75,25 @@ public int hashCode() { abstract public Cause getCause(); - abstract public Set getJobParamerers(); + /** + * Provide parameters to be put into a build. + * @return the parameters + * @deprecated misspelled and wrong context naming. Use {@link #getRunParameters()} + */ + @Deprecated + public Set getJobParamerers() { + if (Util.isOverridden(PushNotification.class, getClass(), "getRunParameters")) { + return getRunParameters(); + } + return Collections.emptySet(); + } + + public Set getRunParameters() { + if (Util.isOverridden(PushNotification.class, getClass(), "getJobParamerers")) { + return getJobParamerers(); + } + return Collections.emptySet(); + } abstract public String getCauseMessage(); diff --git a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerhub/DockerHubPushNotification.java b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerhub/DockerHubPushNotification.java index 32b32d2..99a65cc 100644 --- a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerhub/DockerHubPushNotification.java +++ b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerhub/DockerHubPushNotification.java @@ -84,7 +84,7 @@ public Cause getCause() { } @Override - public Set getJobParamerers() { + public Set getRunParameters() { Set parameters = new HashSet(); parameters.add(new StringParameterValue(KEY_REPO_NAME, getRepoName())); String host = getRegistryHost(); diff --git a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerregistry/DockerRegistryPushNotification.java b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerregistry/DockerRegistryPushNotification.java index 47ba681..dcb2e3c 100644 --- a/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerregistry/DockerRegistryPushNotification.java +++ b/src/main/java/org/jenkinsci/plugins/registry/notification/webhook/dockerregistry/DockerRegistryPushNotification.java @@ -67,7 +67,7 @@ public Cause getCause() { } @Override - public Set getJobParamerers() { + public Set getRunParameters() { Set parameters = new HashSet(); parameters.add(new StringParameterValue(KEY_REPO_NAME, getRepoName())); String host = getRegistryHost(); diff --git a/src/test/java/org/jenkinsci/plugins/registry/notification/DockerHubWebHookTest.java b/src/test/java/org/jenkinsci/plugins/registry/notification/DockerHubWebHookTest.java index 528256c..18338b4 100644 --- a/src/test/java/org/jenkinsci/plugins/registry/notification/DockerHubWebHookTest.java +++ b/src/test/java/org/jenkinsci/plugins/registry/notification/DockerHubWebHookTest.java @@ -23,14 +23,26 @@ */ package org.jenkinsci.plugins.registry.notification; +import hudson.EnvVars; +import hudson.Launcher; +import hudson.model.AbstractBuild; +import hudson.model.BuildListener; +import hudson.model.FreeStyleBuild; +import hudson.tasks.BuildStep; +import hudson.tasks.Builder; +import jenkins.tasks.SimpleBuildStep; import org.jenkinsci.plugins.registry.notification.opt.impl.TriggerOnSpecifiedImageNames; import hudson.model.FreeStyleProject; import hudson.model.Result; +import org.jenkinsci.plugins.registry.notification.webhook.dockerhub.DockerHubPushNotification; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.MockBuilder; +import java.io.IOException; +import java.util.Map; + public class DockerHubWebHookTest { @Rule @@ -48,4 +60,30 @@ public void testDoIndex() throws Exception { j.assertLogContains(repoName, project.getLastBuild()); } + + @Test(timeout = 60000) + public void testEnvironment() throws Exception { + FreeStyleProject project = j.createFreeStyleProject(); + final String repoName = "cb/jenkins"; + project.addTrigger(new DockerHubTrigger(new TriggerOnSpecifiedImageNames(repoName))); + project.getBuildersList().add(new PrintEnvironment()); + project.getBuildersList().add(new MockBuilder(Result.SUCCESS)); + j.createWebClient().goTo("dockerhub-webhook/debug?image=" + repoName); + + j.waitUntilNoActivity(); + FreeStyleBuild build = project.getLastBuild(); + j.assertLogContains(repoName, build); + j.assertLogContains(DockerHubPushNotification.KEY_REPO_NAME + " = " + repoName, build); + } + + static class PrintEnvironment extends Builder { + @Override + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { + EnvVars vars = build.getEnvironment(listener); + for (Map.Entry var : vars.entrySet()) { + listener.getLogger().println(var.getKey() + " = " + var.getValue()); + } + return true; + } + } } \ No newline at end of file