Skip to content

Commit e20ab32

Browse files
authored
Merge pull request #244 from viceice/fix/windows-env-vars
fix: use correct syntax for environment variable
2 parents ef5c63b + b4d40ba commit e20ab32

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

  • src/main/resources/org/jenkinsci/plugins/docker/workflow

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

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

78-
String shell() {
79-
node {
80-
script.isUnix() ? "sh" : "bat"
81-
}
78+
String shell(boolean isUnix) {
79+
isUnix ? "sh" : "bat"
80+
}
81+
82+
String asEnv(boolean isUnix, String var) {
83+
isUnix ? "\$${var}" : "%${var}%"
8284
}
8385

8486
public Image build(String image, String args = '.') {
8587
check(image)
8688
node {
87-
def commandLine = 'docker build -t "$JD_IMAGE" ' + args
89+
def isUnix = script.isUnix()
90+
def commandLine = 'docker build -t "' + asEnv(isUnix, 'JD_IMAGE') + '" ' + args
8891
script.withEnv(["JD_IMAGE=${image}"]) {
89-
script."${shell()}" commandLine
92+
script."${shell(isUnix)}" commandLine
9093
}
9194
this.image(image)
9295
}
@@ -121,12 +124,13 @@ class Docker implements Serializable {
121124
public <V> V inside(String args = '', Closure<V> body) {
122125
docker.node {
123126
def toRun = imageName()
127+
def isUnix = docker.script.isUnix()
124128
docker.script.withEnv(["JD_ID=${id}", "JD_TO_RUN=${toRun}"]) {
125-
if (toRun != id && docker.script."${docker.shell()}"(script: 'docker inspect -f . "$JD_ID"', returnStatus: true) == 0) {
129+
if (toRun != id && docker.script."${docker.shell(isUnix)}"(script: 'docker inspect -f . "' + docker.asEnv(isUnix, 'JD_ID') + '"', returnStatus: true) == 0) {
126130
// Can run it without registry prefix, because it was locally built.
127131
toRun = id
128132
} else {
129-
if (docker.script."${docker.shell()}"(script: 'docker inspect -f . "$JD_TO_RUN"', returnStatus: true) != 0) {
133+
if (docker.script."${docker.shell(isUnix)}"(script: 'docker inspect -f . "' + docker.asEnv(isUnix, 'JD_TO_RUN') + '"', returnStatus: true) != 0) {
130134
// Not yet present locally.
131135
// withDockerContainer requires the image to be available locally, since its start phase is not a durable task.
132136
pull()
@@ -142,16 +146,18 @@ class Docker implements Serializable {
142146
public void pull() {
143147
docker.node {
144148
def toPull = imageName()
149+
def isUnix = docker.script.isUnix()
145150
docker.script.withEnv(["JD_TO_PULL=${toPull}"]) {
146-
docker.script."${docker.shell()}" 'docker pull "$JD_TO_PULL"'
151+
docker.script."${docker.shell(isUnix)}" 'docker pull "' + docker.asEnv(isUnix, 'JD_TO_PULL') + '"'
147152
}
148153
}
149154
}
150155

151156
public Container run(String args = '', String command = "") {
152157
docker.node {
153-
def container = docker.script."${docker.shell()}"(script: "docker run -d${args != '' ? ' ' + args : ''} ${id}${command != '' ? ' ' + command : ''}", returnStdout: true).trim()
154-
new Container(docker, container)
158+
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()
160+
new Container(docker, container, isUnix)
155161
}
156162
}
157163

@@ -169,8 +175,9 @@ class Docker implements Serializable {
169175
public void tag(String tagName = parsedId.tag, boolean force = true) {
170176
docker.node {
171177
def taggedImageName = toQualifiedImageName(parsedId.userAndRepo + ':' + tagName)
178+
def isUnix = docker.script.isUnix()
172179
docker.script.withEnv(["JD_ID=${id}", "JD_TAGGED_IMAGE_NAME=${taggedImageName}"]) {
173-
docker.script."${docker.shell()}" 'docker tag "$JD_ID" "$JD_TAGGED_IMAGE_NAME"'
180+
docker.script."${docker.shell(isUnix)}" 'docker tag "' + docker.asEnv(isUnix, 'JD_ID') + '" "' + docker.asEnv(isUnix, 'JD_TAGGED_IMAGE_NAME') + '"'
174181
}
175182
return taggedImageName;
176183
}
@@ -181,8 +188,9 @@ class Docker implements Serializable {
181188
// The image may have already been tagged, so the tagging may be a no-op.
182189
// That's ok since tagging is cheap.
183190
def taggedImageName = tag(tagName, force)
191+
def isUnix = docker.script.isUnix()
184192
docker.script.withEnv(["JD_TAGGED_IMAGE_NAME=${taggedImageName}"]) {
185-
docker.script."${docker.shell()}" 'docker push "$JD_TAGGED_IMAGE_NAME"'
193+
docker.script."${docker.shell(isUnix)}" 'docker push "' + docker.asEnv(isUnix, 'JD_TAGGED_IMAGE_NAME') + '"'
186194
}
187195
}
188196
}
@@ -192,22 +200,24 @@ class Docker implements Serializable {
192200
public static class Container implements Serializable {
193201

194202
private final Docker docker;
203+
private final boolean isUnix;
195204
public final String id;
196205

197-
private Container(Docker docker, String id) {
206+
private Container(Docker docker, String id, boolean isUnix) {
198207
this.docker = docker
199208
this.id = id
209+
this.isUnix = isUnix;
200210
}
201211

202212
public void stop() {
203213
docker.script.withEnv(["JD_ID=${id}"]) {
204-
docker.script."${docker.shell()}" 'docker stop "$JD_ID" && docker rm -f "$JD_ID"'
214+
docker.script."${docker.shell(isUnix)}" 'docker stop "' + docker.asEnv(isUnix,'JD_ID') + '" && docker rm -f "' + docker.asEnv(isUnix, 'JD_ID') + '"'
205215
}
206216
}
207217

208218
public String port(int port) {
209219
docker.script.withEnv(["JD_ID=${id}", "JD_PORT=${port}"]) {
210-
docker.script."${docker.shell()}"(script: 'docker port "$JD_ID" "$JD_PORT"', returnStdout: true).trim()
220+
docker.script."${docker.shell(isUnix)}"(script: 'docker port "' + docker.asEnv(isUnix, 'JD_ID') + '" "' + docker.asEnv(isUnix, 'JD_PORT') + '"', returnStdout: true).trim()
211221
}
212222
}
213223
}

0 commit comments

Comments
 (0)