Skip to content

Commit 7c3294d

Browse files
chmouelpipelines-as-code[bot]
authored andcommitted
fix: Expand "dynamic" variables in default values
This commit addresses an issue where dynamic variables used as default values in pipeline parameters are not resolved correctly. Currently, when an expandable dynamic variable is added as a default value to a parameter in a Pipeline, it is not resolved, leading to errors. For example, in the Pipeline.yaml: - name: REPO_NAME default: "{{ repo_name }}" - name: REVISION - name: BRANCH - name: PULL_REQUEST_NUMBER Omitting the parameter with a default value in the PipelineRun does not resolve the parameter, resulting not being expanded in the PipelineRun. Business value: - Clean up pipelines by reducing the need to pass through all expandable dynamic parameters. - Make shared pipelines easier to use and reduce the surface for mistakes. Jira: https://issues.redhat.com/browse/SRVKP-4070 Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
1 parent 26b5b0e commit 7c3294d

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

pkg/pipelineascode/match.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
322322
}
323323
}
324324

325-
err = changeSecret(pipelineRuns)
325+
err = p.changePipelineRun(ctx, repo, pipelineRuns)
326326
if err != nil {
327327
return nil, err
328328
}
@@ -357,12 +357,13 @@ func filterRunningPipelineRunOnTargetTest(testPipeline string, prs []*tektonv1.P
357357
return nil
358358
}
359359

360-
// changeSecret we need to go in each pipelinerun,
361-
// change the secret template variable with a random one as generated from GetBasicAuthSecretName
362-
// and store in the annotations so we can create one delete after.
363-
func changeSecret(prs []*tektonv1.PipelineRun) error {
364-
for k, p := range prs {
365-
b, err := json.Marshal(p)
360+
// changePipelineRun go over each pipelineruns and modify things into it.
361+
//
362+
// - the secret template variable with a random one as generated from GetBasicAuthSecretName
363+
// - the template variable with the one from the event (this includes the remote pipeline that has template variables).
364+
func (p *PacRun) changePipelineRun(ctx context.Context, repo *v1alpha1.Repository, prs []*tektonv1.PipelineRun) error {
365+
for k, pr := range prs {
366+
b, err := json.Marshal(pr)
366367
if err != nil {
367368
return err
368369
}
@@ -371,6 +372,7 @@ func changeSecret(prs []*tektonv1.PipelineRun) error {
371372
processed := templates.ReplacePlaceHoldersVariables(string(b), map[string]string{
372373
"git_auth_secret": name,
373374
}, nil, nil, map[string]any{})
375+
processed = p.makeTemplate(ctx, repo, processed)
374376

375377
var np *tektonv1.PipelineRun
376378
err = json.Unmarshal([]byte(processed), &np)
@@ -382,6 +384,7 @@ func changeSecret(prs []*tektonv1.PipelineRun) error {
382384
np.Annotations = map[string]string{}
383385
}
384386
np.Annotations[apipac.GitAuthSecret] = name
387+
385388
prs[k] = np
386389
}
387390
return nil

pkg/pipelineascode/match_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,30 @@ func TestPacRun_checkNeedUpdate(t *testing.T) {
5050
}
5151
}
5252

53-
func TestChangeSecret(t *testing.T) {
53+
func TestChangePipelineRun(t *testing.T) {
5454
prs := []*tektonv1.PipelineRun{
5555
{
5656
ObjectMeta: metav1.ObjectMeta{
57-
Name: "{{git_auth_secret}}",
57+
Name: "{{git_auth_secret}}",
58+
Namespace: "{{ repo_name }}",
5859
},
5960
},
6061
}
61-
err := changeSecret(prs)
62+
event := info.NewEvent()
63+
event.Repository = "testrepo"
64+
p := &PacRun{event: event}
65+
repo := &v1alpha1.Repository{
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: "testrepo",
68+
Namespace: "test",
69+
},
70+
}
71+
ctx, _ := rtesting.SetupFakeContext(t)
72+
err := p.changePipelineRun(ctx, repo, prs)
6273
assert.NilError(t, err)
6374
assert.Assert(t, strings.HasPrefix(prs[0].GetName(), "pac-gitauth"), prs[0].GetName(), "has no pac-gitauth prefix")
6475
assert.Assert(t, prs[0].GetAnnotations()[apipac.GitAuthSecret] != "")
76+
assert.Assert(t, prs[0].GetNamespace() == "testrepo", "namespace should be testrepo: %v", prs[0].GetNamespace())
6577
}
6678

6779
func TestFilterRunningPipelineRunOnTargetTest(t *testing.T) {

test/gitea_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ func TestGiteaPullRequestResolvePipelineOnlyAssociatedWithPipelineRunFilterted(t
139139
defer f()
140140
}
141141

142+
// TestGiteaPullRequestResolvedTektonParamsRemotePipeline see
143+
// https://issues.redhat.com/browse/SRVKP-4070 for details
144+
func TestGiteaPullRequestResolvedTektonParamsRemotePipeline(t *testing.T) {
145+
topts := &tgitea.TestOpts{
146+
Regexp: successRegexp,
147+
TargetEvent: triggertype.PullRequest.String(),
148+
YAMLFiles: map[string]string{
149+
".tekton/pr.yaml": "testdata/pipelinerun_with_tekton_params.yaml",
150+
".tekton/pipeline.yaml": "testdata/pipeline_with_tekton_params.yaml",
151+
},
152+
ExpectEvents: false,
153+
CheckForStatus: "success",
154+
}
155+
_, f := tgitea.TestPR(t, topts)
156+
defer f()
157+
158+
// check the output of the PipelineRun logs
159+
err := twait.RegexpMatchingInPodLog(context.Background(),
160+
topts.ParamsRun,
161+
topts.TargetNS, "pipelinesascode.tekton.dev/event-type=pull_request", "step-task",
162+
*regexp.MustCompile("Hello " + topts.TargetRepoName), "", 2)
163+
assert.NilError(t, err)
164+
}
165+
142166
func TestGiteaPullRequestPrivateRepository(t *testing.T) {
143167
topts := &tgitea.TestOpts{
144168
Regexp: successRegexp,
@@ -1047,7 +1071,8 @@ func verifyProvenance(t *testing.T, topts *tgitea.TestOpts, expectedOutput, cNam
10471071
tgitea.WaitForStatus(t, topts, "heads/"+targetRef, "", false)
10481072

10491073
// check the output of the PipelineRun logs
1050-
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, "pipelinesascode.tekton.dev/event-type=pull_request", cName, *regexp.MustCompile(expectedOutput), "", 2)
1074+
err = twait.RegexpMatchingInPodLog(context.Background(), topts.ParamsRun, topts.TargetNS, "pipelinesascode.tekton.dev/event-type=pull_request",
1075+
cName, *regexp.MustCompile(expectedOutput), "", 2)
10511076
assert.NilError(t, err)
10521077
}
10531078

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
apiVersion: tekton.dev/v1
3+
kind: Pipeline
4+
metadata:
5+
name: pipeline-with-tekton-params
6+
spec:
7+
params:
8+
- name: foo
9+
type: string
10+
default: "{{ repo_name }}"
11+
tasks:
12+
- name: task
13+
taskSpec:
14+
steps:
15+
- name: task
16+
image: registry.access.redhat.com/ubi9/ubi-micro
17+
script: |
18+
echo "Hello $(params.foo)"
19+
exit 0
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: PipelineRun
4+
metadata:
5+
name: "piplinerun-with-tekton-params"
6+
annotations:
7+
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
8+
pipelinesascode.tekton.dev/on-target-branch: "[\\ .TargetBranch //]"
9+
pipelinesascode.tekton.dev/on-event: "[\\ .TargetEvent //]"
10+
spec:
11+
pipelineRef:
12+
name: pipeline-with-tekton-params

0 commit comments

Comments
 (0)