|
| 1 | +// This module is included in the following assembly: |
| 2 | +// |
| 3 | +// *cicd/pipelines/using-pipelines-as-code.adoc |
| 4 | + |
| 5 | +:_content-type: REFERENCE |
| 6 | +[id="creating-pipeline-run-using-pipelines-as-code_{context}"] |
| 7 | += Creating a pipeline run using {pac} |
| 8 | + |
| 9 | +[role="_abstract"] |
| 10 | +To run pipelines using {pac}, you can create pipelines definitions or templates as YAML files in the `.tekton/` directory of the repository. You can reference YAML files in other repositories using remote URLs, but pipeline runs are only triggered by events in the repository containing the `.tekton/` directory. |
| 11 | + |
| 12 | +The {pac} resolver bundles the pipeline runs with all tasks as a single pipeline run without any external dependencies. |
| 13 | + |
| 14 | +[NOTE] |
| 15 | +==== |
| 16 | +* For pipelines, use at least one pipeline run with a spec, or a separated `Pipeline` object. |
| 17 | +* For tasks, embed task spec inside a pipeline, or define separately as a Task object. |
| 18 | +==== |
| 19 | + |
| 20 | +[discrete] |
| 21 | +.Parameterizing commits and URLs |
| 22 | + |
| 23 | +You can specify the parameters of your commit and URL by using dynamic, expandable variables with the {{<var>}} format. Currently, you can use the following variables: |
| 24 | + |
| 25 | +* `{{repo_owner}}`: The repository owner. |
| 26 | +* `{{repo_name}}`: The repository name. |
| 27 | +* `{{repo_url}}`: The repository full URL. |
| 28 | +* `{{revision}}`: Full SHA revision of a commit. |
| 29 | +* `{{sender}}`: The username or account id of the sender of the commit. |
| 30 | +* `{{source_branch}}`: The branch name where the event originated. |
| 31 | +* `{{target_branch}}`: The branch name that the event targets. For push events, it's the same as the `source_branch`. |
| 32 | +* `{{pull_request_number}}`: The pull or merge request number, defined only for a `pull_request` event type. |
| 33 | +* `{{git_auth_secret}}`: The secret name that is generated automatically with Git provider's token for checking out private repos. |
| 34 | +
|
| 35 | +[discrete] |
| 36 | +.Matching an event to a pipeline run |
| 37 | + |
| 38 | +You can match different Git provider events with each pipeline by using special annotations on the pipeline run. If there are multiple pipeline runs matching an event, {pac} runs them in parallel and posts the results to the Git provider as soon a pipeline run finishes. |
| 39 | + |
| 40 | +[discrete] |
| 41 | +.Matching a pull event to a pipeline run |
| 42 | + |
| 43 | +You can use the following example to match the `pipeline-pr-main` pipeline with a `pull_request` event that targets the `main` branch: |
| 44 | + |
| 45 | +[source,yaml] |
| 46 | +---- |
| 47 | +... |
| 48 | + metadata: |
| 49 | + name: pipeline-pr-main |
| 50 | + annotations: |
| 51 | + pipelinesascode.tekton.dev/on-target-branch: "[main]" <1> |
| 52 | + pipelinesascode.tekton.dev/on-event: "[pull_request]" |
| 53 | +... |
| 54 | +---- |
| 55 | +<1> You can specifiy multiple branches by adding comma-separated entries. For example, `"[main, release-nightly]"`. In addition, you can specify the following: |
| 56 | +* Full references to branches such as `"refs/heads/main"` |
| 57 | +* Globs with pattern matching such as `"refs/heads/\*"`, |
| 58 | +* Tags such as `"refs/tags/1.\*"`. |
| 59 | + |
| 60 | +[discrete] |
| 61 | +.Matching a push event to a pipeline run |
| 62 | + |
| 63 | +You can use the following example to match the `pipeline-push-on-main` pipeline with a `push` event targeting the `refs/heads/main` branch: |
| 64 | + |
| 65 | +[source,yaml] |
| 66 | +---- |
| 67 | +... |
| 68 | + metadata: |
| 69 | + name: pipeline-push-on-main |
| 70 | + annotations: |
| 71 | + pipelinesascode.tekton.dev/on-target-branch: "[refs/heads/main]" <1> |
| 72 | + pipelinesascode.tekton.dev/on-event: "[push]" |
| 73 | +... |
| 74 | +---- |
| 75 | +<1> You can specifiy multiple branches by adding comma-separated entries. For example, `"[main, release-nightly]"`. In addition, you can specify the following: |
| 76 | +* Full references to branches such as `"refs/heads/main"` |
| 77 | +* Globs with pattern matching such as `"refs/heads/\*"`, |
| 78 | +* Tags such as `"refs/tags/1.\*"`. |
| 79 | + |
| 80 | +[discrete] |
| 81 | +.Advanced event matching |
| 82 | + |
| 83 | +{pac} supports using Common Expression Language (CEL) based filtering for advanced event matching. If you have the `pipelinesascode.tekton.dev/on-cel-expression` annotation in your pipeline run, {pac} uses the CEL expression and skips the `on-target-branch` annotation. Compared to the simple `on-target-branch` annotation matching, the CEL expressions allows complex filtering and negation. |
| 84 | + |
| 85 | +To use CEL based filtering with {pac}, consider the following examples of annotations: |
| 86 | + |
| 87 | +* To match a `pull_request` event targeting the `main` branch and coming from the `wip` branch: |
| 88 | ++ |
| 89 | +[source,yaml] |
| 90 | +---- |
| 91 | +... |
| 92 | + pipelinesascode.tekton.dev/on-cel-expression: | |
| 93 | + event == "pull_request" && target_branch == "main" && source_branch == "wip" |
| 94 | +... |
| 95 | +---- |
| 96 | +
|
| 97 | +* To run a pipeline only if a path has changed, you can use the `.pathChanged` suffix function with a glob pattern: |
| 98 | ++ |
| 99 | +[source,yaml] |
| 100 | +---- |
| 101 | +... |
| 102 | + pipelinesascode.tekton.dev/on-cel-expression: | |
| 103 | + event == "pull_request" && "docs/\*.md".pathChanged() <1> |
| 104 | +... |
| 105 | +---- |
| 106 | +<1> Matches all markdown files in the `docs` directory. |
| 107 | +
|
| 108 | +* To match all pull requests starting with the title `[DOWNSTREAM]`: |
| 109 | ++ |
| 110 | +[source,yaml] |
| 111 | +---- |
| 112 | +... |
| 113 | + pipelinesascode.tekton.dev/on-cel-expression: | |
| 114 | + event == "pull_request && event_title.startsWith("[DOWNSTREAM]") |
| 115 | +... |
| 116 | +---- |
| 117 | +
|
| 118 | +* To run a pipeline on a `pull_request` event, but skipping the `experimental` branch: |
| 119 | ++ |
| 120 | +[source,yaml] |
| 121 | +---- |
| 122 | +... |
| 123 | + pipelinesascode.tekton.dev/on-cel-expression: | |
| 124 | + event == "pull_request" && target_branch != experimental" |
| 125 | +... |
| 126 | +---- |
| 127 | +
|
| 128 | +For advanced CEL based filtering while using {pac}, you can use the following fields and suffix functions: |
| 129 | + |
| 130 | +* `event`: A `push` or `pull_request` event. |
| 131 | +* `target_branch`: The target branch. |
| 132 | +* `source_branch`: The branch of origin of a `pull_request` event. For `push` events, it is same as the `target_branch`. |
| 133 | +* `event_title`: Matches the title of the event, such as the commit title for a `push` event, and the title of a pull or merge request for a `pull_request` event. Currently, only GitHub, Gitlab, and BitbucketCloud are the supported providers. |
| 134 | +* `.pathChanged`: A suffix function to a string. The string can be a glob of a path to check if the path has changed. Currently, only GitHub and Gitlab are supported as providers. |
| 135 | +
|
| 136 | +[discrete] |
| 137 | +.Using the temporary Github App token for Github API operations |
| 138 | + |
| 139 | +You can use the temporary installation token generated by {pac} from Github App to access the Github API. The token value is stored in the temporary `{{git_auth_secret}}` dynamic variable as generated for private repositories in the `git-provider-token` key. |
| 140 | + |
| 141 | +For example, to add a comment to a pull request, you can use the `github-add-comment` task from {tekton-hub} using a {pac} annotation: |
| 142 | + |
| 143 | +[source,yaml] |
| 144 | +---- |
| 145 | +... |
| 146 | + pipelinesascode.tekton.dev/task: "github-add-comment" |
| 147 | +... |
| 148 | +---- |
| 149 | + |
| 150 | +You can then add a task to the `tasks` section or `finally` tasks in the pipeline run definition: |
| 151 | + |
| 152 | +[source,yaml] |
| 153 | +---- |
| 154 | +[...] |
| 155 | +tasks: |
| 156 | + - name: |
| 157 | + taskRef: |
| 158 | + name: github-add-comment |
| 159 | + params: |
| 160 | + - name: REQUEST_URL |
| 161 | + value: "{{ repo_url }}/pull/{{ pull_request_number }}" <1> |
| 162 | + - name: COMMENT_OR_FILE |
| 163 | + value: "Pipelines as Code IS GREAT!" |
| 164 | + - name: GITHUB_TOKEN_SECRET_NAME |
| 165 | + value: "{{ git_auth_secret }}" |
| 166 | + - name: GITHUB_TOKEN_SECRET_KEY |
| 167 | + value: "git-provider-token" |
| 168 | +... |
| 169 | +---- |
| 170 | +<1> By using the dynamic variables, you can reuse this snippet template for any pull request from any repository. |
| 171 | + |
| 172 | +[NOTE] |
| 173 | +==== |
| 174 | +On GitHub apps, the generated installation token is available for 8 hours, and scoped to the repository from here the events originate, unless configured differently on the cluster. |
| 175 | +==== |
| 176 | + |
0 commit comments