@@ -41,11 +41,15 @@ type Job struct {
4141 // of the form name[:tag].
4242 ImageName * string
4343
44+ // BatchSize restricts the number of concurrent builds.
45+ // Zero indicates no restriction.
46+ BatchSize int
47+
4448 // Directory denotes the Docker build directory (defaults behavior assumes the current working directory).
4549 Directory string
4650}
4751
48- // NewJob generates a default Job.
52+ // NewJob initializes tug and generates a default Job.
4953func NewJob () (* Job , error ) {
5054 platforms , err := AvailablePlatforms ()
5155
@@ -62,8 +66,8 @@ func NewJob() (*Job, error) {
6266 return & Job {Builder : TugBuilderName , Platforms : platforms , Directory : cwd }, nil
6367}
6468
65- // Run executes a Job .
66- func (o Job ) Run () error {
69+ // runBatch executes a batch of platforms .
70+ func (o Job ) runBatch () error {
6771 cmd := exec .Command ("docker" )
6872 cmd .Env = os .Environ ()
6973 cmd .Stderr = os .Stderr
@@ -79,6 +83,39 @@ func (o Job) Run() error {
7983
8084 var platformPairs []string
8185
86+ for _ , platform := range o .Platforms {
87+ platformPairs = append (platformPairs , platform .Format ())
88+ }
89+
90+ cmd .Args = append (cmd .Args , "--platform" )
91+
92+ if o .LoadPlatform == nil {
93+ cmd .Args = append (cmd .Args , strings .Join (platformPairs , "," ))
94+ } else {
95+ cmd .Args = append (cmd .Args , * o .LoadPlatform )
96+ cmd .Args = append (cmd .Args , "--load" )
97+ }
98+
99+ if o .Push {
100+ cmd .Args = append (cmd .Args , "--push" )
101+ }
102+
103+ cmd .Args = append (cmd .Args , "-t" )
104+ cmd .Args = append (cmd .Args , * o .ImageName )
105+
106+ cmd .Args = append (cmd .Args , o .Directory )
107+
108+ if o .Debug {
109+ log .Printf ("Command: %v\n " , cmd )
110+ }
111+
112+ return cmd .Run ()
113+ }
114+
115+ // Run schedules builds.
116+ func (o Job ) Run () error {
117+ var platforms []Platform
118+
82119 for _ , platform := range o .Platforms {
83120 var excludedOs bool
84121
@@ -106,30 +143,35 @@ func (o Job) Run() error {
106143 continue
107144 }
108145
109- platformPairs = append (platformPairs , platform . Format () )
146+ platforms = append (platforms , platform )
110147 }
111148
112- cmd . Args = append ( cmd . Args , "--platform" )
149+ o . Platforms = platforms
113150
114- if o .LoadPlatform == nil {
115- cmd .Args = append (cmd .Args , strings .Join (platformPairs , "," ))
116- } else {
117- cmd .Args = append (cmd .Args , * o .LoadPlatform )
118- cmd .Args = append (cmd .Args , "--load" )
119- }
151+ batchSize := o .BatchSize
120152
121- if o . Push {
122- cmd . Args = append ( cmd . Args , "--push" )
153+ if batchSize == 0 {
154+ return o . runBatch ( )
123155 }
124156
125- cmd .Args = append (cmd .Args , "-t" )
126- cmd .Args = append (cmd .Args , * o .ImageName )
157+ var platformGroups [][]Platform
127158
128- cmd .Args = append (cmd .Args , o .Directory )
159+ for len (o .Platforms ) != 0 {
160+ if len (o .Platforms ) < batchSize {
161+ batchSize = len (o .Platforms )
162+ }
129163
130- if o . Debug {
131- log . Printf ( "Command: %v \n " , cmd )
164+ platformGroups = append ( platformGroups , o . Platforms [ 0 : batchSize ])
165+ o . Platforms = o . Platforms [ batchSize :]
132166 }
133167
134- return cmd .Run ()
168+ for _ , platformGroup := range platformGroups {
169+ o .Platforms = platformGroup
170+
171+ if err := o .runBatch (); err != nil {
172+ return err
173+ }
174+ }
175+
176+ return nil
135177}
0 commit comments