Skip to content

Commit

Permalink
[JENKINS-39216] First work on auto-building Dockerfiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed Oct 28, 2016
1 parent 0e9e778 commit f44f304
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
Expand Up @@ -32,6 +32,7 @@ import hudson.ExtensionList
import hudson.model.Describable
import hudson.model.Descriptor
import hudson.triggers.TriggerDescriptor
import org.apache.commons.codec.digest.DigestUtils
import org.jenkinsci.plugins.pipeline.modeldefinition.actions.ExecutionModelAction
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages
Expand Down Expand Up @@ -198,6 +199,11 @@ public class Utils {
r.addAction(new ExecutionModelAction(stages))
}

@Whitelisted
public static String stringToSHA1(String s) {
return DigestUtils.sha1Hex(s)
}

/**
* Creates and sets the loading for a cache of {@link Describable}s descending from the given descriptor type.
*
Expand Down
@@ -0,0 +1,96 @@
/*
* 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.pipeline.modeldefinition.agent.impl;

import hudson.Extension;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class DockerPipelineFromDockerfile extends DeclarativeAgent {
private String label;
private String dockerfile;
private String name;
private String dockerArgs = "";

@DataBoundConstructor
public DockerPipelineFromDockerfile(@Nonnull String name) {
this.name = name;
}

@Whitelisted
public @Nullable String getLabel() {
return label;
}

@DataBoundSetter
public void setLabel(String label) {
this.label = label;
}

@Whitelisted
public @Nullable String getDockerArgs() {
return dockerArgs;
}

@DataBoundSetter
public void setDockerArgs(String dockerArgs) {
this.dockerArgs = dockerArgs;
}

@Whitelisted
public @Nonnull String getName() {
return name;
}

@Whitelisted
public @Nullable String getDockerfile() {
return dockerfile;
}

@DataBoundSetter
public void setDockerfile(String dockerfile) {
this.dockerfile = dockerfile;
}

@Extension(ordinal = 999) @Symbol("dockerfile")
public static class DescriptorImpl extends DeclarativeAgentDescriptor {
@Override
public @Nonnull String getName() {
return "dockerfile";
}

public @Nonnull String getDeclarativeAgentScriptClass() {
return "org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.DockerPipelineFromDockerfileScript";
}

}
}
@@ -0,0 +1,60 @@
/*
* 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.pipeline.modeldefinition.agent.impl

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.workflow.cps.CpsScript

public class DockerPipelineFromDockerfileScript extends DeclarativeAgentScript {

public DockerPipelineFromDockerfileScript(CpsScript s, DeclarativeAgent a) {
super(s, a)
}

@Override
public Closure run(Closure body) {
String targetLabel = declarativeAgent.label
if (targetLabel == null) {
targetLabel = script.dockerLabel()?.trim()
}
LabelScript labelScript = (LabelScript) Label.DescriptorImpl.instanceForName("label", [label: targetLabel]).getScript(script)
return labelScript.run {
script.checkout script.scm
try {
def hash = Utils.stringToSHA1(script.readFile("Dockerfile"))
def imgName = "${declarativeAgent.name}-${hash}"
def img = script.getProperty("docker").build(imgName)
img.inside(declarativeAgent.dockerArgs, {
body.call()
})
} catch (FileNotFoundException f) {
script.error("No Dockerfile found at root of repository - failing.")
}
}
}
}

0 comments on commit f44f304

Please sign in to comment.