@@ -246,6 +246,9 @@ func ApplyTaskResults(targets PipelineRunState, resolvedResultRefs ResolvedResul
246
246
pipelineTask .TaskRef .Params = pipelineTask .TaskRef .Params .ReplaceVariables (stringReplacements , arrayReplacements , objectReplacements )
247
247
}
248
248
pipelineTask .DisplayName = substitution .ApplyReplacements (pipelineTask .DisplayName , stringReplacements )
249
+ for _ , workspace := range pipelineTask .Workspaces {
250
+ workspace .SubPath = substitution .ApplyReplacements (workspace .SubPath , stringReplacements )
251
+ }
249
252
resolvedPipelineRunTask .PipelineTask = pipelineTask
250
253
}
251
254
}
@@ -374,6 +377,50 @@ func propagateParams(t v1.PipelineTask, stringReplacements map[string]string, ar
374
377
return t
375
378
}
376
379
380
+ // ApplyResultsToWorkspaceBindings applies results from TaskRuns to WorkspaceBindings in a PipelineRun. It replaces placeholders in
381
+ // various binding types with values from TaskRun results.
382
+ func ApplyResultsToWorkspaceBindings (trResults map [string ][]v1.TaskRunResult , pr * v1.PipelineRun ) {
383
+ stringReplacements := map [string ]string {}
384
+ arrayReplacements := map [string ][]string {}
385
+ for taskName , taskResults := range trResults {
386
+ for _ , res := range taskResults {
387
+ switch res .Type {
388
+ case v1 .ResultsTypeString :
389
+ stringReplacements [fmt .Sprintf ("tasks.%s.results.%s" , taskName , res .Name )] = res .Value .StringVal
390
+ case v1 .ResultsTypeArray :
391
+ arrayReplacements [fmt .Sprintf ("tasks.%s.results.%s" , taskName , res .Name )] = res .Value .ArrayVal
392
+ case v1 .ResultsTypeObject :
393
+ for k , v := range res .Value .ObjectVal {
394
+ stringReplacements [fmt .Sprintf ("tasks.%s.results.%s.%s" , taskName , res .Name , k )] = v
395
+ }
396
+ }
397
+ }
398
+ }
399
+
400
+ for i , binding := range pr .Spec .Workspaces {
401
+ if pr .Spec .Workspaces [i ].PersistentVolumeClaim != nil {
402
+ pr .Spec .Workspaces [i ].PersistentVolumeClaim .ClaimName = substitution .ApplyReplacements (binding .PersistentVolumeClaim .ClaimName , stringReplacements )
403
+ }
404
+ pr .Spec .Workspaces [i ].SubPath = substitution .ApplyReplacements (binding .SubPath , stringReplacements )
405
+ if pr .Spec .Workspaces [i ].ConfigMap != nil {
406
+ pr .Spec .Workspaces [i ].ConfigMap .Name = substitution .ApplyReplacements (binding .ConfigMap .Name , stringReplacements )
407
+ }
408
+ if pr .Spec .Workspaces [i ].Secret != nil {
409
+ pr .Spec .Workspaces [i ].Secret .SecretName = substitution .ApplyReplacements (binding .Secret .SecretName , stringReplacements )
410
+ }
411
+ if pr .Spec .Workspaces [i ].Projected != nil {
412
+ for j , source := range binding .Projected .Sources {
413
+ if pr .Spec .Workspaces [i ].Projected .Sources [j ].ConfigMap != nil {
414
+ pr .Spec .Workspaces [i ].Projected .Sources [j ].ConfigMap .Name = substitution .ApplyReplacements (source .ConfigMap .Name , stringReplacements )
415
+ }
416
+ if pr .Spec .Workspaces [i ].Projected .Sources [j ].Secret != nil {
417
+ pr .Spec .Workspaces [i ].Projected .Sources [j ].Secret .Name = substitution .ApplyReplacements (source .Secret .Name , stringReplacements )
418
+ }
419
+ }
420
+ }
421
+ }
422
+ }
423
+
377
424
// PropagateResults propagate the result of the completed task to the unfinished task that is not explicitly specify in the params
378
425
func PropagateResults (rpt * ResolvedPipelineTask , runStates PipelineRunState ) {
379
426
if rpt .ResolvedTask == nil || rpt .ResolvedTask .TaskSpec == nil {
@@ -548,3 +595,36 @@ func runResultValue(taskName string, resultName string, runResults map[string][]
548
595
}
549
596
return nil
550
597
}
598
+
599
+ // ApplyParametersToWorkspaceBindings applies parameters from PipelineSpec and PipelineRun to the WorkspaceBindings in a PipelineRun. It replaces
600
+ // placeholders in various binding types with values from provided parameters.
601
+ func ApplyParametersToWorkspaceBindings (ctx context.Context , ps * v1.PipelineSpec , pr * v1.PipelineRun ) {
602
+ psCopy := ps .DeepCopy ()
603
+ var defaults []v1.ParamSpec
604
+ if len (psCopy .Params ) > 0 {
605
+ defaults = append (defaults , psCopy .Params ... )
606
+ }
607
+ parameters , _ , _ := paramsFromPipelineRun (ctx , pr )
608
+ for i , binding := range pr .Spec .Workspaces {
609
+ if pr .Spec .Workspaces [i ].PersistentVolumeClaim != nil {
610
+ pr .Spec .Workspaces [i ].PersistentVolumeClaim .ClaimName = substitution .ApplyReplacements (binding .PersistentVolumeClaim .ClaimName , parameters )
611
+ }
612
+ pr .Spec .Workspaces [i ].SubPath = substitution .ApplyReplacements (binding .SubPath , parameters )
613
+ if pr .Spec .Workspaces [i ].ConfigMap != nil {
614
+ pr .Spec .Workspaces [i ].ConfigMap .Name = substitution .ApplyReplacements (binding .ConfigMap .Name , parameters )
615
+ }
616
+ if pr .Spec .Workspaces [i ].Secret != nil {
617
+ pr .Spec .Workspaces [i ].Secret .SecretName = substitution .ApplyReplacements (binding .Secret .SecretName , parameters )
618
+ }
619
+ if pr .Spec .Workspaces [i ].Projected != nil {
620
+ for j , source := range binding .Projected .Sources {
621
+ if pr .Spec .Workspaces [i ].Projected .Sources [j ].ConfigMap != nil {
622
+ pr .Spec .Workspaces [i ].Projected .Sources [j ].ConfigMap .Name = substitution .ApplyReplacements (source .ConfigMap .Name , parameters )
623
+ }
624
+ if pr .Spec .Workspaces [i ].Projected .Sources [j ].Secret != nil {
625
+ pr .Spec .Workspaces [i ].Projected .Sources [j ].Secret .Name = substitution .ApplyReplacements (source .Secret .Name , parameters )
626
+ }
627
+ }
628
+ }
629
+ }
630
+ }
0 commit comments