20
20
import hudson .util .ListBoxModel ;
21
21
import io .fabric8 .kubernetes .client .KubernetesClient ;
22
22
import io .fabric8 .tekton .client .TektonClient ;
23
+ import io .fabric8 .tekton .pipeline .v1beta1 .ArrayOrString ;
24
+ import io .fabric8 .tekton .pipeline .v1beta1 .Param ;
23
25
import io .fabric8 .tekton .pipeline .v1beta1 .Pipeline ;
24
26
import io .fabric8 .tekton .pipeline .v1beta1 .PipelineRun ;
27
+ import io .fabric8 .tekton .pipeline .v1beta1 .PipelineRunSpec ;
25
28
import io .fabric8 .tekton .pipeline .v1beta1 .Task ;
26
29
import io .fabric8 .tekton .pipeline .v1beta1 .TaskRun ;
27
30
import io .jenkins .plugins .checks .api .ChecksConclusion ;
32
35
import io .jenkins .plugins .checks .api .ChecksStatus ;
33
36
import java .time .LocalDateTime ;
34
37
import java .time .ZoneOffset ;
38
+ import java .util .ArrayList ;
39
+ import java .util .Arrays ;
40
+ import java .util .Optional ;
35
41
import org .jenkinsci .Symbol ;
36
42
import org .jenkinsci .plugins .displayurlapi .DisplayURLProvider ;
37
43
import org .kohsuke .stapler .DataBoundConstructor ;
@@ -143,7 +149,7 @@ public String getClusterName() {
143
149
return clusterName ;
144
150
}
145
151
146
- protected String createWithResourceSpecificClient (TektonResourceType resourceType , InputStream inputStream ) throws Exception {
152
+ protected String createWithResourceSpecificClient (TektonResourceType resourceType , InputStream inputStream , EnvVars envVars ) throws Exception {
147
153
switch (resourceType ) {
148
154
case task :
149
155
return createTask (inputStream );
@@ -152,7 +158,7 @@ protected String createWithResourceSpecificClient(TektonResourceType resourceTyp
152
158
case pipeline :
153
159
return createPipeline (inputStream );
154
160
case pipelinerun :
155
- return createPipelineRun (inputStream );
161
+ return createPipelineRun (inputStream , envVars );
156
162
default :
157
163
return "" ;
158
164
}
@@ -220,35 +226,71 @@ public String createPipeline(InputStream inputStream) {
220
226
return resourceName ;
221
227
}
222
228
223
- public String createPipelineRun (InputStream inputStream ) throws Exception {
229
+ public String createPipelineRun (InputStream inputStream , EnvVars envVars ) throws Exception {
224
230
if (pipelineRunClient == null ) {
225
231
TektonClient tc = (TektonClient ) tektonClient ;
226
232
setPipelineRunClient (tc .v1beta1 ().pipelineRuns ());
227
233
}
228
234
String resourceName ;
229
- PipelineRun pipelineRun = pipelineRunClient .load (inputStream ).get ();
235
+ final PipelineRun pipelineRun = pipelineRunClient .load (inputStream ).get ();
230
236
if (!Strings .isNullOrEmpty (namespace ) && Strings .isNullOrEmpty (pipelineRun .getMetadata ().getNamespace ())) {
231
237
pipelineRun .getMetadata ().setNamespace (namespace );
232
238
}
239
+
240
+ // GIT_BRANCH=origin/main
241
+ // GIT_COMMIT=bb1ef888f5375bb19d5bb227e35bfcfed49759ed
242
+ // GIT_PREVIOUS_COMMIT=9c7648f892913dfa12963a38fe489b1291e033a8
243
+ // GIT_PREVIOUS_SUCCESSFUL_COMMIT=9c7648f892913dfa12963a38fe489b1291e033a8
244
+ // GIT_URL=https://github.com/garethjevans/test-tekton-client
245
+
246
+ setParamOnPipelineRunSpec (pipelineRun .getSpec (), "BUILD_ID" , envVars .get ("BUILD_ID" ));
247
+ // JOB_NAME
248
+ // JOB_SPEC
249
+ // JOB_TYPE
250
+ // PULL_BASE_REF
251
+ setParamOnPipelineRunSpec (pipelineRun .getSpec (), "PULL_BASE_SHA" , envVars .get ("GIT_COMMIT" ));
252
+ // PULL_NUMBER
253
+ // PULL_PULL_REF
254
+ // PULL_PULL_SHA
255
+ // PULL_REFS
256
+ // REPO_NAME
257
+ // REPO_OWNER
258
+ setParamOnPipelineRunSpec (pipelineRun .getSpec (), "REPO_URL" , envVars .get ("GIT_URL" ));
259
+
233
260
String ns = pipelineRun .getMetadata ().getNamespace ();
234
- if (Strings .isNullOrEmpty (ns )) {
235
- pipelineRun = pipelineRunClient .create (pipelineRun );
236
- } else {
237
- pipelineRun = pipelineRunClient .inNamespace (ns ).create (pipelineRun );
238
- }
239
- resourceName = pipelineRun .getMetadata ().getName ();
261
+
262
+ LOGGER .info ("Creating PipelineRun " + pipelineRun );
263
+
264
+ PipelineRun updatedPipelineRun = Strings .isNullOrEmpty (ns ) ?
265
+ pipelineRunClient .create (pipelineRun ) :
266
+ pipelineRunClient .inNamespace (ns ).create (pipelineRun );
267
+
268
+ resourceName = updatedPipelineRun .getMetadata ().getName ();
240
269
241
270
ChecksDetails checkDetails = new ChecksDetails .ChecksDetailsBuilder ()
242
- .withName ("Tekton: " + pipelineRun .getMetadata ().getName ())
271
+ .withName ("Tekton: " + updatedPipelineRun .getMetadata ().getName ())
243
272
.withStatus (ChecksStatus .IN_PROGRESS )
244
273
.withConclusion (ChecksConclusion .NONE )
245
274
.build ();
246
275
checksPublisher .publish (checkDetails );
247
276
248
- streamPipelineRunLogsToConsole (pipelineRun );
277
+ streamPipelineRunLogsToConsole (updatedPipelineRun );
278
+
249
279
return resourceName ;
250
280
}
251
281
282
+ private void setParamOnPipelineRunSpec (@ NonNull PipelineRunSpec spec , String paramName , String paramValue ) {
283
+ if (spec .getParams () == null ) {
284
+ spec .setParams (new ArrayList <>());
285
+ }
286
+ Optional <Param > param = spec .getParams ().stream ().filter (p -> p .getName ().equals (paramName )).findAny ();
287
+ if (param .isPresent ()) {
288
+ param .get ().setValue (new ArrayOrString (paramValue ));
289
+ } else {
290
+ spec .getParams ().add (new Param (paramName , new ArrayOrString (paramValue )));
291
+ }
292
+ }
293
+
252
294
public void streamTaskRunLogsToConsole (TaskRun taskRun ) throws Exception {
253
295
synchronized (consoleLogger ) {
254
296
KubernetesClient kc = (KubernetesClient ) kubernetesClient ;
@@ -338,7 +380,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
338
380
} else {
339
381
TektonResourceType resourceType = kind .get (0 );
340
382
LOGGER .info ("creating kind " + resourceType .name ());
341
- createdResourceName = createWithResourceSpecificClient (resourceType , new ByteArrayInputStream (data ));
383
+ createdResourceName = createWithResourceSpecificClient (resourceType , new ByteArrayInputStream (data ), envVars );
342
384
}
343
385
}
344
386
} catch (Throwable e ) {
@@ -488,6 +530,7 @@ private byte[] processTektonCatalog(EnvVars envVars, File dir, File file, byte[]
488
530
builder .command (binary , "-b" , "--add-defaults" , "-f" , filePath , "-o" , outputFile .getPath ());
489
531
if (envVars != null ) {
490
532
for (Map .Entry <String , String > entry : envVars .entrySet ()) {
533
+ LOGGER .info ("Adding env var " + entry .getKey () + "=" + entry .getValue ());
491
534
builder .environment ().put (entry .getKey (), entry .getValue ());
492
535
}
493
536
}
0 commit comments