Skip to content

Commit 16e84da

Browse files
authored
Merge pull request #341 from jglick/upgrade
Compatibility for `DockerPipelineScript`
2 parents ab371bd + caf9583 commit 16e84da

File tree

5 files changed

+232
-1
lines changed

5 files changed

+232
-1
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
<jenkins.baseline>2.479</jenkins.baseline>
3535
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
3636
<!-- TODO until in BOM -->
37-
<pipeline-model-definition.version>2.2234.v4a_b_13b_8cd590</pipeline-model-definition.version>
37+
<pipeline-model-definition.version>2.2247.va_423189a_7dff</pipeline-model-definition.version>
38+
<useBeta>true</useBeta>
3839
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
3940
</properties>
4041
<repositories>

src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/AbstractDockerAgent.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
import edu.umd.cs.findbugs.annotations.CheckForNull;
3030
import edu.umd.cs.findbugs.annotations.Nullable;
3131
import hudson.Extension;
32+
import java.net.URL;
33+
import java.util.Set;
3234
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
3335
import org.jenkinsci.plugins.pipeline.modeldefinition.options.DeclarativeOption;
36+
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.CompatibilityLoader;
3437
import org.jenkinsci.plugins.workflow.cps.GroovySourceFileAllowlist;
3538
import org.kohsuke.stapler.DataBoundSetter;
3639

@@ -139,4 +142,18 @@ public boolean isAllowed(String groovyResourceUrl) {
139142
}
140143
}
141144

145+
@Extension(optional = true) public static final class Compat implements CompatibilityLoader {
146+
private static final Set<String> CLASSES = Set.of(
147+
"org.jenkinsci.plugins.docker.workflow.declarative.AbstractDockerPipelineScript",
148+
"org.jenkinsci.plugins.docker.workflow.declarative.DockerPipelineScript",
149+
"org.jenkinsci.plugins.docker.workflow.declarative.DockerPipelineFromDockerfileScript");
150+
@Override public URL loadGroovySource(String clazz) {
151+
if (CLASSES.contains(clazz)) {
152+
return AbstractDockerAgent.class.getResource("compat/" + clazz.replaceFirst(".+[.]", "") + ".groovy");
153+
} else {
154+
return null;
155+
}
156+
}
157+
}
158+
142159
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2017, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
26+
package org.jenkinsci.plugins.docker.workflow.declarative
27+
28+
import hudson.FilePath
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
30+
import org.jenkinsci.plugins.workflow.cps.CpsScript
31+
32+
abstract class AbstractDockerPipelineScript<A extends AbstractDockerAgent<A>> extends DeclarativeAgentScript<A> {
33+
34+
AbstractDockerPipelineScript(CpsScript s, A a) {
35+
super(s, a)
36+
}
37+
38+
@Override
39+
Closure run(Closure body) {
40+
if (describable.reuseNode && script.getContext(FilePath.class) != null) {
41+
return {
42+
configureRegistry(body).call()
43+
}
44+
} else if (describable.containerPerStageRoot) {
45+
return DeclarativeDockerUtils.getLabelScript(describable, script).run {
46+
body.call()
47+
}
48+
} else {
49+
return DeclarativeDockerUtils.getLabelScript(describable, script).run {
50+
configureRegistry(body).call()
51+
}
52+
}
53+
}
54+
55+
protected Closure configureRegistry(Closure body) {
56+
return {
57+
DeclarativeDockerUtils.DockerRegistry registry = DeclarativeDockerUtils.DockerRegistry.build(describable.registryUrl, describable.registryCredentialsId)
58+
if (registry.hasData()) {
59+
script.getProperty("docker").withRegistry(registry.registry, registry.credential) {
60+
runImage(body).call()
61+
}
62+
} else {
63+
runImage(body).call()
64+
}
65+
}
66+
}
67+
68+
protected abstract Closure runImage(Closure body)
69+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2016, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
26+
package org.jenkinsci.plugins.docker.workflow.declarative
27+
28+
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
30+
import org.jenkinsci.plugins.workflow.cps.CpsScript
31+
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
32+
33+
class DockerPipelineFromDockerfileScript extends AbstractDockerPipelineScript<DockerPipelineFromDockerfile> {
34+
35+
DockerPipelineFromDockerfileScript(CpsScript s, DockerPipelineFromDockerfile a) {
36+
super(s, a)
37+
}
38+
39+
@Override
40+
Closure runImage(Closure body) {
41+
return {
42+
def img = null
43+
if (!Utils.withinAStage()) {
44+
script.stage(SyntheticStageNames.agentSetup()) {
45+
try {
46+
img = buildImage().call()
47+
} catch (Exception e) {
48+
Utils.markStageFailedAndContinued(SyntheticStageNames.agentSetup())
49+
throw e
50+
}
51+
}
52+
} else {
53+
img = buildImage().call()
54+
}
55+
if (img != null) {
56+
img.inside(describable.args, {
57+
body.call()
58+
})
59+
}
60+
}
61+
}
62+
63+
private Closure buildImage() {
64+
return {
65+
boolean isUnix = script.isUnix()
66+
def dockerfilePath = describable.getDockerfilePath(isUnix)
67+
try {
68+
RunWrapper runWrapper = (RunWrapper)script.getProperty("currentBuild")
69+
def additionalBuildArgs = describable.getAdditionalBuildArgs() ? " ${describable.additionalBuildArgs}" : ""
70+
def hash = Utils.stringToSHA1("${runWrapper.fullProjectName}\n${script.readFile("${dockerfilePath}")}\n${additionalBuildArgs}")
71+
def imgName = "${hash}"
72+
def commandLine = "docker build -t ${imgName}${additionalBuildArgs} -f \"${dockerfilePath}\" \"${describable.getActualDir()}\""
73+
if (isUnix)
74+
script.sh commandLine
75+
else
76+
script.bat commandLine
77+
78+
return script.getProperty("docker").image(imgName)
79+
} catch (FileNotFoundException f) {
80+
script.error("No Dockerfile found at ${dockerfilePath} in repository - failing.")
81+
return null
82+
}
83+
}
84+
}
85+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2016, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
26+
package org.jenkinsci.plugins.docker.workflow.declarative
27+
28+
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
30+
import org.jenkinsci.plugins.workflow.cps.CpsScript
31+
32+
class DockerPipelineScript extends AbstractDockerPipelineScript<DockerPipeline> {
33+
34+
DockerPipelineScript(CpsScript s, DockerPipeline a) {
35+
super(s, a)
36+
}
37+
38+
@Override
39+
Closure runImage(Closure body) {
40+
return {
41+
if (!Utils.withinAStage() && describable.alwaysPull) {
42+
script.stage(SyntheticStageNames.agentSetup()) {
43+
try {
44+
script.getProperty("docker").image(describable.image).pull()
45+
} catch (Exception e) {
46+
Utils.markStageFailedAndContinued(SyntheticStageNames.agentSetup())
47+
throw e
48+
}
49+
}
50+
}
51+
if (Utils.withinAStage() && describable.alwaysPull) {
52+
script.getProperty("docker").image(describable.image).pull()
53+
}
54+
script.getProperty("docker").image(describable.image).inside(describable.args, {
55+
body.call()
56+
})
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)