Skip to content

JENKINS-57117 - Pipeline Support #7

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ THE SOFTWARE.
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.424</version>
<version>3.51</version> <!-- or whatever the newest version available is -->
</parent>

<artifactId>scripttrigger</artifactId>
Expand All @@ -52,6 +52,8 @@ THE SOFTWARE.
</developers>

<properties>
<jenkins.version>2.176.1</jenkins.version>
<java.level>8</java.level>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jenkinsci/plugins/scripttrigger/AbstractTrigger.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.jenkinsci.plugins.scripttrigger;

import antlr.ANTLRException;
import com.google.common.base.Charsets;
import hudson.Util;
import hudson.model.Action;
import hudson.model.BuildableItem;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected String getName() {
@Override
protected String getCause() {
try {
String scriptContent = Util.loadFile(getLogFile());
String scriptContent = Util.loadFile(getLogFile(), Charsets.UTF_8);
String cause = extractRootCause(scriptContent);
if (cause == null) {
return getDefaultMessageCause();
Expand All @@ -92,12 +93,12 @@ private String extractRootCause(String content) {
protected Action[] getScheduledActions(Node pollingNode, XTriggerLog log) {
String scriptContent;
try {
scriptContent = Util.loadFile(getLogFile());
scriptContent = Util.loadFile(getLogFile(), Charsets.UTF_8);
} catch (IOException e) {
return new Action[0];
}

List<Action> actionList = new ArrayList<Action>();
List<Action> actionList = new ArrayList<>();
String description = extractDescription(scriptContent);
if (description != null) {
actionList.add(new ScriptTriggerRunAction(description));
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/jenkinsci/plugins/scripttrigger/ScriptTrigger.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -123,12 +124,16 @@ private boolean checkIfModifiedWithScriptsEvaluation(Node executingNode, int exp

ScriptTriggerExecutor executor = getScriptTriggerExecutor(log);

EnvVarsResolver envVarsResolver = new EnvVarsResolver();
Map<String, String> envVars;
try {
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, executingNode);
} catch (EnvInjectException e) {
throw new ScriptTriggerException(e);
if (job instanceof AbstractProject) {
EnvVarsResolver envVarsResolver = new EnvVarsResolver();
try {
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, executingNode);
} catch (EnvInjectException e) {
throw new ScriptTriggerException(e);
}
} else {
envVars = new HashMap<>();
}

if (script != null) {
Expand Down Expand Up @@ -189,8 +194,8 @@ public InternalScriptTriggerAction(String actionTitle) {
}

@SuppressWarnings("unused")
public AbstractProject<?, ?> getOwner() {
return (AbstractProject) job;
public BuildableItem getOwner() {
return job;
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/jenkinsci/plugins/scripttrigger/ScriptTriggerExecutor.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import hudson.tasks.CommandInterpreter;
import hudson.tasks.Shell;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.remoting.RoleChecker;

import java.io.*;
import java.util.Map;
Expand Down Expand Up @@ -78,6 +79,9 @@ protected String getStringContent(Node executingNode, final String filePath) thr

try {
return executingNode.getRootPath().act(new FilePath.FileCallable<String>() {
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
StringBuffer content = new StringBuffer();
Expand Down Expand Up @@ -106,6 +110,10 @@ private int executeScript(final Node executingNode, final String scriptContent,
try {

boolean isUnix = executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

public Boolean call() throws ScriptTriggerException {
return File.pathSeparatorChar == ':';
}
Expand All @@ -125,6 +133,10 @@ public Boolean call() throws ScriptTriggerException {
throw new ScriptTriggerException("The node is offline.");
}
return rootPath.act(new FilePath.FileCallable<Integer>() {
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

public Integer invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
try {
return getLocalLauncher(log.getListener()).launch().cmds(cmd).envs(envVars).stdout(log.getListener()).pwd(rootPath).join();
Expand Down Expand Up @@ -159,6 +171,10 @@ private Launcher getLocalLauncher(TaskListener listener) throws ScriptTriggerExc
protected boolean existsScript(Node executingNode, final String path) throws ScriptTriggerException {
try {
return executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

public Boolean call() throws ScriptTriggerException {
File f = new File(path);
if (!f.exists()) {
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/org/jenkinsci/plugins/scripttrigger/groovy/GroovyScriptTrigger.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import hudson.model.*;
import hudson.remoting.VirtualChannel;
import hudson.security.ACL;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.jelly.XMLOutput;
Expand All @@ -40,6 +41,7 @@
import org.jenkinsci.plugins.scripttrigger.AbstractTrigger;
import org.jenkinsci.plugins.scripttrigger.LabelRestrictionClass;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerException;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
Expand All @@ -48,8 +50,6 @@
import java.nio.charset.Charset;
import java.util.*;

import jenkins.model.Jenkins;


/**
* @author Gregory Boissinot
Expand Down Expand Up @@ -127,6 +127,9 @@ protected Action[] getScheduledActions(Node pollingNode, final XTriggerLog log)
throw new ScriptTriggerException("The node is offline.");
}
return rootPath.act(new FilePath.FileCallable<Action[]>() {
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

public Action[] invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
File propFile = new File(propertiesFilePath);
Expand Down Expand Up @@ -166,25 +169,28 @@ protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws Scri
try {

GroovyScriptTriggerExecutor executor = getGroovyScriptTriggerExecutor(log);
final AbstractProject proj = (AbstractProject) job;

EnvVarsResolver envVarsResolver = new EnvVarsResolver();
Map<String, String> envVars;
try {
envVars = envVarsResolver.getPollingEnvVars(proj, pollingNode);
} catch (EnvInjectException e) {
throw new ScriptTriggerException(e);
if (job instanceof AbstractProject) {
EnvVarsResolver envVarsResolver = new EnvVarsResolver();
try {
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, pollingNode);
} catch (EnvInjectException e) {
throw new ScriptTriggerException(e);
}
} else {
envVars = new HashMap<>();
}

if (groovyExpression != null) {
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, proj, getGroovyExpression(), envVars, groovySystemScript);
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, job, getGroovyExpression(), envVars, groovySystemScript);
if (evaluationSucceed) {
return true;
}
}

if (groovyFilePath != null) {
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, proj, Util.replaceMacro(groovyFilePath, envVars), envVars, groovySystemScript);
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, job, Util.replaceMacro(groovyFilePath, envVars), envVars, groovySystemScript);
if (evaluationSucceed) {
return true;
}
Expand Down Expand Up @@ -229,8 +235,8 @@ public InternalGroovyScriptTriggerAction(String actionTitle) {
}

@SuppressWarnings("unused")
public AbstractProject<?, ?> getOwner() {
return (AbstractProject) job;
public BuildableItem getOwner() {
return job;
}

@Override
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/jenkinsci/plugins/scripttrigger/groovy/GroovyScriptTriggerExecutor.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
import groovy.lang.GroovyShell;
import hudson.PluginManager;
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.*;
import hudson.remoting.Callable;
import hudson.util.IOUtils;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerException;
import org.jenkinsci.plugins.scripttrigger.ScriptTriggerExecutor;
import org.jenkinsci.remoting.RoleChecker;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -48,18 +47,22 @@ public GroovyScriptTriggerExecutor(XTriggerLog log) {
super(log);
}

public boolean evaluateGroovyScript(Node executingNode, final AbstractProject proj, final String scriptContent, final Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
public boolean evaluateGroovyScript(Node executingNode, final BuildableItem items, final String scriptContent, final Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {

if (scriptContent == null) {
throw new NullPointerException("The script content object must be set.");
}
try {
if (groovySystemScript) {
log.info("Running as system script");
return evaluateGroovyScript(proj, scriptContent, envVars);
return evaluateGroovyScript(items, scriptContent, envVars);
}

return executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

public Boolean call() throws ScriptTriggerException {
log.info("Running as node script");
return evaluateGroovyScript(null, scriptContent, envVars);
Expand All @@ -80,7 +83,7 @@ public Boolean call() throws ScriptTriggerException {
}
}

private boolean evaluateGroovyScript(final AbstractProject proj, final String scriptContent, final Map<String, String> envVars) {
private boolean evaluateGroovyScript(final BuildableItem item, final String scriptContent, final Map<String, String> envVars) {
if (envVars != null) {
final StringBuilder envDebug = new StringBuilder("Replacing script vars using:");
for (final Map.Entry<String, String> envEntry : envVars.entrySet()) {
Expand Down Expand Up @@ -108,8 +111,8 @@ private boolean evaluateGroovyScript(final AbstractProject proj, final String sc

shell.setVariable("log", log);
shell.setVariable("out", log.getListener().getLogger());
if (proj != null) {
shell.setVariable("project", proj);
if (item != null) {
shell.setVariable("project", item);
}

//Evaluate the new script content
Expand Down Expand Up @@ -148,7 +151,7 @@ protected ClassLoader getClassLoader() {
return cl;
}

public boolean evaluateGroovyScriptFilePath(Node executingNode, AbstractProject proj, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
public boolean evaluateGroovyScriptFilePath(Node executingNode, BuildableItem item, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {

if (scriptFilePath == null) {
throw new NullPointerException("The scriptFilePath object must be set.");
Expand Down Expand Up @@ -189,7 +192,7 @@ public boolean evaluateGroovyScriptFilePath(Node executingNode, AbstractProject
scriptContent = getStringContent(executingNode, scriptFilePath);
}

return evaluateGroovyScript(executingNode, proj, scriptContent, envVars, groovySystemScript);
return evaluateGroovyScript(executingNode, item, scriptContent, envVars, groovySystemScript);
}

}
1 change: 1 addition & 0 deletions src/main/resources/index.jelly
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<div>
This plugin makes it possible to poll changes of an environment with a script.
</div>
1 change: 1 addition & 0 deletions src/main/resources/org/jenkinsci/plugins/scripttrigger/ScriptTrigger/config.jelly
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<!--
The MIT License (MIT)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<!--
The MIT License

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">

<f:entry field="groovySystemScript" title="${%Groovy System Script}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div>
<p>
If checked run the groovy script as a system script, the script will have access to the same
variables as the Groovy Console. The AbstractProject is also bound to the project variable.
variables as the Groovy Console. The BuildableItem is also bound to the project variable.
<br/>
If not checked run the groovy script on the executor node, the script will not have access
to the hudson or job model.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<!--
The MIT License

Expand Down