Skip to content

Commit

Permalink
Add option to display information about the .ci.yml/shell script that…
Browse files Browse the repository at this point in the history
… was used to run the build
  • Loading branch information
suryagaddipati committed Jan 17, 2017
1 parent 3437d7b commit e63281a
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.groupon.jenkins.buildtype.plugins.DotCiPluginAdapter;
import com.groupon.jenkins.buildtype.util.shell.ShellCommands;
import com.groupon.jenkins.buildtype.util.shell.ShellScriptRunner;
import com.groupon.jenkins.dynamic.build.DotCiBuildInfoAction;
import com.groupon.jenkins.dynamic.build.DynamicBuild;
import com.groupon.jenkins.dynamic.build.DynamicSubBuild;
import com.groupon.jenkins.dynamic.build.execution.BuildExecutionContext;
Expand All @@ -43,6 +44,8 @@
import hudson.matrix.Combination;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.util.List;
Expand All @@ -66,6 +69,7 @@ public Result runBuild(final DynamicBuild build, final BuildExecutionContext bui
return result;
}
final Map config = new GroovyYamlTemplateProcessor(getDotCiYml(build), buildEnvironment).getConfig();
addProcessedYamlToDotCiInfoAction(buildExecutionContext.getRun(), config);
this.buildConfiguration = new BuildConfiguration(config);
if (this.buildConfiguration.isSkipped()) {
build.skip();
Expand All @@ -85,6 +89,15 @@ public Result runBuild(final DynamicBuild build, final BuildExecutionContext bui
return result.combine(pluginResult).combine(notifierResult);
}

private void addProcessedYamlToDotCiInfoAction(final Run run, final Map config) {
final DotCiBuildInfoAction dotCiBuildInfoAction = run.getAction(DotCiBuildInfoAction.class);
if (dotCiBuildInfoAction == null) {
run.addAction(new DotCiBuildInfoAction(new Yaml().dump(config)));
} else {
dotCiBuildInfoAction.setBuildConfiguration(new Yaml().dump(config));
}
}

@Override
public Result runSubBuild(final Combination combination, final BuildExecutionContext buildExecutionContext, final BuildListener listener) throws IOException, InterruptedException {
final List<ShellCommands> commands = this.buildConfiguration.getCommands(combination, buildExecutionContext.getBuildEnvironmentVariables());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,54 @@

package com.groupon.jenkins.buildtype.util.shell;

import com.groupon.jenkins.dynamic.build.DotCiBuildInfoAction;
import com.groupon.jenkins.dynamic.build.execution.BuildExecutionContext;
import hudson.Functions;
import hudson.model.BuildListener;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.Shell;

import java.io.IOException;

public class ShellScriptRunner {
private BuildExecutionContext buildExecutionContext;
private BuildListener listener;
private final BuildExecutionContext buildExecutionContext;
private final BuildListener listener;

public ShellScriptRunner(BuildExecutionContext buildExecutionContext, BuildListener listener) {
public ShellScriptRunner(final BuildExecutionContext buildExecutionContext, final BuildListener listener) {
this.buildExecutionContext = buildExecutionContext;
this.listener = listener;

}

public Result runScript(ShellCommands commands) throws IOException, InterruptedException {
public Result runScript(final ShellCommands commands) throws IOException, InterruptedException {
Result r = Result.FAILURE;
//Todo: use VitualChannel to figure out OS
String shellInterpreter = Functions.isWindows() ? "sh" : "/bin/bash";
final String shellInterpreter = Functions.isWindows() ? "sh" : "/bin/bash";
final Run run = this.buildExecutionContext.getRun();
addExecutionInfoAction(run, commands);
try {
Shell execution = new Shell("#!" + shellInterpreter + " -le \n" + commands.toShellScript());
if (buildExecutionContext.performStep(execution, listener)) {
final Shell execution = new Shell("#!" + shellInterpreter + " -le \n" + commands.toShellScript());
if (this.buildExecutionContext.performStep(execution, this.listener)) {
r = Result.SUCCESS;
}
} catch (InterruptedException e) {
} catch (final InterruptedException e) {
r = Executor.currentExecutor().abortResult();
throw e;
} finally {
buildExecutionContext.setResult(r);
this.buildExecutionContext.setResult(r);
}
return r;
}

private void addExecutionInfoAction(final Run run, final ShellCommands commands) {
final DotCiBuildInfoAction dotCiBuildInfoAction = run.getAction(DotCiBuildInfoAction.class);
if (dotCiBuildInfoAction == null) {
run.addAction(new DotCiBuildInfoAction(commands));
} else {
dotCiBuildInfoAction.addCommands(commands);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
The MIT License (MIT)
Copyright (c) 2016, Groupon, 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 com.groupon.jenkins.dynamic.build;

import com.google.common.base.Joiner;
import com.groupon.jenkins.buildtype.util.shell.ShellCommands;
import hudson.model.Run;
import jenkins.model.RunAction2;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DotCiBuildInfoAction implements RunAction2 {
private List<String> commands;
private String buildConfiguration;

public DotCiBuildInfoAction() {
this.commands = new ArrayList<>();
this.buildConfiguration = "";
}

public DotCiBuildInfoAction(final ShellCommands commands) {
this();
this.commands = new ArrayList<>(commands.getCommands());

}

public DotCiBuildInfoAction(final String buildConfiguration) {
this();
this.buildConfiguration = buildConfiguration;
}

public String getBuildConfiguration() {
return this.buildConfiguration;
}

public void setBuildConfiguration(final String buildConfiguration) {
this.buildConfiguration = buildConfiguration;

}

public String getScript() {
return this.commands == null ? "" : Joiner.on("\n").join(this.commands);
}

public void doBuildScript(final StaplerRequest req, final StaplerResponse rsp) throws ServletException, IOException {
final InputStream file = new ByteArrayInputStream(getScript().getBytes(StandardCharsets.UTF_8));
rsp.serveFile(req, file, new Date().getTime(), 0, -1L, "dotci.sh");
}

@Override
public void onAttached(final Run<?, ?> r) {

}

@Override
public void onLoad(final Run<?, ?> r) {

}

@Override
public String getIconFileName() {
return "/plugin/DotCi/images/24x24/logo.png";
}

@Override
public String getDisplayName() {
return "DotCi Build Info";
}

@Override
public String getUrlName() {
return "dotCiBuildInfo";
}

public void addCommands(final ShellCommands commands) {
this.commands.addAll(commands.getCommands());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.model.CauseAction;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStep;
import hudson.util.HttpResponses;
Expand Down Expand Up @@ -333,6 +334,11 @@ public Map<String, Object> getBuildEnvironmentVariables() {
}
}

@Override
public Run getRun() {
return DynamicBuild.this;
}


@Override
protected Result doRun(final BuildListener listener) throws Exception, hudson.model.Run.RunnerAbortedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.model.Cause;
import hudson.model.Node;
import hudson.model.Result;
import hudson.model.Run;
import hudson.slaves.WorkspaceList;
import hudson.slaves.WorkspaceList.Lease;
import hudson.tasks.BuildStep;
Expand Down Expand Up @@ -196,6 +197,11 @@ public Map<String, Object> getBuildEnvironmentVariables() {
}
}

@Override
public Run getRun() {
return DynamicSubBuild.this;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal

import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.BuildStep;

import java.io.IOException;
Expand All @@ -38,4 +39,5 @@ public interface BuildExecutionContext {

Map<String, Object> getBuildEnvironmentVariables();

Run getRun();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core"
xmlns:l="/lib/layout" xmlns:f="/lib/form"
>
<l:layout>
<l:main-panel>
<div class="main">
<script
src="${resURL}/plugin/DotCi/js/highlight.pack.js"></script>
<link rel="stylesheet" type="text/css"
href="${resURL}/plugin/DotCi/css/github.css"/>

<script>hljs.initHighlightingOnLoad();</script>
<p>
<b>.ci.yml</b>
<pre>
<code class="yaml">${it.buildConfiguration}</code>
</pre>

</p>
<p>
<b>Bash Script</b>
<pre>
<code class="bash">${it.script}</code>
</pre>
<p>
<f:form method="post" action="buildScript"
name="downloadScript">
<f:submit value="Download Script"/>
</f:form>
</p>
</p>
</div>
</l:main-panel>
</l:layout>
</j:jelly>
5 changes: 5 additions & 0 deletions src/main/webapp/images/24x24/buildScript
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
chmod -R u+w . ; find . ! -path "./deploykey_rsa.pub" ! -path "./deploykey_rsa" -delete
git init
git remote add origin https://github.com/suryagaddipati/DotCi.git
git fetch origin master
git reset --hard 12a7850061f62184459b5590b35ee4f095659004
5 changes: 5 additions & 0 deletions src/main/webapp/images/24x24/downloadScript
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
chmod -R u+w . ; find . ! -path "./deploykey_rsa.pub" ! -path "./deploykey_rsa" -delete
git init
git remote add origin https://github.com/suryagaddipati/DotCi.git
git fetch origin master
git reset --hard 12a7850061f62184459b5590b35ee4f095659004
Binary file added src/main/webapp/images/24x24/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e63281a

Please sign in to comment.