|
| 1 | +# CI/CD Pipeline — Diagrams |
| 2 | + |
| 3 | +[<- Back to Diagram Index](../../guides/DIAGRAM_INDEX.md) |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +These diagrams show how continuous integration and continuous deployment pipelines automate code quality checks, testing, and deployment using GitHub Actions as the primary example. |
| 8 | + |
| 9 | +## Full CI/CD Pipeline Flow |
| 10 | + |
| 11 | +A CI/CD pipeline runs automatically on every push or pull request. Each stage acts as a gate: if one fails, later stages do not run, preventing broken code from reaching production. |
| 12 | + |
| 13 | +```mermaid |
| 14 | +flowchart LR |
| 15 | + PUSH["git push / PR opened"] --> LINT["Lint & Format<br/>ruff check .<br/>black --check ."] |
| 16 | + LINT -->|"Pass"| TYPE["Type Check<br/>mypy src/"] |
| 17 | + TYPE -->|"Pass"| TEST["Run Tests<br/>pytest --cov"] |
| 18 | + TEST -->|"Pass"| BUILD["Build<br/>docker build -t app ."] |
| 19 | + BUILD -->|"Pass"| DEPLOY_STAGING["Deploy to Staging<br/>Automatic"] |
| 20 | + DEPLOY_STAGING --> SMOKE["Smoke Tests<br/>Hit /health endpoint"] |
| 21 | + SMOKE -->|"Pass"| GATE{"Manual Approval?"} |
| 22 | + GATE -->|"Approved"| DEPLOY_PROD["Deploy to Production"] |
| 23 | + GATE -->|"Skip"| DONE["Pipeline Complete"] |
| 24 | + DEPLOY_PROD --> DONE |
| 25 | +
|
| 26 | + LINT -->|"Fail"| STOP1["Pipeline Stops<br/>Fix lint errors"] |
| 27 | + TYPE -->|"Fail"| STOP2["Pipeline Stops<br/>Fix type errors"] |
| 28 | + TEST -->|"Fail"| STOP3["Pipeline Stops<br/>Fix failing tests"] |
| 29 | + BUILD -->|"Fail"| STOP4["Pipeline Stops<br/>Fix build errors"] |
| 30 | +
|
| 31 | + style PUSH fill:#cc5de8,stroke:#9c36b5,color:#fff |
| 32 | + style LINT fill:#ffd43b,stroke:#f59f00,color:#000 |
| 33 | + style TYPE fill:#ffd43b,stroke:#f59f00,color:#000 |
| 34 | + style TEST fill:#4a9eff,stroke:#2670c2,color:#fff |
| 35 | + style BUILD fill:#ff922b,stroke:#e8590c,color:#fff |
| 36 | + style DEPLOY_STAGING fill:#51cf66,stroke:#27ae60,color:#fff |
| 37 | + style DEPLOY_PROD fill:#51cf66,stroke:#27ae60,color:#fff |
| 38 | + style STOP1 fill:#ff6b6b,stroke:#c92a2a,color:#fff |
| 39 | + style STOP2 fill:#ff6b6b,stroke:#c92a2a,color:#fff |
| 40 | + style STOP3 fill:#ff6b6b,stroke:#c92a2a,color:#fff |
| 41 | + style STOP4 fill:#ff6b6b,stroke:#c92a2a,color:#fff |
| 42 | +``` |
| 43 | + |
| 44 | +**Key points:** |
| 45 | +- Fast checks (lint, format) run first so you get quick feedback on simple mistakes |
| 46 | +- Each stage is a gate: failures stop the pipeline early, saving compute time |
| 47 | +- Staging deployment happens automatically; production may require manual approval |
| 48 | +- Smoke tests verify the deployed app actually starts and responds |
| 49 | + |
| 50 | +## GitHub Actions Workflow Structure |
| 51 | + |
| 52 | +A GitHub Actions workflow is a YAML file in `.github/workflows/`. It defines triggers, jobs, and steps. Jobs run in parallel by default; use `needs` to create dependencies. |
| 53 | + |
| 54 | +```mermaid |
| 55 | +flowchart TD |
| 56 | + subgraph TRIGGER ["Triggers (on:)"] |
| 57 | + PUSH_T["push:<br/>branches: [main]"] |
| 58 | + PR_T["pull_request:<br/>branches: [main]"] |
| 59 | + end |
| 60 | +
|
| 61 | + subgraph JOB_LINT ["Job: lint"] |
| 62 | + LINT_1["runs-on: ubuntu-latest"] |
| 63 | + LINT_2["Step: checkout code"] |
| 64 | + LINT_3["Step: setup-python"] |
| 65 | + LINT_4["Step: pip install ruff"] |
| 66 | + LINT_5["Step: ruff check ."] |
| 67 | + LINT_1 --> LINT_2 --> LINT_3 --> LINT_4 --> LINT_5 |
| 68 | + end |
| 69 | +
|
| 70 | + subgraph JOB_TEST ["Job: test"] |
| 71 | + TEST_1["runs-on: ubuntu-latest"] |
| 72 | + TEST_M["strategy: matrix<br/>python: [3.11, 3.12, 3.13]"] |
| 73 | + TEST_2["Step: checkout code"] |
| 74 | + TEST_3["Step: setup-python (matrix)"] |
| 75 | + TEST_4["Step: pip install -r requirements.txt"] |
| 76 | + TEST_5["Step: pytest --cov"] |
| 77 | + TEST_1 --> TEST_M --> TEST_2 --> TEST_3 --> TEST_4 --> TEST_5 |
| 78 | + end |
| 79 | +
|
| 80 | + subgraph JOB_DEPLOY ["Job: deploy"] |
| 81 | + DEP_1["runs-on: ubuntu-latest"] |
| 82 | + DEP_2["Step: checkout code"] |
| 83 | + DEP_3["Step: deploy to Railway/Render"] |
| 84 | + DEP_1 --> DEP_2 --> DEP_3 |
| 85 | + end |
| 86 | +
|
| 87 | + PUSH_T --> JOB_LINT |
| 88 | + PR_T --> JOB_LINT |
| 89 | + PUSH_T --> JOB_TEST |
| 90 | + PR_T --> JOB_TEST |
| 91 | + JOB_LINT -->|"needs: lint"| JOB_DEPLOY |
| 92 | + JOB_TEST -->|"needs: test"| JOB_DEPLOY |
| 93 | +
|
| 94 | + style TRIGGER fill:#cc5de8,stroke:#9c36b5,color:#fff |
| 95 | + style JOB_LINT fill:#ffd43b,stroke:#f59f00,color:#000 |
| 96 | + style JOB_TEST fill:#4a9eff,stroke:#2670c2,color:#fff |
| 97 | + style JOB_DEPLOY fill:#51cf66,stroke:#27ae60,color:#fff |
| 98 | +``` |
| 99 | + |
| 100 | +**Key points:** |
| 101 | +- Jobs run in parallel by default: `lint` and `test` start at the same time |
| 102 | +- `needs:` creates dependencies: `deploy` waits for both `lint` and `test` to pass |
| 103 | +- Matrix strategy runs the same steps across multiple Python versions simultaneously |
| 104 | +- Each job gets a fresh virtual machine: no state leaks between jobs |
| 105 | + |
| 106 | +## Sequence: Pull Request Lifecycle |
| 107 | + |
| 108 | +The full lifecycle from opening a PR to merging, showing how CI checks integrate with code review. |
| 109 | + |
| 110 | +```mermaid |
| 111 | +sequenceDiagram |
| 112 | + participant Dev as Developer |
| 113 | + participant GH as GitHub |
| 114 | + participant CI as GitHub Actions |
| 115 | + participant Rev as Reviewer |
| 116 | +
|
| 117 | + Dev->>GH: Open Pull Request |
| 118 | + GH->>CI: Trigger workflow (on: pull_request) |
| 119 | +
|
| 120 | + par Parallel Jobs |
| 121 | + CI->>CI: Job: lint (ruff, black) |
| 122 | + CI->>CI: Job: test (pytest, 3 Python versions) |
| 123 | + end |
| 124 | +
|
| 125 | + alt All checks pass |
| 126 | + CI-->>GH: Status: All checks passed |
| 127 | + GH-->>Dev: Green checkmarks |
| 128 | + Dev->>Rev: Request review |
| 129 | + Rev->>GH: Review changes |
| 130 | + Rev-->>Dev: Approve / Request changes |
| 131 | +
|
| 132 | + alt Approved |
| 133 | + Dev->>GH: Merge PR |
| 134 | + GH->>CI: Trigger deploy workflow (on: push to main) |
| 135 | + CI->>CI: Build and deploy to production |
| 136 | + CI-->>GH: Deployment successful |
| 137 | + end |
| 138 | + else Some checks fail |
| 139 | + CI-->>GH: Status: Checks failed |
| 140 | + GH-->>Dev: Red X marks |
| 141 | + Dev->>Dev: Fix issues locally |
| 142 | + Dev->>GH: Push fix commits |
| 143 | + GH->>CI: Re-trigger workflow |
| 144 | + end |
| 145 | +``` |
| 146 | + |
| 147 | +**Key points:** |
| 148 | +- CI runs automatically when a PR is opened and on every subsequent push to the PR branch |
| 149 | +- Merge is blocked until all required checks pass (configurable in branch protection rules) |
| 150 | +- Deploying on merge to `main` means every merged PR goes to production automatically |
| 151 | +- Failed checks give immediate feedback: fix and push again to re-trigger |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +| [Back to Diagram Index](../../guides/DIAGRAM_INDEX.md) | |
| 156 | +|:---:| |
0 commit comments