forked from jenkinsci/docker-workflow-plugin
/
Docker.groovy
192 lines (163 loc) · 6.8 KB
/
Docker.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* 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.
*/
package org.jenkinsci.plugins.docker.workflow
class Docker implements Serializable {
private org.jenkinsci.plugins.workflow.cps.CpsScript script
public Docker(org.jenkinsci.plugins.workflow.cps.CpsScript script) {
this.script = script
}
public <V> V withRegistry(String url, String credentialsId = null, Closure<V> body) {
node {
script.withEnv(["DOCKER_REGISTRY_URL=${url}"]) {
script.withDockerRegistry([url: url, credentialsId: credentialsId]) {
body()
}
}
}
}
public <V> V withServer(String uri, String credentialsId = null, Closure<V> body) {
node {
script.withDockerServer([uri: uri, credentialsId: credentialsId]) {
body()
}
}
}
public <V> V withTool(String toolName, Closure<V> body) {
node {
script.withEnv(["PATH=${script.tool name: toolName, type: 'org.jenkinsci.plugins.docker.commons.tools.DockerTool'}/bin:${script.env.PATH}", "DOCKER_TOOL_NAME=${toolName}"]) {
body()
}
}
}
private <V> V node(Closure<V> body) {
if (script.env.NODE_NAME != null) {
// Already inside a node block.
body()
} else {
script.node {
body()
}
}
}
public Image image(String id) {
new Image(this, id)
}
public Image build(String image, String args = '.') {
node {
def parsedArgs = args.split(/ (?=([^"']*["'][^"']*["'])*[^"']*$)/)
def dir = parsedArgs[-1] ?: '.'
// Detect custom Dockerfile:
def dockerfile = "${dir}/Dockerfile"
for (int i=0; i<parsedArgs.length; i++) {
if (parsedArgs[i] == '-f' && i < (parsedArgs.length - 1)) {
dockerfile = parsedArgs[i+1]
break
}
}
script.sh "docker build -t ${image} ${args}"
script.dockerFingerprintFrom dockerfile: dockerfile, image: image, toolName: script.env.DOCKER_TOOL_NAME
this.image(image)
}
}
public static class Image implements Serializable {
private final Docker docker;
public final String id;
private ImageNameTokens parsedId;
private Image(Docker docker, String id) {
this.docker = docker
this.id = id
this.parsedId = new ImageNameTokens(id)
}
private String toQualifiedImageName(String imageName) {
return new org.jenkinsci.plugins.docker.commons.credentials.DockerRegistryEndpoint(docker.script.env.DOCKER_REGISTRY_URL, null).imageName(imageName)
}
public String imageName() {
return toQualifiedImageName(id)
}
public <V> V inside(String args = '', Closure<V> body) {
docker.node {
if (docker.script.sh(script: "docker inspect -f . ${id}", returnStatus: true) != 0) {
// Not yet present locally.
// withDockerContainer requires the image to be available locally, since its start phase is not a durable task.
pull()
}
docker.script.withDockerContainer(image: id, args: args, toolName: docker.script.env.DOCKER_TOOL_NAME) {
body()
}
}
}
public void pull() {
docker.node {
docker.script.sh "docker pull ${imageName()}"
}
}
public Container run(String args = '', String command = "") {
docker.node {
def container = docker.script.sh(script: "docker run -d${args != '' ? ' ' + args : ''} ${id}${command != '' ? ' ' + command : ''}", returnStdout: true).trim()
docker.script.dockerFingerprintRun containerId: container, toolName: docker.script.env.DOCKER_TOOL_NAME
new Container(docker, container)
}
}
public <V> V withRun(String args = '', String command = "", Closure<V> body) {
docker.node {
Container c = run(args, command)
try {
body.call(c)
} finally {
c.stop()
}
}
}
public void tag(String tagName = parsedId.tag, boolean force = true) {
docker.node {
def taggedImageName = toQualifiedImageName(parsedId.userAndRepo + ':' + tagName)
// TODO as of 1.10.0 --force is deprecated; for 1.12+ do not try it even once
docker.script.sh "docker tag --force=${force} ${id} ${taggedImageName} || docker tag ${id} ${taggedImageName}"
return taggedImageName;
}
}
public void push(String tagName = parsedId.tag, boolean force = true) {
docker.node {
// The image may have already been tagged, so the tagging may be a no-op.
// That's ok since tagging is cheap.
def taggedImageName = tag(tagName, force)
docker.script.sh "docker push ${taggedImageName}"
}
}
}
public static class Container implements Serializable {
private final Docker docker;
public final String id;
private Container(Docker docker, String id) {
this.docker = docker
this.id = id
}
public void stop() {
docker.script.sh "docker stop ${id} && docker rm -f ${id}"
}
public String port(int port) {
docker.script.sh(script: "docker port ${id} ${port}", returnStdout: true).trim()
}
}
}