|
| 1 | +# Semaphore CI pipeline for a non-Phoenix Elixir project |
| 2 | +# Spec: https://docs.semaphoreci.com/reference/pipeline-yaml |
| 3 | + |
1 | 4 | version: v1.0
|
2 |
| -name: "π« Elixir CI Pipeline" |
| 5 | +name: "π§ Elixir CI Pipeline" |
3 | 6 |
|
| 7 | +# ------------------------------------------------------------------- |
| 8 | +# Define the machine type and OS image to run the pipeline on |
| 9 | +# ------------------------------------------------------------------- |
4 | 10 | agent:
|
5 | 11 | machine:
|
6 |
| - type: {{ machine_type }} # Define the machine type (e.g., e1-standard-2 for cost-efficiency) |
7 |
| - os_image: {{ os_image }} # Specify the OS image (e.g., ubuntu2004 for compatibility) |
| 12 | + type: {{ machine_type }} |
| 13 | + os_image: {{ os_image }} |
8 | 14 |
|
9 | 15 | containers:
|
10 | 16 | - name: main
|
11 |
| - image: 'registry.semaphoreci.com/elixir:1.15.7' # Elixir 1.15.7 with Erlang/OTP 26 |
| 17 | + image: 'registry.semaphoreci.com/elixir:1.16' # Elixir 1.16 with Erlang/OTP 26 |
12 | 18 | - name: postgres
|
13 | 19 | image: 'registry.semaphoreci.com/postgres:17' # PostgreSQL 17 for database operations
|
14 | 20 |
|
| 21 | +# Configure when to stop the pipeline early |
15 | 22 | fail_fast:
|
16 | 23 | stop:
|
17 |
| - when: branch != 'main' # Stop execution early for non-main branches to save resources |
| 24 | + when: branch != 'main' # Stop all blocks if a job fails on non-main branches |
18 | 25 | auto_cancel:
|
19 | 26 | running:
|
20 | 27 | when: branch != 'main' # Cancel running pipelines on non-main branches
|
21 | 28 | queued:
|
22 |
| - when: branch = 'main' # Cancel queued pipelines on the main branch |
| 29 | + when: branch = 'main' # Cancel queued pipelines on main branch |
23 | 30 |
|
| 31 | +# ------------------------------------------------------------------- |
| 32 | +# GLOBAL JOB CONFIG β runs before every job |
| 33 | +# Docs: https://docs.semaphoreci.com/reference/pipeline-yaml#global-job-config |
| 34 | +# Keep only commands truly needed everywhere |
| 35 | +# ------------------------------------------------------------------- |
24 | 36 | global_job_config:
|
25 | 37 | prologue:
|
26 | 38 | commands:
|
27 |
| - - checkout # Fetch the latest code from the repository |
28 |
| - - cache restore # Restore cached dependencies for faster builds |
29 |
| - - mix local.hex --force # Install Hex, the Elixir package manager |
30 |
| - - mix local.rebar --force # Install rebar3, a build tool for Erlang/Elixir |
31 |
| - - mix deps.get # Fetch project dependencies |
| 39 | + - checkout # Clone repo β https://docs.semaphoreci.com/reference/toolbox#checkout |
| 40 | + - mix local.hex --force |
| 41 | + - mix local.rebar --force |
| 42 | + - cache restore # Reuse deps/ or _build cache |
32 | 43 |
|
| 44 | +# ------------------------------------------------------------------- |
| 45 | +# BLOCK: π¦ Install & Build β compile & cache artifacts |
| 46 | +# ------------------------------------------------------------------- |
33 | 47 | blocks:
|
34 |
| - - name: "π οΈ Setup and Cache" |
| 48 | + - name: "π¦ Install & Build" |
35 | 49 | dependencies: []
|
36 | 50 | task:
|
37 | 51 | jobs:
|
38 |
| - - name: "Install Dependencies" |
| 52 | + - name: "βοΈ Compile" |
| 53 | + commands: |
| 54 | + - mix deps.get |
| 55 | + - mix compile |
| 56 | + - cache store # Save deps/ or _build for later steps |
| 57 | + |
| 58 | +# ------------------------------------------------------------------- |
| 59 | +# CODE-QUALITY BLOCKS β one block per check |
| 60 | +# ------------------------------------------------------------------- |
| 61 | + - name: "π Format" |
| 62 | + dependencies: ["π¦ Install & Build"] |
| 63 | + task: |
| 64 | + jobs: |
| 65 | + - name: "π¨ mix format" |
| 66 | + commands: |
| 67 | + - mix format --check-formatted |
| 68 | + |
| 69 | + - name: "π Credo" |
| 70 | + dependencies: ["π¦ Install & Build"] |
| 71 | + task: |
| 72 | + jobs: |
| 73 | + - name: "π mix credo" |
39 | 74 | commands:
|
40 |
| - - mix deps.compile # Compile dependencies to ensure compatibility |
41 |
| - - mix compile # Compile the project |
42 |
| - - cache store # Store compiled dependencies for future runs |
| 75 | + - mix credo --strict |
43 | 76 |
|
44 |
| - - name: "π Code Quality" |
45 |
| - dependencies: ["π οΈ Setup and Cache"] |
| 77 | + - name: "π Sobelow" |
| 78 | + dependencies: ["π¦ Install & Build"] |
46 | 79 | task:
|
47 | 80 | jobs:
|
48 |
| - - name: "Run Formatter and Credo" |
| 81 | + - name: "π‘οΈ mix sobelow" |
49 | 82 | commands:
|
50 |
| - - mix format --check-formatted # Ensure all code is properly formatted |
51 |
| - - mix credo --strict # Perform static analysis to enforce best practices |
| 83 | + - mix sobelow --exit |
52 | 84 |
|
53 |
| - - name: "π Security Checks" |
54 |
| - dependencies: ["π οΈ Setup and Cache"] |
| 85 | + - name: "π‘ Deps Audit" |
| 86 | + dependencies: ["π¦ Install & Build"] |
55 | 87 | task:
|
56 | 88 | jobs:
|
57 |
| - - name: "Run Security Scans" |
| 89 | + - name: "π‘οΈ mix deps.audit" |
58 | 90 | commands:
|
59 |
| - - mix deps.audit # Audit dependencies for known vulnerabilities |
60 |
| - - mix sobelow # Run security analysis on the codebase |
| 91 | + - mix deps.audit |
61 | 92 |
|
62 | 93 | - name: "π Dialyzer"
|
63 |
| - dependencies: ["π οΈ Setup and Cache"] |
| 94 | + dependencies: ["π¦ Install & Build"] |
| 95 | + task: |
| 96 | + jobs: |
| 97 | + - name: "π§ mix dialyzer" |
| 98 | + env_vars: |
| 99 | + - name: MIX_ENV |
| 100 | + value: test |
| 101 | + commands: |
| 102 | + - mix dialyzer --halt-exit-status |
| 103 | + |
| 104 | +# ------------------------------------------------------------------- |
| 105 | +# BLOCK: π§ͺ Tests β split suite for faster feedback |
| 106 | +# ------------------------------------------------------------------- |
| 107 | + - name: "π§ͺ Tests" |
| 108 | + dependencies: ["π¦ Install & Build"] |
64 | 109 | task:
|
| 110 | + epilogue: # Publish JUnit only for test jobs |
| 111 | + always: |
| 112 | + commands: |
| 113 | + - '[ -f report.xml ] && test-results publish report.xml' # https://docs.semaphoreci.com/using-semaphore/tests/test-reports |
65 | 114 | jobs:
|
66 |
| - - name: "Type Checking" |
| 115 | + - name: "π§ͺ Tests-1" |
| 116 | + env_vars: |
| 117 | + - name: MIX_TEST_PARTITION |
| 118 | + value: "1" |
67 | 119 | commands:
|
68 |
| - - mix dialyzer # Perform static type checking for code correctness |
| 120 | + - mix test --color |
| 121 | + - name: "π§ͺ Tests-2" |
| 122 | + env_vars: |
| 123 | + - name: MIX_TEST_PARTITION |
| 124 | + value: "2" |
| 125 | + commands: |
| 126 | + - mix test --color |
69 | 127 |
|
70 |
| - - name: "π§ͺ Test Suite" |
71 |
| - dependencies: ["π οΈ Setup and Cache"] |
| 128 | +# ------------------------------------------------------------------- |
| 129 | +# BLOCK: π Docker Deploy β runs only on main branch |
| 130 | +# Secrets docs: https://docs.semaphoreci.com/reference/pipeline-yaml#secrets-in-task |
| 131 | +# ------------------------------------------------------------------- |
| 132 | + - name: "π Docker Deploy" |
| 133 | + dependencies: |
| 134 | + - "π Format" |
| 135 | + - "π Credo" |
| 136 | + - "π Sobelow" |
| 137 | + - "π‘ Deps Audit" |
| 138 | + - "π Dialyzer" |
| 139 | + - "π§ͺ Tests" |
| 140 | + run: |
| 141 | + when: "branch = 'main'" |
72 | 142 | task:
|
73 |
| - env_vars: |
74 |
| - - name: MIX_ENV |
75 |
| - value: test # Set the environment to test |
76 |
| - - name: PGHOST |
77 |
| - value: postgres # PostgreSQL container name |
78 |
| - - name: PGUSER |
79 |
| - value: postgres # PostgreSQL user |
| 143 | + secrets: |
| 144 | + - name: dockerhub # Registry creds |
80 | 145 | jobs:
|
81 |
| - - name: "π’ Run Tests with Coverage" |
82 |
| - parallelism: 4 # Run tests in parallel for faster execution |
| 146 | + - name: "π³ Build & Push" |
83 | 147 | commands:
|
84 |
| - - mix test.setup # Set up the test database |
85 |
| - - mix test --partitions $SEMAPHORE_JOB_COUNT --partition $SEMAPHORE_JOB_INDEX --cover # Run tests with partitioning and coverage reporting |
| 148 | + - echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin |
| 149 | + - docker build -t myapp:${SEMAPHORE_GIT_SHA} . |
| 150 | + - docker tag myapp:${SEMAPHORE_GIT_SHA} myapp:latest |
| 151 | + - docker push myapp:${SEMAPHORE_GIT_SHA} |
| 152 | + - docker push myapp:latest |
| 153 | + |
| 154 | +# ------------------------------------------------------------------- |
| 155 | +# AFTER PIPELINE β merge JUnit files into a single report |
| 156 | +# Docs: https://docs.semaphoreci.com/using-semaphore/tests/test-reports#step3 |
| 157 | +# ------------------------------------------------------------------- |
| 158 | +after_pipeline: |
| 159 | + task: |
| 160 | + jobs: |
| 161 | + - name: "π Merge Reports" |
| 162 | + commands: |
| 163 | + - test-results gen-pipeline-report # Combine job reports |
0 commit comments