Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from abayer/jenkins-39216
[JENKINS-39216] Add "dockerfile" agent backend
- Loading branch information
Showing
8 changed files
with
262 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...g/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineFromDockerfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
|
||
public class DockerPipelineFromDockerfile extends DeclarativeAgent { | ||
private String label; | ||
private Object dockerfile; | ||
private String dockerArgs = ""; | ||
|
||
@DataBoundConstructor | ||
public DockerPipelineFromDockerfile(@Nonnull Object dockerfile) { | ||
this.dockerfile = dockerfile; | ||
} | ||
|
||
public @CheckForNull String getLabel() { | ||
return label; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setLabel(String label) { | ||
this.label = label; | ||
} | ||
|
||
public @CheckForNull String getDockerArgs() { | ||
return dockerArgs; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setDockerArgs(String dockerArgs) { | ||
this.dockerArgs = dockerArgs; | ||
} | ||
|
||
public @Nonnull Object getDockerfile() { | ||
return dockerfile; | ||
} | ||
|
||
public String getDockerfileAsString() { | ||
if (dockerfile instanceof String) { | ||
return (String)dockerfile; | ||
} else { | ||
return "Dockerfile"; | ||
} | ||
} | ||
|
||
@Extension(ordinal = 999) @Symbol("dockerfile") | ||
public static class DescriptorImpl extends DeclarativeAgentDescriptor { | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...sci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineFromDockerfileScript.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(declarativeAgent.getDockerfileAsString())) | ||
def imgName = "${hash}" | ||
def img = script.getProperty("docker").build(imgName, "-f ${declarativeAgent.getDockerfileAsString()} .") | ||
img.inside(declarativeAgent.dockerArgs, { | ||
body.call() | ||
}) | ||
} catch (FileNotFoundException f) { | ||
script.error("No Dockerfile found at root of repository - failing.") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
pipeline-model-definition/src/test/resources/fromAlternateDockerfile.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
pipeline { | ||
agent dockerfile:"Dockerfile.alternate", dockerArgs:"-v /tmp:/tmp -p 8000:8000" | ||
stages { | ||
stage("foo") { | ||
steps { | ||
sh 'cat /hi-there' | ||
sh 'echo "The answer is 42"' | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
38 changes: 38 additions & 0 deletions
38
pipeline-model-definition/src/test/resources/fromDockerfile.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
pipeline { | ||
agent dockerfile:true, dockerArgs:"-v /tmp:/tmp -p 8000:8000" | ||
stages { | ||
stage("foo") { | ||
steps { | ||
sh 'cat /hi-there' | ||
sh 'echo "The answer is 42"' | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|