Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit 5cdbfb6

Browse files
authored
Merge pull request #40 from dillon-cullinan/master
[REVIEW] FEA Add support for retrying docker pulls on failure
2 parents dc46478 + 7af5a07 commit 5cdbfb6

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/main/java/com/gpuopenanalytics/jenkins/remotedocker/job/DockerImageConfiguration.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,31 @@
4646
public class DockerImageConfiguration extends AbstractDockerConfiguration {
4747

4848
private final String image;
49+
private final String maxRetries;
4950
private final boolean forcePull;
5051

5152
@DataBoundConstructor
5253
public DockerImageConfiguration(List<ConfigItem> configItemList,
5354
List<VolumeConfiguration> volumes,
5455
String image,
55-
boolean forcePull) {
56+
boolean forcePull,
57+
String maxRetries) {
5658
super(configItemList, volumes);
5759
this.image = image;
5860
this.forcePull = forcePull;
61+
this.maxRetries = maxRetries;
62+
}
63+
64+
public boolean isForcePull() {
65+
return forcePull;
5966
}
6067

6168
public String getImage() {
6269
return image;
6370
}
6471

65-
public boolean isForcePull() {
66-
return forcePull;
72+
public String getRetries() {
73+
return maxRetries;
6774
}
6875

6976
@Override
@@ -78,6 +85,12 @@ public void validate() throws Descriptor.FormException {
7885
for (VolumeConfiguration volume : getVolumes()) {
7986
volume.validate();
8087
}
88+
if (StringUtils.isEmpty(maxRetries) && isForcePull()) {
89+
throw new Descriptor.FormException("Max Retries cannot be empty", "maxRetries");
90+
}
91+
if (!StringUtils.isNumeric(maxRetries)) {
92+
throw new Descriptor.FormException("Max Retries must be an integer", "maxRetries");
93+
}
8194
}
8295

8396
@Override
@@ -86,11 +99,29 @@ public void setupImage(AbstractDockerLauncher launcher,
8699
if (isForcePull()) {
87100
ArgumentListBuilder args = new ArgumentListBuilder();
88101
String image = Utils.resolveVariables(launcher, getImage());
102+
String maxRetries = Utils.resolveVariables(launcher, getRetries());
89103
args.add("docker", "pull", image);
104+
int numRetries = Integer.parseInt(maxRetries);
105+
int retries = 0;
106+
int status;
107+
90108
Launcher.ProcStarter proc = launcher.executeCommand(args)
91-
.stderr(launcher.getListener().getLogger())
92-
.stdout(launcher.getListener());
93-
int status = proc.join();
109+
.stderr(launcher.getListener().getLogger())
110+
.stdout(launcher.getListener());
111+
status = proc.join();
112+
113+
while (retries < numRetries && status != 0) {
114+
retries += 1;
115+
String message = "Docker pull failed, retry " + Integer.toString(retries) +
116+
" of " + maxRetries + "...";
117+
launcher.getListener().getLogger().println(message);
118+
119+
proc = launcher.executeCommand(args)
120+
.stderr(launcher.getListener().getLogger())
121+
.stdout(launcher.getListener());
122+
status = proc.join();
123+
}
124+
94125
if (status != 0) {
95126
throw new IOException("Could not pull image: " + image);
96127
}

src/main/resources/com/gpuopenanalytics/jenkins/remotedocker/job/DockerImageConfiguration/advanced-config.jelly

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
<j:jelly xmlns:j="jelly:core"
2727
xmlns:f="/lib/form">
2828

29-
<f:entry title="Force pull" field="forcePull">
30-
<f:checkbox/>
31-
</f:entry>
29+
<f:optionalBlock field="forcePull" title="Force Pull" inline="true" checked="${instance.forcePull}">
30+
<f:entry title="Max Retries" field="maxRetries">
31+
<f:textbox default="1"/>
32+
</f:entry>
33+
</f:optionalBlock>
3234

3335
</j:jelly>
3436

0 commit comments

Comments
 (0)