Skip to content

Commit 5f6b635

Browse files
author
Mikaël Bouchez
committed
feat(ci): add examples with corrections
1 parent 1c302d4 commit 5f6b635

File tree

5 files changed

+471
-740
lines changed

5 files changed

+471
-740
lines changed

.github/workflows/ci.yml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: CI Pipeline
2+
3+
on:
4+
push:
5+
branches:
6+
- examples
7+
8+
jobs:
9+
install:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
15+
- name: Setup Node.js
16+
uses: actions/setup-node@v4
17+
with:
18+
node-version: 22
19+
20+
- name: Cache dependencies
21+
uses: actions/cache@v4
22+
with:
23+
path: node_modules
24+
key: node-modules-${{ hashFiles('package-lock.json') }}
25+
restore-keys: node-modules-
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Upload node_modules as an artifact
31+
uses: actions/upload-artifact@v4
32+
with:
33+
name: node-modules
34+
path: node_modules
35+
retention-days: 1
36+
37+
test_artifact:
38+
runs-on: ubuntu-latest
39+
needs: install
40+
steps:
41+
- name: Download node_modules artifact (2)
42+
uses: actions/download-artifact@v4
43+
with:
44+
name: node-modules
45+
path: ./node_modules/
46+
47+
- name: List node_modules/.bin contents (2)
48+
run: ls -la node_modules/.bin
49+
50+
lint:
51+
runs-on: ubuntu-latest
52+
steps:
53+
- name: Checkout repository
54+
uses: actions/checkout@v4
55+
56+
- name: Setup Biome CLI
57+
uses: biomejs/setup-biome@v2
58+
59+
- name: Run Biome
60+
run: biome ci
61+
62+
typing:
63+
runs-on: ubuntu-latest
64+
needs: install
65+
steps:
66+
- name: Checkout repository
67+
uses: actions/checkout@v4
68+
69+
- name: Download node_modules artifact
70+
uses: actions/download-artifact@v4
71+
with:
72+
name: node-modules
73+
path: node_modules
74+
75+
- name: Run type check
76+
run: npm run typing-check
77+
78+
coverage:
79+
runs-on: ubuntu-latest
80+
needs: install
81+
steps:
82+
- name: Checkout repository
83+
uses: actions/checkout@v4
84+
85+
- name: Download node_modules artifact
86+
uses: actions/download-artifact@v4
87+
with:
88+
name: node-modules
89+
path: node_modules
90+
91+
- name: Run tests with coverage
92+
run: npm run coverage
93+
94+
- name: "Report Coverage"
95+
if: always()
96+
uses: davelosert/vitest-coverage-report-action@v2

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,4 @@ node_modules
5151
!.yarn/sdks
5252
!.yarn/versions
5353
.pnp.*
54-
tsconfig.lib.tsbuildinfo
55-
56-
GITLAB.md
54+
tsconfig.lib.tsbuildinfo

README.md

Lines changed: 124 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ npm install
3636
git commit --allow-empty -m "Init kata john"
3737
```
3838

39-
## Create Your First GitHub Actions Workflow
39+
## 🚀 Create Your First GitHub Actions Workflow
4040

4141
<details>
4242
<summary>Expand</summary>
@@ -77,9 +77,6 @@ jobs:
7777

7878
- name: Install dependencies
7979
run: npm ci
80-
81-
- name: Install biome
82-
run: npm install -D @biomejs/cli-linux-x64
8380
```
8481
8582
4. Commit and push your workflow file:
@@ -118,8 +115,6 @@ Once you push your changes, GitHub will automatically trigger the workflow.
118115

119116
3. Install dependencies – Runs npm ci to install dependencies.
120117

121-
4. Install biome – Installs @biomejs/cli-linux-x64 to avoid issues in GitHub Actions.
122-
123118
</details>
124119

125120
## 🚀 Using Artifacts and Cache in GitHub Actions
@@ -131,7 +126,7 @@ In GitHub Actions, artifacts and cache serve similar purposes as in GitLab CI:
131126

132127
🔗 Refer to the [GitHub Actions documentation](https://docs.github.com/en/actions/using-workflows/caching-dependencies-and-builds) on caching and artifacts for more details.
133128

134-
### 🏋️ Exercise
129+
### 📝 Exercise
135130

136131
1. Enhance the `install` job to include artifacts and cache configurations:
137132

@@ -213,10 +208,8 @@ By adding a **lint job** in the pipeline, we ensure that all code adheres to bes
213208
Enhance the workflow by:
214209

215210
1. Creating a new stage called **check**.
216-
2. Adding a **lint job** that runs `npm run lint`.
217-
3. Reusing `node_modules/` from the **install** job using GitHub Actions cache & artifacts.
218-
4. Making **lint** depend on **install**, ensuring dependencies are installed first.
219-
5. Commit your updated workflow
211+
2. Adding a **lint job** that runs `biome ci` using the [Setup Biome GitHub Action](https://github.com/marketplace/actions/setup-biome).
212+
3. Commit your updated workflow
220213
- Push your changes to trigger the GitHub Actions workflow.
221214
- Verify that the **lint job** runs successfully in the **Actions** tab. 🚀
222215

@@ -226,81 +219,145 @@ Enhance the workflow by:
226219
<summary>Expand</summary>
227220

228221
```yaml
229-
name: CI Pipeline
222+
lint:
223+
runs-on: ubuntu-latest
224+
needs: install # Ensures the install job completes first
225+
steps:
226+
- name: Checkout repository
227+
uses: actions/checkout@v4
228+
229+
- name: Setup Biome CLI
230+
uses: biomejs/setup-biome@v2 # The action will look for the version of the @biomejs/biome dependency in the lockfiles of popular package managers such as npm.
231+
232+
- name: Run Biome
233+
run: biome ci # Files won’t be modified, the command is a read-only operation.
234+
```
230235

231-
on:
232-
push:
233-
branches:
234-
- john
236+
### 🔍 Explanation
235237

236-
jobs:
237-
install:
238-
runs-on: ubuntu-latest
239-
steps:
240-
- name: Checkout repository
241-
uses: actions/checkout@v4
238+
TODO
242239

243-
- name: Setup Node.js
244-
uses: actions/setup-node@v4
245-
with:
246-
node-version: 22
240+
</details>
247241

248-
- name: Cache dependencies
249-
uses: actions/cache@v4
250-
with:
251-
path: node_modules
252-
key: node-modules-${{ hashFiles('package-lock.json') }}
253-
restore-keys: node-modules-
242+
## 🚀 Add Typing Check Job in GitHub Actions
254243

255-
- name: Install dependencies
256-
run: npm ci
244+
Type checking ensures that your TypeScript project is type-safe and free of type-related errors before going to production.
245+
By using the --noEmit flag, TypeScript verifies the code without generating build files.
257246

258-
- name: Upload node_modules as an artifact
259-
uses: actions/upload-artifact@v4
260-
with:
261-
name: node-modules
262-
path: node_modules
263-
retention-days: 1
247+
### 📝 Exercise
264248

265-
lint:
266-
runs-on: ubuntu-latest
267-
needs: install # Ensures the install job completes first
268-
steps:
269-
- name: Checkout repository
270-
uses: actions/checkout@v4
249+
Enhance the workflow by:
271250

272-
- name: Download node_modules artifact
273-
uses: actions/download-artifact@v4
274-
with:
275-
name: node-modules
276-
path: node_modules
251+
1. Defining a typing job in the check stage.
252+
2. Running the command: `npm run typing-check` (which should internally run `tsc --noEmit`).
253+
3. Making the typing job depend on the install job (so `node_modules` is available from artifact).
254+
4. Commit your changes
277255

278-
- name: Run linter
279-
run: npm run lint
256+
- Push your code to trigger the GitHub Actions workflow.
257+
- Confirm that type checking is performed in the Actions tab.
258+
259+
### 🎯 Correction
260+
261+
<details>
262+
<summary>Expand</summary>
263+
264+
```yaml
265+
typing:
266+
runs-on: ubuntu-latest
267+
needs: install
268+
steps:
269+
- name: Checkout repository
270+
uses: actions/checkout@v4
271+
272+
- name: Download node_modules artifact
273+
uses: actions/download-artifact@v4
274+
with:
275+
name: node-modules
276+
path: node_modules
277+
278+
- name: Run type check
279+
run: npm run typing-check
280280
```
281281

282282
### 🔍 Explanation
283283

284-
### **install job**:
284+
#### `typing job`:
285+
286+
- Depends on the install job using `needs: install`.
287+
288+
- Reuses `node_modules` by downloading the artifact from the previous job.
289+
290+
- Runs `npm run typing-check`, which should call `tsc --noEmit` in your package.json.
291+
292+
#### Why `--noEmit`?
293+
294+
Ensures your TypeScript code compiles without actually creating build output.
295+
Ideal for CI checks where only validation is needed.
296+
297+
</details>
285298

286-
- Installs dependencies using `npm ci`.
287-
- Caches `node_modules/` using `actions/cache` for efficiency.
288-
- Uploads `node_modules/` as an artifact to be used in later jobs.
299+
## 🚀 Add Coverage Job in GitHub Actions
289300

290-
### **lint job**:
301+
Code coverage helps measure how much of your codebase is tested by your test suite.
302+
Generating a coverage report and exposing it as an artifact gives visibility on test quality and helps maintain high standards.
291303

292-
- Declares `needs: install`, ensuring that dependencies are installed first.
293-
- Downloads `node_modules/` from the artifact saved in the **install** job.
294-
- Runs `npm run lint` to check code quality.
304+
### 📝 Exercise
305+
306+
Enhance the workflow by:
307+
308+
1. Adding a new coverage job in the check stage.
309+
2. Making the coverage job depend on the install job.
310+
3. Running the command: `npm run coverage` to execute tests and generate a coverage report.
311+
4. Uploading the coverage report (in Cobertura format, usually coverage/cobertura-coverage.xml) as an artifact.
312+
5. Adding the coverage summary regex to display the total Statements coverage:
313+
314+
```yaml
315+
coverage: '/^Statements\s*:\s*([\d\.]+)%/'
316+
```
317+
318+
6. Commit your changes
319+
320+
- Push your code to trigger the GitHub Actions workflow.
321+
- Check the coverage percentage and validate that the report is correctly generated and accessible as an artifact.
322+
323+
### 🎯 Correction
324+
325+
<details>
326+
327+
<summary>Expand</summary>
328+
329+
```yaml
330+
coverage:
331+
runs-on: ubuntu-latest
332+
needs: install
333+
steps:
334+
- name: Checkout repository
335+
uses: actions/checkout@v4
336+
337+
- name: Download node_modules artifact
338+
uses: actions/download-artifact@v4
339+
with:
340+
name: node-modules
341+
path: node_modules
342+
343+
- name: Run tests with coverage
344+
run: npm run coverage
345+
346+
- name: Upload coverage report
347+
uses: actions/upload-artifact@v4
348+
with:
349+
name: coverage-report
350+
path: coverage/cobertura-coverage.xml
351+
352+
coverage: '/^Statements\\s*:\\s*([\\d\\.]+)%/' # GitHub reads this regex for coverage summary
353+
```
295354

296-
### 💡 Why use both **cache** and **artifacts**?
355+
🔍 Explanation
297356

298-
- **Cache (`actions/cache`)** is shared between workflow runs (persists across multiple commits).
299-
- **Artifacts (`actions/upload-artifact`)** are job-specific (persist only within the same workflow execution).
357+
- `npm run coverage` should generate a report in Cobertura format (usually with Jest and `jest --coverage --coverageReporters=cobertura`).
300358

301-
Using both ensures that dependencies are efficiently reused across jobs and across multiple workflow runs.
359+
- We upload `coverage/cobertura-coverage.xml` as an artifact so it can be downloaded and visualized.
302360

303-
🚀 **This setup ensures faster builds and enforces linting rules before merging any code!**
361+
The coverage field uses a regex to extract the total statement coverage percentage from the test output (e.g., Statements: 85.45%).
304362

305363
</details>
306-
Happy automating!

0 commit comments

Comments
 (0)