Skip to content
Permalink
Browse files

[JENKINS-46511] Option to only trigger downstream pipelines when the …

…generated artifact has been "mvn deploy", not "mvn package" or "mvn install"
  • Loading branch information...
cyrille-leclerc committed Sep 3, 2017
1 parent e431bd8 commit f73418a3f4948f2c7709107dcfac637c465463ce
@@ -18,7 +18,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
@@ -141,24 +140,40 @@ protected void recordDependencies(List<MavenSpyLogProcessor.MavenDependency> dep

protected void recordGeneratedArtifacts(@Nonnull Element mavenSpyLogsElt, @Nonnull Run run, @Nonnull TaskListener listener, @Nonnull PipelineMavenPluginDao dao) {
List<MavenSpyLogProcessor.MavenArtifact> generatedArtifacts = listArtifacts(mavenSpyLogsElt);
recordGeneratedArtifacts(generatedArtifacts, run, listener, dao);
List<Element> mvnDeployEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, "MojoSucceeded", "org.apache.maven.plugins", "maven-deploy-plugin", "deploy");
List<Element> nexusDeployEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, "MojoSucceeded", "org.sonatype.plugins", "nexus-staging-maven-plugin", "deploy");

boolean artifactsHaveBeenMvnDeployed = !mvnDeployEvents.isEmpty() || !nexusDeployEvents.isEmpty();

recordGeneratedArtifacts(generatedArtifacts, artifactsHaveBeenMvnDeployed, run, listener, dao);
}

protected void recordGeneratedArtifacts(List<MavenSpyLogProcessor.MavenArtifact> generatedArtifacts, @Nonnull Run run, @Nonnull TaskListener listener, @Nonnull PipelineMavenPluginDao dao) {
/**
*
* @param generatedArtifacts deployed artifacts
* @param artifactsHaveBeenMvnDeployed artifacts have been deployed using "mvn deploy" or "mvn nexus-staging-maven-plugin:deploy"
* @param run
* @param listener
* @param dao
*/
protected void recordGeneratedArtifacts(List<MavenSpyLogProcessor.MavenArtifact> generatedArtifacts, boolean artifactsHaveBeenMvnDeployed, @Nonnull Run run, @Nonnull TaskListener listener, @Nonnull PipelineMavenPluginDao dao) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] pipelineGraphPublisher - recordGeneratedArtifacts...");
}
for(MavenSpyLogProcessor.MavenArtifact artifact: generatedArtifacts) {
boolean skipDownstreamPipelines = this.skipDownstreamTriggers ||
(this.skipDownstreamTriggersForNonDeployedArtifacts && !artifactsHaveBeenMvnDeployed);

if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Build {0}#{1} - record generated {2}:{3}, version:{4}, skipDownstreamTriggers:{5}",
new Object[]{run.getParent().getFullName(), run.getNumber(), artifact.getId(), artifact.type, artifact.version, skipDownstreamTriggers});
listener.getLogger().println("[withMaven] pipelineGraphPublisher - Record generated artifact: " + artifact.getId() + ", version: " + artifact.version + ", skipDownstreamTriggers: " + skipDownstreamTriggers +
LOGGER.log(Level.FINE, "Build {0}#{1} - record generated {2}:{3}, version:{4}, skipDownstreamTriggers:{5}, artifactsHaveBeenMvnDeployed: {6}",
new Object[]{run.getParent().getFullName(), run.getNumber(),
artifact.getId(), artifact.type, artifact.version,
skipDownstreamTriggers, artifactsHaveBeenMvnDeployed});
listener.getLogger().println("[withMaven] pipelineGraphPublisher - Record generated artifact: " + artifact.getId() + ", version: " + artifact.version +
", skipDownstreamTriggers: " + skipDownstreamTriggers + ", artifactsHaveBeenMvnDeployed:" + artifactsHaveBeenMvnDeployed +
", file: " + artifact.file);
}
boolean skipDownstreamPipelines = this.skipDownstreamTriggers ||
(this.skipDownstreamTriggersForNonDeployedArtifacts && !Objects.equals(artifact.baseVersion, artifact.version));
dao.recordGeneratedArtifact(run.getParent().getFullName(), run.getNumber(),
dao.recordGeneratedArtifact(run.getParent().getFullName(), run.getNumber(),
artifact.groupId, artifact.artifactId, artifact.version, artifact.type, artifact.baseVersion,
skipDownstreamPipelines);
}
@@ -40,6 +40,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -197,9 +198,54 @@ public static String toString(@Nullable Node node) {
return result;
}

/*
<ExecutionEvent type="MojoSucceeded" class="org.apache.maven.lifecycle.internal.DefaultExecutionEvent" _time="2017-02-02 23:03:17.06">
<project artifactIdId="supplychain-portal" groupId="com.acmewidgets.supplychain" name="supplychain-portal" version="0.0.7" />
<plugin executionId="default-test" goal="test" groupId="org.apache.maven.plugins" artifactId="maven-surefire-plugin" version="2.18.1">
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
</plugin>
</ExecutionEvent>
*/

/**
* Relativize path
*
* @param mavenSpyLogs
* @param eventType e.g. "MojoSucceeded"
* @param pluginGroupId e.g. "org.apache.maven.plugins" artifactId=
* @param pluginArtifactId e.g. "maven-surefire-plugin"
* @param pluginGoal e.g. "test"
* @return
*/
@Nonnull
public static List<Element> getExecutionEvents(@Nonnull Element mavenSpyLogs, String eventType, String pluginGroupId, String pluginArtifactId, String pluginGoal) {
List<Element> result = new ArrayList<>();
for (Element executionEventElt : getChildrenElements(mavenSpyLogs, "ExecutionEvent")) {

if (executionEventElt.getAttribute("type").equals(eventType)) {
Element pluginElt = XmlUtils.getUniqueChildElementOrNull(executionEventElt, "plugin");
if (pluginElt == null) {
// ignore unexpected
} else {
if (pluginElt.getAttribute("groupId").equals(pluginGroupId) &&
pluginElt.getAttribute("artifactId").equals(pluginArtifactId) &&
pluginElt.getAttribute("goal").equals(pluginGoal)) {
result.add(executionEventElt);
} else {
// ignore non matching plugin
}
}
} else {
// ignore not supported event type
}

}
return result;
}


/**
* Relativize path
* <p>
* TODO replace all the workarounds (JENKINS-44088, JENKINS-46084, mac special folders...) by a unique call to
* {@link File#getCanonicalPath()} on the workspace for the whole "MavenSpyLogProcessor#processMavenSpyLogs" code block.
* We donb't want to pay an RPC call to {@link File#getCanonicalPath()} each time.

0 comments on commit f73418a

Please sign in to comment.
You can’t perform that action at this time.