4646public 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 }
0 commit comments