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,79 @@ 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
+ // BUILD_ID
241
+ // JOB_NAME
242
+ // JOB_SPEC
243
+ // JOB_TYPE
244
+ // PULL_BASE_REF
245
+ // PULL_BASE_SHA
246
+ // PULL_NUMBER
247
+ // PULL_PULL_REF
248
+ // PULL_PULL_SHA
249
+ // PULL_REFS
250
+ // REPO_NAME
251
+ // REPO_OWNER
252
+ // REPO_URL
253
+
254
+ // GIT_BRANCH=origin/main
255
+ // GIT_COMMIT=bb1ef888f5375bb19d5bb227e35bfcfed49759ed
256
+ // GIT_PREVIOUS_COMMIT=9c7648f892913dfa12963a38fe489b1291e033a8
257
+ // GIT_PREVIOUS_SUCCESSFUL_COMMIT=9c7648f892913dfa12963a38fe489b1291e033a8
258
+ // GIT_URL=https://github.com/garethjevans/test-tekton-client
259
+
260
+ setParamOnPipelineRunSpec (pipelineRun .getSpec (), "BUILD_ID" , envVars .get ("BUILD_ID" ));
261
+ setParamOnPipelineRunSpec (pipelineRun .getSpec (), "PULL_BASE_SHA" , envVars .get ("GIT_COMMIT" ));
262
+ setParamOnPipelineRunSpec (pipelineRun .getSpec (), "REPO_URL" , envVars .get ("GIT_URL" ));
263
+
264
+ pipelineRun .getSpec ().getParams ().add (new Param ("BUILD_ID" , new ArrayOrString (envVars .get ("BUILD_ID" ))));
265
+ pipelineRun .getSpec ().getParams ().add (new Param ("" , new ArrayOrString (envVars .get ("GIT_COMMIT" ))));
266
+ pipelineRun .getSpec ().getParams ().add (new Param ("" , new ArrayOrString (envVars .get ("GIT_URL" ))));
267
+
233
268
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 ();
269
+
270
+ LOGGER .info ("Creating PipelineRun " + pipelineRun );
271
+
272
+ PipelineRun updatedPipelineRun = Strings .isNullOrEmpty (ns ) ?
273
+ pipelineRunClient .create (pipelineRun ) :
274
+ pipelineRunClient .inNamespace (ns ).create (pipelineRun );
275
+
276
+ resourceName = updatedPipelineRun .getMetadata ().getName ();
240
277
241
278
ChecksDetails checkDetails = new ChecksDetails .ChecksDetailsBuilder ()
242
- .withName ("Tekton: " + pipelineRun .getMetadata ().getName ())
279
+ .withName ("Tekton: " + updatedPipelineRun .getMetadata ().getName ())
243
280
.withStatus (ChecksStatus .IN_PROGRESS )
244
281
.withConclusion (ChecksConclusion .NONE )
245
282
.build ();
246
283
checksPublisher .publish (checkDetails );
247
284
248
- streamPipelineRunLogsToConsole (pipelineRun );
285
+ streamPipelineRunLogsToConsole (updatedPipelineRun );
286
+
249
287
return resourceName ;
250
288
}
251
289
290
+ private void setParamOnPipelineRunSpec (@ NonNull PipelineRunSpec spec , String paramName , String paramValue ) {
291
+ if (spec .getParams () == null ) {
292
+ spec .setParams (new ArrayList <>());
293
+ }
294
+ Optional <Param > param = spec .getParams ().stream ().filter (p -> p .getName ().equals (paramName )).findAny ();
295
+ if (param .isPresent ()) {
296
+ param .get ().setValue (new ArrayOrString (paramValue ));
297
+ } else {
298
+ spec .getParams ().add (new Param (paramName , new ArrayOrString (paramValue )));
299
+ }
300
+ }
301
+
252
302
public void streamTaskRunLogsToConsole (TaskRun taskRun ) throws Exception {
253
303
synchronized (consoleLogger ) {
254
304
KubernetesClient kc = (KubernetesClient ) kubernetesClient ;
@@ -338,7 +388,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
338
388
} else {
339
389
TektonResourceType resourceType = kind .get (0 );
340
390
LOGGER .info ("creating kind " + resourceType .name ());
341
- createdResourceName = createWithResourceSpecificClient (resourceType , new ByteArrayInputStream (data ));
391
+ createdResourceName = createWithResourceSpecificClient (resourceType , new ByteArrayInputStream (data ), envVars );
342
392
}
343
393
}
344
394
} catch (Throwable e ) {
@@ -488,6 +538,7 @@ private byte[] processTektonCatalog(EnvVars envVars, File dir, File file, byte[]
488
538
builder .command (binary , "-b" , "--add-defaults" , "-f" , filePath , "-o" , outputFile .getPath ());
489
539
if (envVars != null ) {
490
540
for (Map .Entry <String , String > entry : envVars .entrySet ()) {
541
+ LOGGER .info ("Adding env var " + entry .getKey () + "=" + entry .getValue ());
491
542
builder .environment ().put (entry .getKey (), entry .getValue ());
492
543
}
493
544
}
0 commit comments