Skip to content

Commit bf0a99c

Browse files
chmouelsavitaashture
authored andcommitted
Add doc for passing arguments to GitOps commands
Add documentation for passing arguments to GitOps commands. We moved all the documentation in a separate file to make it easier to read and find since it was becoming too big in the `running.md` file. Add some unittest as well so to make sure the documentation match the implementation 🙃 Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 39e9eee commit bf0a99c

File tree

5 files changed

+310
-179
lines changed

5 files changed

+310
-179
lines changed

docs/content/docs/guide/authoringprs.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,27 @@ weight: 3
2525
[git-clone](https://github.com/tektoncd/catalog/blob/main/task/git-clone/)
2626
task from the [tektoncd/catalog](https://github.com/tektoncd/catalog).
2727

28-
To be able to specify the parameters of your commit and URL, Pipelines-as-Code
29-
allows you to have those "dynamic" variables expanded. Those variables look
30-
like this `{{ var }}` and those are the one you can use:
28+
- To be able to specify parameters of your commit and URL, Pipelines-as-Code
29+
give you some “dynamic” variables that is defined according to the execution
30+
of the events. Those variables look like this `{{ var }}` and can be used
31+
anywhere in your template, see [below](#dynamic-variables) for the list of
32+
available variables.
33+
34+
- For Pipelines-as-Code to process your `PipelineRun`, you must have either an
35+
embedded `PipelineSpec` or a separate `Pipeline` object that references a YAML
36+
file in the `.tekton` directory. The Pipeline object can include `TaskSpecs`,
37+
which may be defined separately as Tasks in another YAML file in the same
38+
directory. It's important to give each `PipelineRun` a unique name to avoid
39+
conflicts. **PipelineRuns with duplicate names will never be matched**.
40+
41+
## Dynamic variables
42+
43+
Here is a list of al the dynamic variables available in Pipelines-as-Code. The
44+
one that would be the most important to you would probably be the `revision` and `repo_url`
45+
variables, they will give you the commit SHA and the repository URL that is
46+
getting tested. You usually use this with the
47+
[git-clone](https://hub.tekton.dev/tekton/task/git-clone) task to be able to
48+
checkout the code that is being tested.
3149

3250
| Variable | Description | Example | Example Output |
3351
|---------------------|---------------------------------------------------------------------------------------------------|-------------------------------------|------------------------------|
@@ -47,13 +65,6 @@ weight: 3
4765
| target_namespace | The target namespace where the Repository has matched and the PipelineRun will be created. | `{{target_namespace}}` | my-namespace |
4866
| trigger_comment | The comment triggering the pipelinerun when using a [GitOps command]({{< relref "/docs/guide/running.md#gitops-command-on-pull-or-merge-request" >}}) (like `/test`, `/retest`) | `{{trigger_comment}}` | /merge-pr branch |
4967

50-
- For Pipelines-as-Code to process your `PipelineRun`, you must have either an
51-
embedded `PipelineSpec` or a separate `Pipeline` object that references a YAML
52-
file in the `.tekton` directory. The Pipeline object can include `TaskSpecs`,
53-
which may be defined separately as Tasks in another YAML file in the same
54-
directory. It's important to give each `PipelineRun` a unique name to avoid
55-
conflicts. **PipelineRuns with duplicate names will never be matched**.
56-
5768
## Matching an event to a PipelineRun
5869

5970
Each `PipelineRun` can match different Git provider events through some special
@@ -196,13 +207,9 @@ metadata:
196207
Will match the merge-pr PipelineRun when a comment on a pull request starts
197208
with `/merge-pr`
198209

199-
You can then use the template variable `{{ trigger_comment }}` to get the
200-
actual comment and do some action based on for example the comment content.
201-
There is a restriction with the trigger_comment variable we modify it to
202-
replace the newline with a `\n` since the multi-line comment can cause a
203-
issue when replaced inside the yaml. It is up to you to replace it back to
204-
newline, for example with shell scripts you can use `echo -e` to expand the
205-
newline back.
210+
When the PipelineRun that has been triggered with the `on-comment` annotation
211+
gets started the template variable `{{ trigger_comment }}` get set. See the
212+
documentation [here]({{< relref "/docs/guide/gitops_commands/#accessing-the-comment-triggering-the-pipelinerun" >}})
206213

207214
Note that the `on-comment` annotation will respect the `pull_request` [Policy]({{< relref "/docs/guide/policy" >}}) rule,
208215
so only users into the `pull_request` policy will be able to trigger the
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
---
2+
title: Gitops Commands
3+
weight: 5.1
4+
---
5+
# GitOps commands
6+
7+
Pipelines-as-Code support the concept of `GitOps commands` a way to have the
8+
users issue a special command on a comment of a Pull Request or a Pushed Commit
9+
to control `Pipelines-as-Code`.
10+
11+
The advantage of using a `Gitops command` is to get a journal of all the
12+
execution of your Pipeline directly on your Pull request near your code.
13+
14+
## GitOps commands on Pull Requests
15+
16+
For example when you are on a Pull Request you may want to restart all your
17+
pipelineruns, to do so you can add a comment on your pull request starting with
18+
`/retest` and all PipelineRuns attached to that pull request will be restarted :
19+
20+
Example :
21+
22+
```text
23+
Thanks for contributing, This is a much needed bugfix, and we love it ❤️ The
24+
failure is not with your PR but seems to be an infra issue.
25+
26+
/retest
27+
```
28+
29+
If you have multiple `PipelineRun` and you want to target a specific
30+
`PipelineRun` you can use the `/test` and the specific PipelineRun as a comment
31+
to restart it, example:
32+
33+
```text
34+
roses are red, violets are blue. pipeline are bound to flake by design.
35+
36+
/test <pipelinerun-name>
37+
```
38+
39+
## GitOps commands on pushed commits
40+
41+
If you want to trigger a GitOps command on a pushed commit, you can include the
42+
`GitOps` comments within your commit messages. These comments can be used to
43+
restart either all pipelines or specific ones. Here's how it works:
44+
45+
For restarting all pipeline runs:
46+
47+
1. Use `/retest` or `/test` within your commit message.
48+
49+
For restarting a specific pipeline run:
50+
2. Use `/retest <pipelinerun-name>` or `/test <pipelinerun-name>` within your
51+
commit message. Replace `<pipelinerun-name>` with the specific name of the
52+
pipeline run you want to restart.
53+
54+
**Note:**
55+
56+
When executing `GitOps` commands on a commit that exists in multiple branches
57+
within a push request, the branch with the latest commit will be used.
58+
59+
This means:
60+
61+
1. If a user specifies commands like `/retest` or `/test` without any argument
62+
in a comment on a branch, the test will automatically be performed on the **main** branch.
63+
64+
Examples :
65+
1. `/retest`
66+
2. `/test`
67+
3. `/retest <pipelinerun-name>`
68+
4. `/test <pipelinerun-name>`
69+
70+
2. If the user includes a branch specification such as `/retest branch:test` or
71+
`/test branch:test`, the test will be executed on the commit where the comment is
72+
located, with the context of the **test** branch.
73+
74+
Examples :
75+
1. `/retest branch:test`
76+
2. `/test branch:test`
77+
3. `/retest <pipelinerun-name> branch:test`
78+
4. `/test <pipelinerun-name> branch:test`
79+
80+
To issue a `GitOps` comment on a pushed commit you can follow these steps:
81+
82+
1. Go to your repository.
83+
2. Click on the **Commits** section.
84+
3. Choose one of the individual **Commit**.
85+
4. Click on the line number where you want to add a `GitOps` comment, as shown in the image below:
86+
87+
![GitOps Commits For Comments](/images/gitops-comments-on-commit.png)
88+
89+
Please note that this feature is supported for the GitHub provider only.
90+
91+
## GitOps commands on non-matching PipelineRun
92+
93+
The PipelineRun will be restarted regardless of the annotations if the comment
94+
`/test <pipelinerun-name>` or `/retest <pipelinerun-name>` is used . This let
95+
you have control of PipelineRuns that gets only triggered by a comment on the
96+
pull request.
97+
98+
## Accessing the comment triggering the PipelineRun
99+
100+
When you trigger a PipelineRun via a Gitops Command, the template variable `{{
101+
trigger_comment }}` get set to the actual comment that triggered it.
102+
103+
You can then do some action based on for example the comment content with a
104+
shell script or others.
105+
106+
There is a restriction with the `trigger_comment` variable, we modify it to
107+
replace the newline with a `\n` since the multi-line comment can cause a issue
108+
when replaced inside the yaml.
109+
110+
It is up to you to replace it back with newlines, for example with shell scripts
111+
you can use `echo -e` to expand the newline back.
112+
113+
Example of a shell script:
114+
115+
```shell
116+
echo -e "{{ trigger_comment }}" > /tmp/comment
117+
grep "string" /tmp/comment
118+
```
119+
120+
## Custom GitOps commands
121+
122+
Using the [on-comment]({{< relref
123+
"/docs/guide/authoringprs.md#matching-a-pipelinerun-on-a-regexp-in-a-comment"
124+
>}}) annotation on your `PipelineRun` you can define custom GitOps commands that
125+
will be triggered by the comments on the pull request.
126+
127+
See the [on-comment]({{< relref
128+
"/docs/guide/authoringprs.md#matching-a-pipelinerun-on-a-regexp-in-a-comment"
129+
>}}) guide for more detailed information.
130+
131+
## Cancelling a PipelineRun
132+
133+
You can cancel a running PipelineRun by commenting on the PullRequest.
134+
135+
For example if you want to cancel all your PipelinerRuns you can add a comment starting
136+
with `/cancel` and all PipelineRun attached to that pull request will be cancelled.
137+
138+
Example :
139+
140+
```text
141+
It seems the infra is down, so cancelling the pipelineruns.
142+
143+
/cancel
144+
```
145+
146+
If you have multiple `PipelineRun` and you want to target a specific
147+
`PipelineRun` you can use the `/cancel` comment with the PipelineRun name
148+
149+
Example :
150+
151+
```text
152+
roses are red, violets are blue. why to run the pipeline when the infra is down.
153+
154+
/cancel <pipelinerun-name>
155+
```
156+
157+
On GitHub App the status of the Pipeline will be set to `cancelled`.
158+
159+
![pipelinerun canceled](/images/pr-cancel.png)
160+
161+
### Cancelling the PipelineRun on push request
162+
163+
You can cancel a running PipelineRun by commenting on the commit.
164+
Here's how you can do it.
165+
166+
Example :
167+
168+
1. Use `/cancel` to cancel all PipeineRuns.
169+
2. Use `/cancel <pipelinerun-name>` to cancel a specific PipeineRun
170+
171+
**Note:**
172+
173+
When executing `GitOps` comments on a commit that exists in multiple branches
174+
within a push request, the branch with the latest commit will be used.
175+
176+
This means:
177+
178+
1. If a user specifies commands like `/cancel`
179+
without any argument in a comment on a branch,
180+
it will automatically target the **main** branch.
181+
182+
Examples :
183+
1. `/cancel`
184+
2. `/cancel <pipelinerun-name>`
185+
186+
2. If the user issues a command like `/cancel branch:test`,
187+
it will target the commit where the comment was made but use the **test** branch.
188+
189+
Examples :
190+
1. `/cancel branch:test`
191+
2. `/cancel <pipelinerun-name> branch:test`
192+
193+
In the GitHub App, the status of the Pipeline will be set to `cancelled`.
194+
195+
![GitOps Commits For Comments For PipelineRun Canceled](/images/gitops-comments-on-commit-cancel.png)
196+
197+
Please note that this feature is supported for the GitHub provider only.
198+
199+
## Passing parameters to GitOps commands as argument
200+
201+
{{< tech_preview "Passing parameters to GitOps commands as argument" >}}
202+
203+
When you issue a GitOps command, you can pass arguments to the command to
204+
redefine some the [standard]({{< relref "/docs/guide/authoringprs#dynamic-variables" >}})
205+
standard or [custom parameters]({{< relref "/docs/guide/customparams" >}})
206+
207+
For example you can do:
208+
209+
```text
210+
/test pipelinerun1 key=value
211+
```
212+
213+
and the custom parameter `key` if defined as custom parameter will be defined to `value`
214+
215+
If the comment does not start with a `/` it will not be parsed.
216+
217+
You can only override parameters from the standard or when set as custom
218+
parameters, you cannot pass arbitrary parameters that hasn't been defined
219+
previously.
220+
221+
You can pass those `key=value` anywhere in your comment and it will be parsed.
222+
223+
There is different format that can get accepted, which let you pass values with space or newlines:
224+
225+
* key=value
226+
* key="a value"
227+
* key="another \"value\" defined"
228+
* key="another
229+
value with newline"

0 commit comments

Comments
 (0)