|
| 1 | +// This module is included in the following assemblies: |
| 2 | +// * pac/creating-pipeline-runs-pac.adoc |
| 3 | + |
| 4 | +:_mod-docs-content-type: PROCEDURE |
| 5 | +[id="creating-pipeline-run-pac_{context}"] |
| 6 | += Creating a pipeline run in {pac} |
| 7 | + |
| 8 | +You can create a pipeline run definition for {pac} in your repository and match it to an event, such as a pull request. When the event happens, {pac} creates a `PipelineRun` custom resource (CR) from this definition, and then {pipelines-shortname} executes the pipeline run. |
| 9 | + |
| 10 | +.Prerequisites |
| 11 | + |
| 12 | +* You configured {pac} to integrate with your Git repository hosting service provider. |
| 13 | +
|
| 14 | +* You created a `Repository` custom resource (CR) to define the connection to your repository in {pac}. |
| 15 | +
|
| 16 | +* You have a `.tekton` directory in the root of your repository. |
| 17 | +
|
| 18 | +.Procedure |
| 19 | + |
| 20 | +. Create a file with a `.yaml` or `.yml` extension in the `.tekton` directory. |
| 21 | + |
| 22 | +. In the file that you created, create a YAML specification for a `PipelineRun` CR. This specification can use all features of {pipelines-shortname}. |
| 23 | + |
| 24 | +. Depending on the requirements of your pipeline run definition, complete any of the following optional steps: |
| 25 | + |
| 26 | +** Create other files with a `.yaml` or `.yml` extension in the `.tekton` directory. In these files, provide definitions of resources that your pipeline run definition references, such as `Pipeline`, `Task`, or `StepAction` CRs. The {pac} resolver automatically resolves these resources and includes them in the `PipelineRun` CR that is based on your definition. |
| 27 | + |
| 28 | +** Use _dynamic variables_ in your pipeline run specification. {pac} replaces these variables with values that represent the current context. For example, `{{ repo_url }}` is the current URL for the repository and `{{ revision }}` is the commit SHA on which the pipeline run was started. |
| 29 | ++ |
| 30 | +For more information about dynamic variables, see the "Dynamic variables in a pipeline run specification" section. |
| 31 | + |
| 32 | +** Add one or more {pac} resolver annotations to your pipeline run definition. A {pac} resolver annotation references a pipeline or task in {tekton-hub}, an HTTP location, or a location in your repository outside the `.tekton` directory. If you create a {pac} resolver annotation to reference a resource, you can use this resource by name in your pipeline run definition. The {pac} resolver automatically resolves these resources and includes them in the `PipelineRun` CR that is based on your definition. |
| 33 | ++ |
| 34 | +For more information about these annotations, see the "{pac} resolver annotations" section. |
| 35 | + |
| 36 | +. Match the pipeline run to events by adding any of the following annotations to the pipeline run definition. When the defined event happens, {pac} starts the pipeline run. For more information about matching the pipeline run to events, see the "Annotations for matching events to a pipeline run" section. |
| 37 | + |
| 38 | +** A combination of the `pipelinesascode.tekton.dev/on-event` annotation, which defines a `pull request` or `push` event, and a `pipelinesascode.tekton.dev/on-target-branch` annotation, which defines the branch that the pull request or push event must target. If you match your pipeline run to a pull request or push event, the pipeline run starts when the event is created. For pull requests, it starts again each time the source branch event is updated. |
| 39 | ++ |
| 40 | +[NOTE] |
| 41 | +==== |
| 42 | +If your Git repository provider uses merge requests and not pull requests, a `pull_request` event definition matches a merge request. |
| 43 | +==== |
| 44 | + |
| 45 | +** A `pipelinesascode.tekton.dev/on-comment` annotation, which matches the pipeline run to a comment on a pull request by regular expression. If you match your pipeline run to a comment, it starts when the comment is added to a pull request. To start the pipeline run again, add the comment again. |
| 46 | + |
| 47 | +** A `pipelinesascode.tekton.dev/on-cel-expression` annotation, which matches the pipeline run if the specified Common Expression Language (CEL) expression evaluates to `true` on a pull request or push event. |
| 48 | + |
| 49 | +. Optional: Add one or more annotations that filter the matching events. With these annotations, when the defined matching event (such as a pull request, a push event, or a comment) happens, {pac} checks if these annotations are also matched. The pipeline run starts only if all the annotations that you added to it are matched. For more information about filtering matching events, see the "Annotations for filtering events" section. |
| 50 | + |
| 51 | +** A `pipelinesascode.tekton.dev/on-path-changed` annotation is matched if the pull request or push event affect files in the specified paths. |
| 52 | + |
| 53 | +** A `pipelinesascode.tekton.dev/on-path-changed-ignore` annotation excludes matching the event if the event changes _only_ files in the specified paths and does not change any other files in the repository. |
| 54 | + |
| 55 | +** A `pipelinesascode.tekton.dev/on-label` annotation is matched if the pull request or push event has one of the specified labels. |
| 56 | + |
| 57 | +. Optional: Add the `pipelinesascode.tekton.dev/cancel-in-progress: "true"` annotation to enable automatic cancellation of the pipeline run in certain cases. For example, if a pull request triggers a pipeline run and then the user pushes new commits into the pull request source branch, each push triggers a new copy of the pipeline run. if you enable automatic cancellation-in-progress, after a new copy of the pipeline run starts, {pac} cancels the older run to avoid running many copies of the pipeline run at the same time. For more information about this annotation, see the "Annotations for specifying automatic cancellation-in-progress for a pipeline run" section. |
| 58 | + |
| 59 | +.Example {pac} pipeline run definition |
| 60 | +[source,yaml] |
| 61 | +---- |
| 62 | +apiVersion: tekton.dev/v1 |
| 63 | +kind: PipelineRun |
| 64 | +metadata: |
| 65 | + name: maven-build |
| 66 | +annotations: |
| 67 | + pipelinesascode.tekton.dev/task: "[git-clone]" # <1> |
| 68 | + pipelinesascode.tekton.dev/on-event: "[pull_request]" # <2> |
| 69 | + pipelinesascode.tekton.dev/on-target-branch: "[main, release]" # <3> |
| 70 | + pipelinesascode.tekton.dev/on-path-changed: "[src/**]" # <4> |
| 71 | + pipelinesascode.tekton.dev/cancel-in-progress: "true" # <5> |
| 72 | +spec: |
| 73 | + pipelineSpec: |
| 74 | + workspaces: |
| 75 | + - name: shared-workspace |
| 76 | + tasks: |
| 77 | + - name: fetch-repo |
| 78 | + taskRef: |
| 79 | + - name: git-clone # <6> |
| 80 | + params: |
| 81 | + - name: url |
| 82 | + value: {{ repo_url }} # <7> |
| 83 | + - name: revision |
| 84 | + value: {{ revision }} # <8> |
| 85 | + workspaces: |
| 86 | + - name: output |
| 87 | + workspace: shared-workspace |
| 88 | + - name: build-from-source |
| 89 | + taskRef: |
| 90 | + resolver: cluster |
| 91 | + params: |
| 92 | + - name: kind |
| 93 | + value: task |
| 94 | + - name: name |
| 95 | + value: maven |
| 96 | + - name: namespace |
| 97 | + value: openshift-pipelines |
| 98 | + workspaces: |
| 99 | + - name: source |
| 100 | + workspace: shared-workspace |
| 101 | +---- |
| 102 | +<1> The `pipelinesascode.tekton.dev/task` annotation references the `git-clone` task from {tekton-hub}. |
| 103 | +<2> The `pipelinesascode.tekton.dev/on-event` annotation matches the pipeline run to a pull request or merge request event. |
| 104 | +<3> The `pipelinesascode.tekton.dev/on-target-branch` annotation specifies that a pull request into either the `main` branch or `release` branch triggers this pipeline run. |
| 105 | +<4> The `pipelinesascode.tekton.dev/on-path-changed` annotation specifies that the pipeline run triggers only if the pull request contains changes to files under the `src` directory. |
| 106 | +<5> The `pipelinesascode.tekton.dev/cancel-in-progress` annotation specifies that, if the pipeline run is started again for the same pull request, {pac} cancels the previous run. |
| 107 | +<6> The pipeline run specification references the `git-clone` task by name. Because of the `pipelinesascode.tekton.dev/task` annotation, the {pac} resolver resolves this reference to the `git-clone` task from {tekton-hub}. |
| 108 | +<7> {pac} replaces the `{{ repo_url }}` dynamic variable with the URL for the Git repository. |
| 109 | +<8> {pac} replaces the `{{ revision }}` dynamic variable with the revision of the branch for which the pipeline run was started. |
0 commit comments