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

Adds Pipeline support for Batch and PowerShell #6

Open
wants to merge 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
work

.idea
/target/
31 changes: 23 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.10</version>
<version>3.25</version>
</parent>

<groupId>org.jenkinsci.plugins</groupId>
Expand All @@ -18,7 +18,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.javadoc.skip>true</maven.javadoc.skip>
<jenkins.version>1.642.3</jenkins.version>
<jenkins.version>2.7.3</jenkins.version>
<java.level>8</java.level>
</properties>

<url>https://wiki.jenkins-ci.org/display/JENKINS/Managed+Script+Plugin</url>
Expand Down Expand Up @@ -50,7 +51,22 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.12.1</version>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>durable-task</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.16</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -80,11 +96,10 @@
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<version>1.106</version>
<extensions>true</extensions>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* The MIT License
*
* Copyright 2014 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.managedscripts;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.TaskListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import org.jenkinsci.plugins.durabletask.DurableTaskDescriptor;
import org.jenkinsci.plugins.durabletask.FileMonitoringTask;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Runs a Windows batch script.
*/
public final class ManagedBatchScript extends FileMonitoringTask {

private static final Logger LOGGER = Logger.getLogger(ManagedBatchScriptStep.class.getName());
private final String script;
private boolean capturingOutput;
private final String[] buildStepArgs;

@DataBoundConstructor
public ManagedBatchScript(String script, String[] buildStepArgs) {
this.script = script;
this.buildStepArgs = buildStepArgs.clone();
}

public String getScript() {
return script;
}

@Override
public void captureOutput() {
capturingOutput = true;
}

@SuppressFBWarnings(value = "VA_FORMAT_STRING_USES_NEWLINE", justification = "%n from master might be \\n")
@Override
protected FileMonitoringController doLaunch(FilePath ws, Launcher launcher, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
if (launcher.isUnix()) {
throw new IOException("Batch scripts can only be run on Windows nodes");
}
BatchController c = new BatchController(ws);

String cmd;
if (capturingOutput) {
cmd = String.format("@echo off \r\ncmd /c \"\"%s\"\" > \"%s\" 2> \"%s\"\r\necho %%ERRORLEVEL%% > \"%s\"\r\n",
quote(c.getBatchFile2(ws)),
quote(c.getOutputFile(ws)),
quote(c.getLogFile(ws)),
quote(c.getResultFile(ws)));
} else {
cmd = String.format("@echo off \r\ncmd /c \"\"%s\"\" > \"%s\" 2>&1\r\necho %%ERRORLEVEL%% > \"%s\"\r\n",
quote(c.getBatchFile2(ws)),
quote(c.getLogFile(ws)),
quote(c.getResultFile(ws)));
}
c.getBatchFile1(ws).write(cmd, "UTF-8");
c.getBatchFile2(ws).write(getMangedScriptCommandLine(ws), "UTF-8");

Launcher.ProcStarter ps = launcher.launch().cmds("cmd", "/c", "\"\"" + c.getBatchFile1(ws) + "\"\"").envs(escape(envVars)).pwd(ws).quiet(true);
listener.getLogger().println("[" + ws.getRemote().replaceFirst("^.+\\\\", "") + "] Running batch script"); // details printed by cmd
ps.readStdout().readStderr(); // TODO see BourneShellScript
ps.start();
return c;
}

private String getMangedScriptCommandLine(FilePath ws) {
String modifiedScript;
Config buildStepConfig = ConfigFiles.getByIdOrNull(Jenkins.getInstance(), script);
if (buildStepConfig == null) {
throw new IllegalStateException(org.jenkinsci.plugins.managedscripts.Messages.config_does_not_exist(script));
}
modifiedScript = buildStepConfig.content + "\r\nexit %ERRORLEVEL%";
try {
FilePath scriptPath = ws.createTextTempFile("managedbatch", ".bat", modifiedScript);
return buildCommandLine(scriptPath);
} catch (IOException | InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Error creating tmp file");
throw new RuntimeException(ex);
}
}

private String buildCommandLine(FilePath script) {
StringBuilder commandline = new StringBuilder("cmd /c call ");
commandline.append(script.getRemote());

// Add additional parameters set by user
if (buildStepArgs != null) {
for (String arg : buildStepArgs) {
commandline.append(" ");
commandline.append(arg);
}
}

return commandline.toString();
}

private static String quote(FilePath f) {
return f.getRemote().replace("%", "%%");
}

private static final class BatchController extends FileMonitoringController {

private BatchController(FilePath ws) throws IOException, InterruptedException {
super(ws);
}

public FilePath getBatchFile1(FilePath ws) throws IOException, InterruptedException {
return controlDir(ws).child("jenkins-wrap.bat");
}

public FilePath getBatchFile2(FilePath ws) throws IOException, InterruptedException {
return controlDir(ws).child("jenkins-main.bat");
}

private static final long serialVersionUID = 1L;
}

@Extension
public static final class DescriptorImpl extends DurableTaskDescriptor {

@Override
public String getDisplayName() {
return "Managed Windows batch";
}

}

}
Loading