0
# Build Operations
1
2
This document covers OpenShift build and build configuration operations, including binary builds, webhook triggers, log streaming, and build management.
3
4
## Core Imports
5
6
```java { .api }
7
import io.fabric8.openshift.client.OpenShiftClient;
8
import io.fabric8.openshift.api.model.Build;
9
import io.fabric8.openshift.api.model.BuildList;
10
import io.fabric8.openshift.api.model.BuildConfig;
11
import io.fabric8.openshift.api.model.BuildConfigList;
12
import io.fabric8.openshift.api.model.BuildRequest;
13
import io.fabric8.openshift.client.dsl.BuildResource;
14
import io.fabric8.openshift.client.dsl.BuildConfigResource;
15
import io.fabric8.kubernetes.client.dsl.MixedOperation;
16
```
17
18
## Build Operations
19
20
### Basic Build Operations
21
22
```java { .api }
23
// List all builds in a namespace
24
BuildList builds = client.builds()
25
.inNamespace("my-project")
26
.list();
27
28
// Get a specific build
29
Build build = client.builds()
30
.inNamespace("my-project")
31
.withName("my-app-1")
32
.get();
33
34
// Delete a build
35
Boolean deleted = client.builds()
36
.inNamespace("my-project")
37
.withName("my-app-1")
38
.delete();
39
```
40
41
### Build Log Operations
42
43
```java { .api }
44
// Get build logs
45
String buildLog = client.builds()
46
.inNamespace("my-project")
47
.withName("my-app-1")
48
.getLog();
49
50
// Get logs with follow mode
51
String followLogs = client.builds()
52
.inNamespace("my-project")
53
.withName("my-app-1")
54
.getLog(true); // follow = true
55
56
// Get logs for a specific version
57
String versionLog = client.builds()
58
.inNamespace("my-project")
59
.withName("my-app-1")
60
.withVersion(2)
61
.getLog();
62
```
63
64
### Build Filtering and Listing
65
66
```java { .api }
67
// List builds with label selector
68
BuildList labeledBuilds = client.builds()
69
.inNamespace("my-project")
70
.withLabel("app", "my-application")
71
.list();
72
73
// List builds with field selector
74
BuildList completedBuilds = client.builds()
75
.inNamespace("my-project")
76
.withField("status.phase", "Complete")
77
.list();
78
```
79
80
## BuildConfig Operations
81
82
### Basic BuildConfig Operations
83
84
```java { .api }
85
// List all build configurations
86
BuildConfigList buildConfigs = client.buildConfigs()
87
.inNamespace("my-project")
88
.list();
89
90
// Get a specific build configuration
91
BuildConfig buildConfig = client.buildConfigs()
92
.inNamespace("my-project")
93
.withName("my-app")
94
.get();
95
96
// Create a build configuration
97
BuildConfig newBuildConfig = new BuildConfigBuilder()
98
.withNewMetadata()
99
.withName("my-new-app")
100
.withNamespace("my-project")
101
.endMetadata()
102
.withNewSpec()
103
// Build config spec details
104
.endSpec()
105
.build();
106
107
BuildConfig created = client.buildConfigs()
108
.inNamespace("my-project")
109
.create(newBuildConfig);
110
```
111
112
### Build Instantiation
113
114
```java { .api }
115
// Instantiate a build from a build config
116
Build instantiatedBuild = client.buildConfigs()
117
.inNamespace("my-project")
118
.withName("my-app")
119
.instantiate();
120
121
// Instantiate with build request
122
BuildRequest buildRequest = new BuildRequestBuilder()
123
.withNewMetadata()
124
.withName("my-app")
125
.endMetadata()
126
.withTriggeredBy(/* trigger causes */)
127
.build();
128
129
Build requestedBuild = client.buildConfigs()
130
.inNamespace("my-project")
131
.withName("my-app")
132
.instantiate(buildRequest);
133
```
134
135
### Binary Build Operations
136
137
```java { .api }
138
// Start binary build from directory
139
Build binaryBuild = client.buildConfigs()
140
.inNamespace("my-project")
141
.withName("my-app")
142
.instantiateBinary()
143
.fromDirectory(new File("/path/to/source"));
144
145
// Start binary build from file
146
Build fileBuild = client.buildConfigs()
147
.inNamespace("my-project")
148
.withName("my-app")
149
.instantiateBinary()
150
.fromFile(new File("/path/to/source.tar.gz"));
151
152
// Start binary build with input stream
153
Build streamBuild = client.buildConfigs()
154
.inNamespace("my-project")
155
.withName("my-app")
156
.instantiateBinary()
157
.fromInputStream(inputStream);
158
```
159
160
### Webhook Trigger Operations
161
162
```java { .api }
163
// Trigger build via webhook
164
Build webhookBuild = client.buildConfigs()
165
.inNamespace("my-project")
166
.withName("my-app")
167
.trigger()
168
.webhookTrigger("secret-here");
169
170
// Generic webhook trigger
171
Build genericBuild = client.buildConfigs()
172
.inNamespace("my-project")
173
.withName("my-app")
174
.trigger()
175
.generic("webhook-secret");
176
177
// GitHub webhook trigger
178
Build githubBuild = client.buildConfigs()
179
.inNamespace("my-project")
180
.withName("my-app")
181
.trigger()
182
.github("webhook-secret");
183
```
184
185
## Advanced Build Operations
186
187
### Build Status and Monitoring
188
189
```java { .api }
190
// Watch build progress
191
client.builds()
192
.inNamespace("my-project")
193
.withName("my-app-1")
194
.watch(new Watcher<Build>() {
195
@Override
196
public void eventReceived(Action action, Build build) {
197
System.out.println("Build " + build.getMetadata().getName() +
198
" phase: " + build.getStatus().getPhase());
199
}
200
201
@Override
202
public void onClose(WatcherException cause) {
203
// Handle watch close
204
}
205
});
206
207
// Check if build is complete
208
Build build = client.builds()
209
.inNamespace("my-project")
210
.withName("my-app-1")
211
.get();
212
213
boolean isComplete = "Complete".equals(build.getStatus().getPhase());
214
boolean isFailed = "Failed".equals(build.getStatus().getPhase());
215
```
216
217
### Build Configuration Updates
218
219
```java { .api }
220
// Update build configuration
221
BuildConfig updated = client.buildConfigs()
222
.inNamespace("my-project")
223
.withName("my-app")
224
.edit(bc -> new BuildConfigBuilder(bc)
225
.editSpec()
226
.editStrategy()
227
.editDockerStrategy()
228
.withFrom(new ObjectReferenceBuilder()
229
.withKind("ImageStreamTag")
230
.withName("nodejs:14-ubi8")
231
.build())
232
.endDockerStrategy()
233
.endStrategy()
234
.endSpec()
235
.build());
236
```
237
238
### Build Scaling and Management
239
240
```java { .api }
241
// Cancel a running build
242
Build cancelledBuild = client.builds()
243
.inNamespace("my-project")
244
.withName("my-app-1")
245
.edit(b -> new BuildBuilder(b)
246
.editStatus()
247
.withPhase("Cancelled")
248
.withCancelled(true)
249
.endStatus()
250
.build());
251
252
// Delete all builds for a build config
253
Boolean allDeleted = client.builds()
254
.inNamespace("my-project")
255
.withLabel("buildconfig", "my-app")
256
.delete();
257
```
258
259
## Usage Examples
260
261
### Complete Build Workflow Example
262
263
```java
264
import io.fabric8.openshift.client.OpenShiftClient;
265
import io.fabric8.openshift.api.model.*;
266
267
public class BuildWorkflow {
268
private final OpenShiftClient client;
269
270
public BuildWorkflow(OpenShiftClient client) {
271
this.client = client;
272
}
273
274
public void completeBuildWorkflow(String namespace, String appName) {
275
// 1. Create or get build configuration
276
BuildConfig buildConfig = getOrCreateBuildConfig(namespace, appName);
277
278
// 2. Start a build
279
Build build = client.buildConfigs()
280
.inNamespace(namespace)
281
.withName(appName)
282
.instantiate();
283
284
// 3. Monitor build progress
285
monitorBuild(namespace, build.getMetadata().getName());
286
287
// 4. Get build logs after completion
288
String logs = client.builds()
289
.inNamespace(namespace)
290
.withName(build.getMetadata().getName())
291
.getLog();
292
293
System.out.println("Build completed with logs:\n" + logs);
294
}
295
296
private BuildConfig getOrCreateBuildConfig(String namespace, String appName) {
297
BuildConfig existing = client.buildConfigs()
298
.inNamespace(namespace)
299
.withName(appName)
300
.get();
301
302
if (existing != null) {
303
return existing;
304
}
305
306
// Create new build config
307
return client.buildConfigs()
308
.inNamespace(namespace)
309
.create(new BuildConfigBuilder()
310
.withNewMetadata()
311
.withName(appName)
312
.withNamespace(namespace)
313
.addToLabels("app", appName)
314
.endMetadata()
315
.withNewSpec()
316
.withNewSource()
317
.withType("Git")
318
.withNewGit()
319
.withUri("https://github.com/openshift/ruby-ex.git")
320
.endGit()
321
.endSource()
322
.withNewStrategy()
323
.withType("Source")
324
.withNewSourceStrategy()
325
.withNewFrom()
326
.withKind("ImageStreamTag")
327
.withName("ruby:2.7-ubi8")
328
.withNamespace("openshift")
329
.endFrom()
330
.endSourceStrategy()
331
.endStrategy()
332
.withNewOutput()
333
.withNewTo()
334
.withKind("ImageStreamTag")
335
.withName(appName + ":latest")
336
.endTo()
337
.endOutput()
338
.endSpec()
339
.build());
340
}
341
342
private void monitorBuild(String namespace, String buildName) {
343
client.builds()
344
.inNamespace(namespace)
345
.withName(buildName)
346
.waitUntilCondition(build ->
347
"Complete".equals(build.getStatus().getPhase()) ||
348
"Failed".equals(build.getStatus().getPhase()),
349
10, TimeUnit.MINUTES);
350
}
351
}
352
```
353
354
### Binary Build Example
355
356
```java
357
public class BinaryBuildExample {
358
public void performBinaryBuild(OpenShiftClient client, String namespace,
359
String buildConfigName, File sourceDirectory) {
360
try {
361
// Start binary build from directory
362
Build build = client.buildConfigs()
363
.inNamespace(namespace)
364
.withName(buildConfigName)
365
.instantiateBinary()
366
.fromDirectory(sourceDirectory);
367
368
System.out.println("Started binary build: " + build.getMetadata().getName());
369
370
// Monitor progress with logs
371
String buildName = build.getMetadata().getName();
372
373
// Stream logs in real-time
374
client.builds()
375
.inNamespace(namespace)
376
.withName(buildName)
377
.watchLog(System.out);
378
379
// Wait for completion
380
client.builds()
381
.inNamespace(namespace)
382
.withName(buildName)
383
.waitUntilCondition(b ->
384
"Complete".equals(b.getStatus().getPhase()) ||
385
"Failed".equals(b.getStatus().getPhase()),
386
15, TimeUnit.MINUTES);
387
388
Build completedBuild = client.builds()
389
.inNamespace(namespace)
390
.withName(buildName)
391
.get();
392
393
if ("Complete".equals(completedBuild.getStatus().getPhase())) {
394
System.out.println("Build completed successfully!");
395
} else {
396
System.out.println("Build failed: " +
397
completedBuild.getStatus().getMessage());
398
}
399
400
} catch (Exception e) {
401
System.err.println("Binary build failed: " + e.getMessage());
402
throw new RuntimeException(e);
403
}
404
}
405
}
406
```
407
408
## Types
409
410
### Build
411
412
```java { .api }
413
public class Build implements HasMetadata {
414
public ObjectMeta getMetadata();
415
public BuildSpec getSpec();
416
public BuildStatus getStatus();
417
}
418
419
public class BuildSpec {
420
public BuildSource getSource();
421
public BuildStrategy getStrategy();
422
public BuildOutput getOutput();
423
public BuildConfigRef getBuildConfigRef();
424
public List<BuildTriggerCause> getTriggeredBy();
425
}
426
427
public class BuildStatus {
428
public String getPhase(); // Pending, Running, Complete, Failed, Error, Cancelled
429
public String getMessage();
430
public String getStartTimestamp();
431
public String getCompletionTimestamp();
432
public Long getDuration();
433
public String getOutputDockerImageReference();
434
public Boolean getCancelled();
435
}
436
```
437
438
### BuildConfig
439
440
```java { .api }
441
public class BuildConfig implements HasMetadata {
442
public ObjectMeta getMetadata();
443
public BuildConfigSpec getSpec();
444
public BuildConfigStatus getStatus();
445
}
446
447
public class BuildConfigSpec {
448
public BuildSource getSource();
449
public BuildStrategy getStrategy();
450
public BuildOutput getOutput();
451
public List<BuildTriggerPolicy> getTriggers();
452
public String getRunPolicy(); // Serial, Parallel, SerialLatestOnly
453
public Integer getSuccessfulBuildsHistoryLimit();
454
public Integer getFailedBuildsHistoryLimit();
455
}
456
457
public class BuildConfigStatus {
458
public Integer getLastVersion();
459
public List<SourceRevision> getLastRevisions();
460
}
461
```
462
463
### BuildResource DSL
464
465
```java { .api }
466
public interface BuildResource extends Resource<Build> {
467
String getLog();
468
String getLog(boolean follow);
469
BuildResource withVersion(Integer version);
470
}
471
```
472
473
### BuildConfigResource DSL
474
475
```java { .api }
476
public interface BuildConfigResource<T, S, I> extends Resource<T> {
477
Build instantiate();
478
Build instantiate(BuildRequest buildRequest);
479
BinaryBuildRequestBuilder instantiateBinary();
480
WebhookTriggerBuilder trigger();
481
}
482
```
483
484
### Binary Build Operations
485
486
```java { .api }
487
public class BinaryBuildRequestBuilder {
488
public Build fromDirectory(File directory);
489
public Build fromFile(File file);
490
public Build fromInputStream(InputStream inputStream);
491
}
492
```
493
494
### Webhook Trigger Operations
495
496
```java { .api }
497
public class WebhookTriggerBuilder {
498
public Build webhookTrigger(String secret);
499
public Build generic(String secret);
500
public Build github(String secret);
501
public Build gitlab(String secret);
502
public Build bitbucket(String secret);
503
}
504
```