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

Workflow/pipeline support #10

Open
wants to merge 4 commits into
base: main
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
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.436</version>
<version>1.642.3</version>
</parent>

<artifactId>unity3d-plugin</artifactId>
Expand Down Expand Up @@ -82,9 +82,16 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r06</version>
<version>19.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.2</version>
<type>jar</type>
<optional>true</optional>
</dependency>
</dependencies>

<repositories>
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/jenkinsci/plugins/unity3d/Helper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.jenkinsci.plugins.unity3d;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import hudson.util.ArgumentListBuilder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
*
* @author rstyrczula
*/
public class Helper {
public static Set<Integer> toIntegerSet(String csvIntegers) {
Set<Integer> result = new HashSet<Integer>();
if (! csvIntegers.trim().equals("")) {
result.addAll(Collections2.transform(Arrays.asList(csvIntegers.split(",")), new Function<String, Integer>() {
public Integer apply(String s) {
return Integer.parseInt(s.trim());
}
}));
}
return result;
}

public static String findCommandlineArgument(ArgumentListBuilder args, String arg) {
return findArgFromList(args.toList(), arg);
}

public static String findArgFromList(List<String> a, String arg) {
String customArg = null;
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i).equals(arg)) {
customArg = a.get(i+1);
}
}
return customArg;
}
}
110 changes: 63 additions & 47 deletions src/main/java/org/jenkinsci/plugins/unity3d/Unity3dBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.unity3d;

import hudson.AbortException;
import hudson.CopyOnWrite;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -9,7 +10,6 @@
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Result;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
Expand All @@ -21,21 +21,21 @@
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.logging.Logger;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepMonitor;
import jenkins.tasks.SimpleBuildStep;

import net.sf.json.JSONObject;
import org.jenkinsci.plugins.unity3d.io.Pipe;
import org.jenkinsci.plugins.unity3d.io.StreamCopyThread;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -49,7 +49,7 @@
* </u>
* @author Jerome Lacoste
*/
public class Unity3dBuilder extends Builder {
public class Unity3dBuilder extends Builder implements SimpleBuildStep {
private static final Logger log = Logger.getLogger(Unity3dBuilder.class.getName());

/**
Expand All @@ -66,7 +66,7 @@ public class Unity3dBuilder extends Builder {
public Unity3dBuilder(String unity3dName, String argLine, String unstableReturnCodes) {
this.unity3dName = unity3dName;
this.argLine = argLine;
this.unstableReturnCodes = unstableReturnCodes;
this.unstableReturnCodes = Util.fixNull(unstableReturnCodes);
}

@SuppressWarnings("unused")
Expand All @@ -90,8 +90,16 @@ public String getUnstableReturnCodes() {
return unstableReturnCodes;
}

/**
* @since 1.4
*/
@DataBoundSetter
public void setUnstableReturnCodes(String unstableReturnCodes) {
this.unstableReturnCodes = Util.fixNull(unstableReturnCodes);
}

Set<Integer> toUnstableReturnCodesSet() {
return toIntegerSet(unstableReturnCodes);
return Helper.toIntegerSet(unstableReturnCodes);
}

private String getArgLineOrGlobalArgLine() {
Expand All @@ -114,11 +122,32 @@ private PerformException(String s) {
super(s);
}
}


@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

@Override // SimpleBuildStep.perform
public void perform(Run<?, ?> run, FilePath fp, Launcher lnchr, TaskListener tl) throws InterruptedException, AbortException {
try
{
_perform(run, fp, lnchr, tl);
}
catch (PerformException e) {
tl.fatalError(e.getMessage());
throw new AbortException(e.getMessage());
} catch (IOException e) {
Util.displayIOException(e, tl);
String errorMessage = Messages.Unity3d_ExecUnexpectedlyFailed();
e.printStackTrace(tl.fatalError(errorMessage));
}
}

@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException {
try {
_perform(build, launcher, listener);
_perform(build, build.getWorkspace(), launcher, listener);
return true;
} catch (PerformException e) {
listener.fatalError(e.getMessage());
Expand All @@ -131,12 +160,12 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
}
}

private void _perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException, PerformException {
private void _perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException, PerformException {
EnvVars env = build.getEnvironment(listener);

Unity3dInstallation ui = getAndConfigureUnity3dInstallation(listener, env);

ArgumentListBuilder args = prepareCommandlineArguments(build, launcher, ui, env);
Unity3dInstallation ui = getAndConfigureUnity3dInstallation(listener, env, workspace);

ArgumentListBuilder args = prepareCommandlineArguments(build, workspace, launcher, ui, env);

String customLogFile = findLogFileArgument(args);

Expand All @@ -150,7 +179,7 @@ private void _perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener
StreamCopyThread copierThread = new StreamCopyThread("Pipe editor.log to output thread.", pipe.getIn(), ca);
try {
copierThread.start();
int r = launcher.launch().cmds(args).envs(env).stdout(ca).pwd(build.getWorkspace()).join();
int r = launcher.launch().cmds(args).envs(env).stdout(ca).pwd(workspace).join();
// r == 11 means executeMethod could not be found ?
checkProcResult(build, r);
} finally {
Expand Down Expand Up @@ -178,7 +207,7 @@ private void _perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener
}
}

private void checkProcResult(AbstractBuild<?, ?> build, int result) throws PerformException {
private void checkProcResult(Run<?, ?> build, int result) throws PerformException {
log.info("Unity command line exited with error code: " + result);
if (isBuildUnstable(result)) {
log.info(Messages.Unity3d_BuildMarkedAsUnstableBecauseOfStatus(result));
Expand All @@ -200,39 +229,38 @@ private boolean isBuildSuccess(int result) {

/** Find the -logFile argument from the built arg line **/
private String findLogFileArgument(ArgumentListBuilder args) {
String customLogFile = null;
List<String> a = args.toList();
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i).equals("-logFile")) {
customLogFile = a.get(i+1);
}
}
return customLogFile;
return Helper.findCommandlineArgument(args, "-logFile");
}

private ArgumentListBuilder prepareCommandlineArguments(AbstractBuild<?,?> build, Launcher launcher, Unity3dInstallation ui, EnvVars vars) throws IOException, InterruptedException, PerformException {
private ArgumentListBuilder prepareCommandlineArguments(Run<?,?> build, FilePath workspace, Launcher launcher, Unity3dInstallation ui, EnvVars vars) throws IOException, InterruptedException, PerformException {
String exe;
try {
exe = ui.getExecutable(launcher);
} catch (RuntimeException re) {
throw new PerformException(re.getMessage());
}

if(build instanceof AbstractBuild<?, ?>) {
AbstractBuild<?, ?> abstractBuild = (AbstractBuild<?, ?>)build;
FilePath moduleRoot = abstractBuild.getModuleRoot();
String moduleRootRemote = moduleRoot.getRemote();
Map<String,String> buildParameters = abstractBuild.getBuildVariables();

FilePath moduleRoot = build.getModuleRoot();
String moduleRootRemote = moduleRoot.getRemote();
Map<String,String> buildParameters = build.getBuildVariables();

return createCommandlineArgs(exe, moduleRootRemote, vars, buildParameters);
return createCommandlineArgs(exe, moduleRootRemote, vars, buildParameters);
} else {
// build variables should be parsed by groovy script
return createCommandlineArgs(exe, workspace.getRemote(), vars, null);
}
}

private Unity3dInstallation getAndConfigureUnity3dInstallation(BuildListener listener, EnvVars env) throws PerformException, IOException, InterruptedException {
private Unity3dInstallation getAndConfigureUnity3dInstallation(TaskListener listener, EnvVars env, FilePath workspace) throws PerformException, IOException, InterruptedException {
Unity3dInstallation ui = getUnity3dInstallation();

if(ui==null) {
throw new PerformException(Messages.Unity3d_NoUnity3dInstallation());
}

ui = ui.forNode(Computer.currentComputer().getNode(), listener);
ui = ui.forNode(workspace.toComputer().getNode(), listener);
ui = ui.forEnvironment(env);
return ui;
}
Expand Down Expand Up @@ -267,18 +295,6 @@ private Unity3dInstallation getUnity3dInstallation() {
return null;
}

static Set<Integer> toIntegerSet(String csvIntegers) {
Set<Integer> result = new HashSet<Integer>();
if (! csvIntegers.trim().equals("")) {
result.addAll(Collections2.transform(Arrays.asList(csvIntegers.split(",")), new Function<String, Integer>() {
public Integer apply(String s) {
return Integer.parseInt(s.trim());
}
}));
}
return result;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
Expand Down Expand Up @@ -310,7 +326,7 @@ public void setInstallations(Unity3dInstallation... antInstallations) {

public FormValidation doCheckUnstableReturnCodes(@QueryParameter String value) {
try {
toIntegerSet(value);
Helper.toIntegerSet(value);
return FormValidation.ok();
} catch (RuntimeException re) {
return FormValidation.error(Messages.Unity3d_InvalidParamUnstableReturnCodes(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import jenkins.security.MasterToSlaveCallable;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstallation;
Expand Down Expand Up @@ -60,7 +60,7 @@ public Unity3dInstallation forNode(Node node, TaskListener log) throws IOExcepti
* Gets the executable path of this Unity3dBuilder on the given target system.
*/
public String getExecutable(Launcher launcher) throws IOException, InterruptedException {
return launcher.getChannel().call(new Callable<String, IOException>() {
return launcher.getChannel().call(new MasterToSlaveCallable<String, IOException>() {
public String call() throws IOException {
return checkUnity3dExecutablePath(getHome());
}
Expand Down Expand Up @@ -129,7 +129,7 @@ private static String checkUnity3dExecutablePath(String home) {
* @throws IOException
*/
public Future<Long> pipeEditorLog(final Launcher launcher, final String customLogFile, final OutputStream ros) throws IOException {
return launcher.getChannel().callAsync(new Callable<Long, IOException>() {
return launcher.getChannel().callAsync(new MasterToSlaveCallable<Long, IOException>() {
public Long call() throws IOException {
return new PipeFileAfterModificationAction(getEditorLogFile(customLogFile).getAbsolutePath(), ros, true).call();
}
Expand All @@ -144,7 +144,7 @@ public Long call() throws IOException {
* @throws InterruptedException
*/
public String getEditorLogPath(final Launcher launcher, final String customLogFile) throws IOException, InterruptedException {
return launcher.getChannel().call(new Callable<String, IOException>() {
return launcher.getChannel().call(new MasterToSlaveCallable<String, IOException>() {
public String call() throws IOException {
return getEditorLogFile(customLogFile).getAbsolutePath();
}
Expand Down
Loading