Skip to content

Commit 4e3b1e5

Browse files
authored
Merge pull request #243 from jglick/shell
Simplify `Docker.shell`
2 parents 129a2d7 + 2bef7d5 commit 4e3b1e5

File tree

1 file changed

+11
-11
lines changed
  • src/main/resources/org/jenkinsci/plugins/docker/workflow

1 file changed

+11
-11
lines changed

src/main/resources/org/jenkinsci/plugins/docker/workflow/Docker.groovy

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class Docker implements Serializable {
7575
new Image(this, id)
7676
}
7777

78-
String shell(boolean isUnix) {
79-
isUnix ? "sh" : "bat"
78+
private Object shell(boolean isUnix, Object args) {
79+
isUnix ? script.sh(args) : script.bat(args)
8080
}
8181

8282
String asEnv(boolean isUnix, String var) {
@@ -89,7 +89,7 @@ class Docker implements Serializable {
8989
def isUnix = script.isUnix()
9090
def commandLine = 'docker build -t "' + asEnv(isUnix, 'JD_IMAGE') + '" ' + args
9191
script.withEnv(["JD_IMAGE=${image}"]) {
92-
script."${shell(isUnix)}" commandLine
92+
shell(isUnix, commandLine)
9393
}
9494
this.image(image)
9595
}
@@ -126,11 +126,11 @@ class Docker implements Serializable {
126126
def toRun = imageName()
127127
def isUnix = docker.script.isUnix()
128128
docker.script.withEnv(["JD_ID=${id}", "JD_TO_RUN=${toRun}"]) {
129-
if (toRun != id && docker.script."${docker.shell(isUnix)}"(script: 'docker inspect -f . "' + docker.asEnv(isUnix, 'JD_ID') + '"', returnStatus: true) == 0) {
129+
if (toRun != id && docker.shell(isUnix, [script: 'docker inspect -f . "' + docker.asEnv(isUnix, 'JD_ID') + '"', returnStatus: true]) == 0) {
130130
// Can run it without registry prefix, because it was locally built.
131131
toRun = id
132132
} else {
133-
if (docker.script."${docker.shell(isUnix)}"(script: 'docker inspect -f . "' + docker.asEnv(isUnix, 'JD_TO_RUN') + '"', returnStatus: true) != 0) {
133+
if (docker.shell(isUnix, [script: 'docker inspect -f . "' + docker.asEnv(isUnix, 'JD_TO_RUN') + '"', returnStatus: true]) != 0) {
134134
// Not yet present locally.
135135
// withDockerContainer requires the image to be available locally, since its start phase is not a durable task.
136136
pull()
@@ -148,15 +148,15 @@ class Docker implements Serializable {
148148
def toPull = imageName()
149149
def isUnix = docker.script.isUnix()
150150
docker.script.withEnv(["JD_TO_PULL=${toPull}"]) {
151-
docker.script."${docker.shell(isUnix)}" 'docker pull "' + docker.asEnv(isUnix, 'JD_TO_PULL') + '"'
151+
docker.shell(isUnix, 'docker pull "' + docker.asEnv(isUnix, 'JD_TO_PULL') + '"')
152152
}
153153
}
154154
}
155155

156156
public Container run(String args = '', String command = "") {
157157
docker.node {
158158
def isUnix = docker.script.isUnix()
159-
def container = docker.script."${docker.shell(isUnix)}"(script: "docker run -d${args != '' ? ' ' + args : ''} ${id}${command != '' ? ' ' + command : ''}", returnStdout: true).trim()
159+
def container = docker.shell(isUnix, [script: "docker run -d${args != '' ? ' ' + args : ''} ${id}${command != '' ? ' ' + command : ''}", returnStdout: true]).trim()
160160
new Container(docker, container, isUnix)
161161
}
162162
}
@@ -177,7 +177,7 @@ class Docker implements Serializable {
177177
def taggedImageName = toQualifiedImageName(parsedId.userAndRepo + ':' + tagName)
178178
def isUnix = docker.script.isUnix()
179179
docker.script.withEnv(["JD_ID=${id}", "JD_TAGGED_IMAGE_NAME=${taggedImageName}"]) {
180-
docker.script."${docker.shell(isUnix)}" 'docker tag "' + docker.asEnv(isUnix, 'JD_ID') + '" "' + docker.asEnv(isUnix, 'JD_TAGGED_IMAGE_NAME') + '"'
180+
docker.shell(isUnix, 'docker tag "' + docker.asEnv(isUnix, 'JD_ID') + '" "' + docker.asEnv(isUnix, 'JD_TAGGED_IMAGE_NAME') + '"')
181181
}
182182
return taggedImageName;
183183
}
@@ -190,7 +190,7 @@ class Docker implements Serializable {
190190
def taggedImageName = tag(tagName, force)
191191
def isUnix = docker.script.isUnix()
192192
docker.script.withEnv(["JD_TAGGED_IMAGE_NAME=${taggedImageName}"]) {
193-
docker.script."${docker.shell(isUnix)}" 'docker push "' + docker.asEnv(isUnix, 'JD_TAGGED_IMAGE_NAME') + '"'
193+
docker.shell(isUnix, 'docker push "' + docker.asEnv(isUnix, 'JD_TAGGED_IMAGE_NAME') + '"')
194194
}
195195
}
196196
}
@@ -211,13 +211,13 @@ class Docker implements Serializable {
211211

212212
public void stop() {
213213
docker.script.withEnv(["JD_ID=${id}"]) {
214-
docker.script."${docker.shell(isUnix)}" 'docker stop "' + docker.asEnv(isUnix,'JD_ID') + '" && docker rm -f --volumes "' + docker.asEnv(isUnix, 'JD_ID') + '"'
214+
docker.shell(isUnix, 'docker stop "' + docker.asEnv(isUnix,'JD_ID') + '" && docker rm -f --volumes "' + docker.asEnv(isUnix, 'JD_ID') + '"')
215215
}
216216
}
217217

218218
public String port(int port) {
219219
docker.script.withEnv(["JD_ID=${id}", "JD_PORT=${port}"]) {
220-
docker.script."${docker.shell(isUnix)}"(script: 'docker port "' + docker.asEnv(isUnix, 'JD_ID') + '" "' + docker.asEnv(isUnix, 'JD_PORT') + '"', returnStdout: true).trim()
220+
docker.shell(isUnix, [script: 'docker port "' + docker.asEnv(isUnix, 'JD_ID') + '" "' + docker.asEnv(isUnix, 'JD_PORT') + '"', returnStdout: true]).trim()
221221
}
222222
}
223223
}

0 commit comments

Comments
 (0)