|
| 1 | +--- |
| 2 | +weight: 20 |
| 3 | +i18n: |
| 4 | + title: |
| 5 | + en: PAC Core Concepts |
| 6 | + zh: PAC æ ¸å¿ƒæ¦‚å¿µ |
| 7 | +title: PAC Core Concepts |
| 8 | +--- |
| 9 | + |
| 10 | +# PAC Core Concepts |
| 11 | + |
| 12 | +:::info For All Users |
| 13 | + |
| 14 | +This document provides foundational concepts for both **administrators** and **regular users** to understand how PAC works. |
| 15 | + |
| 16 | +::: |
| 17 | + |
| 18 | +This document provides a detailed overview of the core concepts behind Pipelines-as-Code (PAC), its architecture, and how it integrates with Kubernetes and Git providers. |
| 19 | + |
| 20 | +## What is Pipelines as Code? |
| 21 | + |
| 22 | +Pipelines-as-Code (PAC) is a component that enables you to define and manage Tekton Pipeline workflows directly in your source code repository. Instead of maintaining pipelines in the Kubernetes cluster, you can: |
| 23 | + |
| 24 | +- Store pipeline definitions in Git alongside your code |
| 25 | +- Version control pipeline configurations |
| 26 | +- Review pipeline changes through Merge Requests |
| 27 | +- Trigger pipelines automatically from Git events |
| 28 | + |
| 29 | +## Architecture Overview |
| 30 | + |
| 31 | +PAC consists of several key components that work together: |
| 32 | + |
| 33 | + ```mermaid |
| 34 | + flowchart TD |
| 35 | + A[Git Provider<br/>GitHub/GitLab/Bitbucket] -->|Webhook Events| B[PAC Webhook<br/>Receiver] |
| 36 | + B --> C[PAC Controller<br/>Processor] |
| 37 | + C -->|Creates PipelineRuns| D[Tekton Pipeline<br/>Executor] |
| 38 | + D --> E[PAC Watcher<br/>Status Reporter] |
| 39 | + E -->|Reports Status| A |
| 40 | + |
| 41 | + style A fill:#e1f5ff |
| 42 | + style B fill:#fff4e1 |
| 43 | + style C fill:#fff4e1 |
| 44 | + style D fill:#e8f5e9 |
| 45 | + style E fill:#fff4e1 |
| 46 | + ``` |
| 47 | + |
| 48 | +## Core Components |
| 49 | + |
| 50 | +### 1. Repository concept |
| 51 | + |
| 52 | +#### 1.1. Repository Custom Resource |
| 53 | + |
| 54 | +The Repository Custom Resource defines the connection between a Git repository and the PAC controller. |
| 55 | + |
| 56 | +**Key Fields**: |
| 57 | + |
| 58 | +- `spec.url`: Git repository URL |
| 59 | +- `spec.git_provider`: Git provider configuration (type, URL, credentials) |
| 60 | +- `spec.webhook`: Webhook configuration (secret for validation) |
| 61 | + |
| 62 | +**Example**: |
| 63 | + |
| 64 | + ```yaml |
| 65 | + apiVersion: pipelinesascode.tekton.dev/v1alpha1 |
| 66 | + kind: Repository |
| 67 | + metadata: |
| 68 | + name: my-repo |
| 69 | + namespace: project-pipelines |
| 70 | + spec: |
| 71 | + url: "https://gitlab.com/user/repo.git" |
| 72 | + git_provider: |
| 73 | + type: gitlab |
| 74 | + url: "https://gitlab.com" |
| 75 | + secret: |
| 76 | + name: gitlab-secret |
| 77 | + key: token |
| 78 | + webhook_secret: |
| 79 | + name: webhook-secret |
| 80 | + key: secret |
| 81 | + ``` |
| 82 | +
|
| 83 | +### 2. PAC Controller |
| 84 | +
|
| 85 | +The PAC Controller is the main component that: |
| 86 | +
|
| 87 | +- Receives webhook events from Git providers |
| 88 | +- Fetches pipeline definitions from Git repositories |
| 89 | +- Creates PipelineRun resources in Kubernetes |
| 90 | +- Manages Repository CRs (Custom Resources) |
| 91 | +
|
| 92 | +**Deployment**: Deployed as a Kubernetes Deployment in a namespace (default: `tekton-pipelines`, but can be customized via `targetNamespace` in the `OpenShiftPipelinesAsCode` CR). |
| 93 | + |
| 94 | +### 3. PAC Watcher |
| 95 | + |
| 96 | +The PAC Watcher monitors PipelineRun execution and: |
| 97 | + |
| 98 | +- Tracks pipeline status (pending, running, success, failure) |
| 99 | +- Reports status back to the Git provider |
| 100 | +- Updates commit status and Pull Request/Merge Request checks |
| 101 | + |
| 102 | +**Deployment**: Deployed as a Kubernetes Deployment in a namespace (default: `tekton-pipelines`, but can be customized via `targetNamespace` in the `OpenShiftPipelinesAsCode` CR). |
| 103 | + |
| 104 | +### 4. PAC Webhook |
| 105 | + |
| 106 | +The PAC Webhook receives HTTP requests from Git providers: |
| 107 | + |
| 108 | +- Validates webhook signatures |
| 109 | +- Processes webhook payloads |
| 110 | +- Forwards events to the PAC Controller |
| 111 | + |
| 112 | +**Deployment**: Deployed as a Kubernetes Service and optionally exposed via Ingress/NodePort. |
| 113 | + |
| 114 | +## How It Works |
| 115 | + |
| 116 | +### 1. Repository Registration |
| 117 | + |
| 118 | +When you configure a repository using `tkn pac create repo`: |
| 119 | + |
| 120 | +1. A Repository CR is created in your Kubernetes cluster |
| 121 | +2. A webhook is configured in your Git provider pointing to the PAC controller |
| 122 | +3. A Kubernetes Secret is created with Git provider credentials |
| 123 | +4. A basic `.tekton/pipelinerun.yaml` template is generated |
| 124 | + |
| 125 | +### 2. Event Flow |
| 126 | + |
| 127 | +[PAC Event Flow Diagram](https://pipelinesascode.com/docs/flows/diagram/) |
| 128 | + |
| 129 | +When a Git event occurs (push, pull request, merge request, comment): |
| 130 | + |
| 131 | +1. **Git provider sends webhook** → PAC Webhook receives the event |
| 132 | +2. **Webhook validation** → PAC validates the webhook signature |
| 133 | +3. **Event processing** → PAC Controller processes the event |
| 134 | +4. **Repository lookup** → Controller finds matching Repository CR |
| 135 | +5. **Pipeline fetch** → Controller fetches pipeline definitions from Git |
| 136 | +6. **PipelineRun creation** → Controller creates PipelineRun in Kubernetes |
| 137 | +7. **Status monitoring** → PAC Watcher monitors PipelineRun execution |
| 138 | +8. **Status reporting** → Watcher reports status back to the Git provider |
| 139 | + |
| 140 | +### 3. Pipeline Execution |
| 141 | + |
| 142 | +1. PAC Controller creates a PipelineRun based on the pipeline definition |
| 143 | +2. Tekton Pipeline controller picks up the PipelineRun |
| 144 | +3. Pipeline tasks are executed in sequence |
| 145 | +4. PAC Watcher monitors the execution |
| 146 | +5. Status is reported back to the Git provider |
| 147 | + |
| 148 | +## Platform Support |
| 149 | + |
| 150 | +### Kubernetes |
| 151 | + |
| 152 | +PAC can be deployed on standard Kubernetes clusters through the Tekton Operator. Although the resource name contains "OpenShift", a patch enables PAC controller support on Kubernetes. |
| 153 | + |
| 154 | +**Deployment**: Create the `OpenShiftPipelinesAsCode` CR directly. |
| 155 | + |
| 156 | +## Git Provider Integration |
| 157 | + |
| 158 | +PAC supports multiple Git providers including GitHub, GitLab, Bitbucket Cloud, and Bitbucket Data Center. Each provider has specific integration features: |
| 159 | + |
| 160 | +**Webhook Configuration**: |
| 161 | + |
| 162 | +- URL: PAC controller endpoint |
| 163 | +- Secret: Webhook secret for validation |
| 164 | +- Events: Push, Pull Request/Merge Request, Comments |
| 165 | + |
| 166 | +**Status Reporting**: |
| 167 | + |
| 168 | +- Commit status updates |
| 169 | +- Pull Request/Merge Request status checks |
| 170 | +- Comment-based commands |
| 171 | + |
| 172 | +**Authentication**: |
| 173 | + |
| 174 | +- Personal Access Token or App tokens with appropriate scopes (varies by provider) |
| 175 | +- Stored in Kubernetes Secret |
| 176 | + |
| 177 | +For provider-specific configuration details, see: |
| 178 | +- [Configure Repository](../how_to/configure_gitlab_repo.mdx) |
| 179 | +- Provider-specific guides in the documentation |
| 180 | + |
| 181 | +## Pipeline Definition |
| 182 | + |
| 183 | +### File Location |
| 184 | + |
| 185 | +By default, PAC looks for pipeline definitions in: |
| 186 | + |
| 187 | +- `.tekton/pipelinerun.yaml` (all .tekton/\*.yaml manifests of PipelineRun will be processed) |
| 188 | +- All `.yaml` and `.yml` files in the `.tekton/` directory |
| 189 | + |
| 190 | +### Pipeline Structure |
| 191 | + |
| 192 | +A PAC pipeline is a standard Tekton PipelineRun with PAC-specific annotations: |
| 193 | + |
| 194 | + ```yaml |
| 195 | + apiVersion: tekton.dev/v1 |
| 196 | + kind: PipelineRun |
| 197 | + metadata: |
| 198 | + name: my-pipeline |
| 199 | + annotations: |
| 200 | + pipelinesascode.tekton.dev/on-target-branch: "[refs/heads/main]" |
| 201 | + pipelinesascode.tekton.dev/on-event: "[push]" |
| 202 | + spec: |
| 203 | + pipelineSpec: |
| 204 | + tasks: |
| 205 | + - name: hello |
| 206 | + taskSpec: |
| 207 | + steps: |
| 208 | + - name: echo |
| 209 | + image: alpine:latest |
| 210 | + script: | |
| 211 | + echo "Hello from PAC!" |
| 212 | + ``` |
| 213 | + |
| 214 | +### Event Annotations |
| 215 | + |
| 216 | +PAC uses annotations to determine when to trigger pipelines: |
| 217 | + |
| 218 | +- `pipelinesascode.tekton.dev/on-target-branch`: Target branch for the event (e.g., `"[main]"` or `"[refs/heads/main]"`) |
| 219 | +- `pipelinesascode.tekton.dev/on-event`: Event type (e.g., `"[push]"` or `"[pull_request]"`) |
| 220 | +- `pipelinesascode.tekton.dev/on-comment`: Trigger on comment commands |
| 221 | + |
| 222 | +For more details, see [Event Annotations](../how_to/maintain_pipeline_code.mdx#event-annotations). |
| 223 | + |
| 224 | +## Variables and Context |
| 225 | + |
| 226 | +PAC provides dynamic variables that can be used in pipeline definitions. Variables use the `{{<var>}}` format and are automatically expanded by PAC when creating PipelineRuns. |
| 227 | + |
| 228 | +For more details, see [Parameterizing Commits and URLs](../how_to/maintain_pipeline_code.mdx#parameterizing-commits-and-urls). |
| 229 | + |
| 230 | +### Git Context Variables |
| 231 | + |
| 232 | +- `{{repo_url}}`: Repository full URL |
| 233 | +- `{{revision}}`: Full SHA revision of a commit |
| 234 | +- `{{source_branch}}`: Branch name where the event originated |
| 235 | +- `{{target_branch}}`: Branch name that the event targets |
| 236 | +- `{{repo_owner}}`: Repository owner |
| 237 | +- `{{repo_name}}`: Repository name |
| 238 | + |
| 239 | +### Event Context Variables |
| 240 | + |
| 241 | +- `{{sender}}`: Username or account ID of the event sender |
| 242 | + |
| 243 | +### Pull Request / Merge Request Context Variables |
| 244 | + |
| 245 | +- `{{pull_request_number}}`: Pull Request or Merge Request number (available for pull_request events only) |
| 246 | + |
| 247 | +For a complete list of available variables and their usage, see [Parameterizing Commits and URLs](../how_to/maintain_pipeline_code.mdx#parameterizing-commits-and-urls). |
| 248 | + |
| 249 | +## Task Resolution |
| 250 | + |
| 251 | +PAC can automatically resolve tasks from multiple sources: |
| 252 | + |
| 253 | +1. **Local Tasks**: Tasks defined in the repository |
| 254 | +2. **Tekton Hub**: Tasks from Tekton Hub catalog |
| 255 | +3. **Remote URLs**: Tasks from remote Git repositories or URLs |
| 256 | + |
| 257 | +For more details, see [PAC Resolver](../how_to/pac_resolver.mdx). |
| 258 | + |
| 259 | +## Security Considerations |
| 260 | + |
| 261 | +### Webhook Security |
| 262 | + |
| 263 | +- Webhook secrets validate incoming requests |
| 264 | +- Secrets are stored in Kubernetes Secrets |
| 265 | +- Git providers validate webhook signatures |
| 266 | + |
| 267 | +### Access Control |
| 268 | + |
| 269 | +- Repository CRs are namespace-scoped |
| 270 | +- RBAC controls who can create Repository CRs |
| 271 | +- Git provider tokens have limited scopes for security |
| 272 | + |
| 273 | +### Pipeline Permissions |
| 274 | + |
| 275 | +- PipelineRuns run in specified namespaces |
| 276 | +- ServiceAccounts control pipeline permissions |
| 277 | +- Secrets are mounted as needed |
| 278 | + |
| 279 | +## Best Practices |
| 280 | + |
| 281 | +### 1. Repository Organization |
| 282 | + |
| 283 | +- Keep pipeline definitions in `.tekton/` directory |
| 284 | +- Use descriptive pipeline names |
| 285 | +- Version control all pipeline changes |
| 286 | + |
| 287 | +### 2. Event Filtering |
| 288 | + |
| 289 | +- Use specific branch patterns |
| 290 | +- Use path filtering to limit triggers |
| 291 | +- Separate merge request and push pipelines |
| 292 | + |
| 293 | +### 3. Task Management |
| 294 | + |
| 295 | +- Use Tekton Hub tasks when possible |
| 296 | +- Create reusable task definitions |
| 297 | +- Version control task definitions |
| 298 | + |
| 299 | +### 4. Security |
| 300 | + |
| 301 | +- Don't hardcode secrets in pipeline files |
| 302 | +- Use Kubernetes Secrets |
| 303 | +- Limit pipeline permissions |
| 304 | + |
| 305 | +## Troubleshooting |
| 306 | + |
| 307 | +### Common Issues |
| 308 | + |
| 309 | +1. **Pipeline not triggering**: Check annotations and branch names |
| 310 | +2. **Webhook not received**: Verify webhook URL and secret |
| 311 | +3. **Tasks not found**: Check task references and network connectivity |
| 312 | +4. **Status not reported**: Verify Git provider token permissions |
| 313 | + |
| 314 | +For detailed troubleshooting, see [Common Issues](../trouble_shooting/common_issues.mdx). |
| 315 | + |
| 316 | +## Next Steps |
| 317 | + |
| 318 | +- [Quick Start](../quick_start.mdx) - Get started with PAC |
| 319 | +- [Manage PAC Component](../configure/manage_pac.mdx) - Deploy and manage PAC |
| 320 | +- [Configure Repository](../how_to/configure_gitlab_repo.mdx) - Set up repository integration |
| 321 | +- [Maintain Pipeline Code](../how_to/maintain_pipeline_code.mdx) - Define pipelines in code |
| 322 | + |
| 323 | +## Related Documentation |
| 324 | + |
| 325 | +- [Pipeline As Code](https://pipelinesascode.com/) - Pipelines As Code Official Documentation |
| 326 | + |
0 commit comments