Skip to content

Commit

Permalink
getting the new hotness over with the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelneale committed Feb 17, 2016
2 parents 53dd862 + 11da7c7 commit 9ddd122
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
target
work

20 changes: 20 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,20 @@
The MIT License

Copyright (c) 2016, 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.
91 changes: 91 additions & 0 deletions pom.xml
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.3</version>
<relativePath/>
</parent>

<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>simple-build</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>Simple opinionated build plugin for Jenkins Pipeline</name>
<description>This plugin provides a Pipeline step to read a .travis.yml file and run it as a part of a Pipeline job.</description>
<!-- TODO: Make a wiki page -->
<url>https://wiki.jenkins-ci.org/display/JENKINS/TODO+Plugin</url>
<licenses>
<license>
<name>MIT License</name>
<url>http://opensource.org/licenses/MIT</url>
</license>
</licenses>

<properties>
<jenkins.version>1.609.3</jenkins.version>
<jenkins-test-harness.version>2.1</jenkins-test-harness.version>
<java.level>6</java.level>
<workflow.version>1.13</workflow.version>
</properties>

<developers>
<developer>
<id>mneale</id>
<name>Michael Neale</name>
<email>michael.neale@gmail.com</email>
</developer>
</developers>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>docker-workflow</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.4.2</version>
</dependency>


</dependencies>
</project>
@@ -0,0 +1,84 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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.simplebuild;

import groovy.lang.Binding;
import hudson.Extension;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.ProxyWhitelist;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist;
import org.jenkinsci.plugins.workflow.cps.CpsScript;
import org.jenkinsci.plugins.workflow.cps.CpsThread;
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;

import java.io.File;
import java.io.IOException;

@Extension
public class SimpleBuildDSL extends GlobalVariable {
@Override
public String getName() {
return "simpleBuild";
}

@Override
public Object getValue(CpsScript script) throws Exception {
Binding binding = script.getBinding();
Object pipelineDSL;

// If we already have a travisPipelineConverter defined, reuse it. Otherwise, load it from the DSL groovy and
// add it to the binding.
if (binding.hasVariable(getName())) {
pipelineDSL = binding.getVariable(getName());
} else {
CpsThread c = CpsThread.current();
if (c == null)
throw new IllegalStateException("Expected to be called from CpsThread");

File converterGroovy = new File(getClass().getClassLoader().getResource("SimpleBuild.groovy").getFile());
pipelineDSL = c.getExecution()
.getShell()
.getClassLoader()
.parseClass(converterGroovy)
.newInstance();
binding.setVariable(getName(), pipelineDSL);
}

return pipelineDSL;
}

@Extension
public static class MiscWhitelist extends ProxyWhitelist {
/**
* Methods to add to the script-security whitelist for this plugin to work.
*
* @throws IOException
*/
public MiscWhitelist() throws IOException {
super(new StaticWhitelist(
"method java.util.Map$Entry getKey",
"method java.util.Map$Entry getValue"
));
}
}
}
135 changes: 135 additions & 0 deletions src/main/resources/SimpleBuild.groovy
@@ -0,0 +1,135 @@

// See https://github.com/jenkinsci/workflow-plugin/tree/master/cps-global-lib#defining-global-functions

/* sample with all the things turned on:
<code>
simpleBuild {
machine = "hi-speed"
docker = "java:1.9"
env = [
FOO : 42,
BAR : "YASS"
]
git_repo = "https://github.com/cloudbeers/PR-demo"
before_script = "echo before"
script = 'echo after $FOO'
after_script = 'echo done now'
notifications = [
email : "mneale@cloudbees.com"
]
}
</code>
*/


// The call(body) method in any file in workflowLibs.git/vars is exposed as a
// method with the same name as the file.
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

/** Run the build scripts */

try {
if (config.docker_image != null) {
runViaDocker(config)
} else {
runViaLabel(config)
}
} catch (Exception rethrow) {
failureDetail = failureDetail(rethrow)
sendMail(config, "FAILURE: Pipeline '${env.JOB_NAME}' (${env.BUILD_NUMBER}) failed!",
"Your job failed, please review it ${env.BUILD_URL}.\n\n${failureDetail}")
throw rethrow
}

/** conditionally notify - maybe wih a catch */
sendMail(config, "Pipeline '${env.JOB_NAME}' (${env.BUILD_NUMBER}) succeeded.",
"Be happy. Pipeline '${env.JOB_NAME}' (${env.BUILD_NUMBER}) succeeded.")


}

def sendMail(config, mailSubject, message) {
/*
* We have to build a primitive list up so we can use simple iteration
* so that things can be serialized as per continuation passing style
*/
emailList = []
if (config.notifications != null) {
for ( e in config.notifications ) {
if (e.getKey() == "email") {
emailList.add(e.getValue());
}
}
}

for (i = 0; i < emailList.size(); i++) {
mail body: message, subject: mailSubject, to: emailList[i]
}
}


/** Execute the scripts on the appropriate label node */
def runViaLabel(config) {
node(config.machine) {runScripts(config)}
}

def runViaDocker(config) {
node(config.machine) {
docker.image(config.docker_image).inside {
runScripts(config)
}
}
}


/** Run the before/script combination */
def runScripts(config) {
envList = []
for ( e in config.env ) {
envList.add("${e.getKey()}=${e.getValue()}")
}
withEnv(envList) {

/* checkout the codes */
if (config.git_repo == null) {
checkout scm
} else {
git config.git_repo
}

/* run the basic build steps */
if (config.before_script != null) {
sh config.before_script
}
sh config.script
if (config.after_script != null) {
sh config.after_script
}


}
}

/**
* Read the detail from the exception to be used in the failure message
* https://issues.jenkins-ci.org/browse/JENKINS-28119 will give better options.
*/
def failureDetail(exception) {
/* not allowed to access StringWriter
def w = new StringWriter()
exception.printStackTrace(new PrintWriter(w))
return w.toString();
*/
return exception.toString()
}
26 changes: 26 additions & 0 deletions src/main/resources/index.jelly
@@ -0,0 +1,26 @@
<!--
The MIT License
Copyright (c) 2015 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.
-->
<?jelly escape-by-default='true'?>
<!--
This view is used to render the installed plugins page.
-->
<div>
This provides simple build DSLs for Jenkins Pipelines.
</div>

0 comments on commit 9ddd122

Please sign in to comment.