|
| 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 | + |
| 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 | + |
| 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 | + |
| 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