Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PARTIALLY FIXED JENKINS-28632] Integration with Workflow to trigger a Workflow job #3

Merged
merged 7 commits into from Nov 29, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.509</version><!-- which version of Jenkins is this plugin built against? -->
<version>1.580.1</version><!-- minimun required version to work with Workflow -->
</parent>

<artifactId>deployment-notification</artifactId>
Expand Down
@@ -1,7 +1,6 @@
package org.jenkinsci.plugins.deployment;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.AutoCompletionCandidates;
import hudson.model.BuildableItem;
import hudson.model.Fingerprint;
Expand All @@ -15,6 +14,7 @@
import hudson.triggers.TriggerDescriptor;
import jenkins.model.FingerprintFacet;
import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -34,7 +34,7 @@
*
* @author Kohsuke Kawaguchi
*/
public class DeploymentTrigger extends Trigger<AbstractProject> {
public class DeploymentTrigger extends Trigger<Job> {
private final String upstreamJob;
private final Condition cond;

Expand All @@ -59,6 +59,12 @@ public void checkAndFire(DeploymentFacet facet) {
if (upstream==null)
upstream = Jenkins.getInstance().getItem(upstreamJob, job, Job.class);

ParameterizedJobMixIn parameterizedJobMixIn = new ParameterizedJobMixIn() {
@Override protected Job asJob() {
return job;
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a TODO comment for convenience method in 1.621+.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done.


RangeSet r = cond.calcMatchingBuildNumberOf(upstream, facet);
if (!r.isEmpty()) {
if (findTriggeredRecord(facet.getFingerprint()).add(this)) {
Expand All @@ -67,13 +73,13 @@ public void checkAndFire(DeploymentFacet facet) {
if (b!=null) {
// pass all the current parameters if we can
ParametersAction action = b.getAction(ParametersAction.class);
job.scheduleBuild(job.getQuietPeriod(), new UpstreamDeploymentCause(b), action);
parameterizedJobMixIn.scheduleBuild2(5, action);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐜 Use parameterizedJobMixIn.scheduleBuild(action) directly (it already uses the proper quiet period)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amuniz That is incorrect as scheduleBuild is not expecting an Action but a Cause.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then use parameterizedJobMixIn.scheduleBuild2(parameterizedJobMixIn.getQuietPeriod(), new CauseAction(new UpstreamDeploymentCause(b)), action). Otherwise you are going to loose the UpstreamDeploymentCause (which I guess it's used somewhere later).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are still loosing some information here, since new UpstreamDeploymentCause(b) is not passed to scheduleBuild2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but scheduleBuild2 doesn't allow you to add the Cause.

http://javadoc.jenkins-ci.org/jenkins/model/ParameterizedJobMixIn.html

@amuniz @jglick Any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try to wrap the cause in a CauseAction?

return;
}
}

// didn't find any matching build, so just trigger it but without the cause to link to the upstream
job.scheduleBuild(); // TODO: expose a version that takes name and build number
parameterizedJobMixIn.scheduleBuild(); // TODO: expose a version that takes name and build number
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -101,10 +107,15 @@ public static class ListenerImpl extends DeploymentFacetListener {
public void onChange(final DeploymentFacet facet, HostRecord newRecord) {
POOL.submit(new Runnable() {
public void run() {
for (AbstractProject<?,?> p : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
DeploymentTrigger t = p.getTrigger(DeploymentTrigger.class);
if (t!=null) {
t.checkAndFire(facet);
for (Job<?,?> job : Jenkins.getInstance().getAllItems(Job.class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note to use the new idiom in 1.621: TODO - 1.621: use getTrigger(Job<?,?> job, Class<T> clazz)

if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob pJob = (ParameterizedJobMixIn.ParameterizedJob) job;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just call getAllItems(ParameterizedJob.class).

for (Trigger trigger : pJob.getTriggers().values()) {
if (trigger instanceof DeploymentTrigger) {
DeploymentTrigger deploymentTrigger = (DeploymentTrigger) trigger;
deploymentTrigger.checkAndFire(facet);
}
}
}
}
}
Expand Down