Skip to content

Commit 0ef277d

Browse files
committed
chore: enhance CI
with conditional e2e tests recording in Cypress Cloud and parallel execution in GitHub Actions
1 parent 118be1e commit 0ef277d

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

.github/workflows/dhis2-verify-app.yml

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ name: 'dhis2: verify (app)'
1010
# 'jobs.release.steps.*.cwd'
1111
# 'jobs.release.steps.*.build-dir'
1212

13-
on: push
13+
on:
14+
push:
15+
pull_request:
16+
types: [labeled]
1417

1518
concurrency:
1619
group: ${{ github.workflow}}-${{ github.ref }}
@@ -23,6 +26,18 @@ env:
2326
D2_VERBOSE: true
2427

2528
jobs:
29+
setup-matrix:
30+
runs-on: ubuntu-latest
31+
outputs:
32+
matrix: ${{ steps.set-matrix.outputs.specs }}
33+
steps:
34+
- uses: actions/checkout@v3
35+
- name: Generate test matrix
36+
id: set-matrix
37+
run: |
38+
node cypress/support/generateTestMatrix.js > matrix.json
39+
echo "::set-output name=specs::$(cat matrix.json)"
40+
2641
build:
2742
runs-on: ubuntu-latest
2843
steps:
@@ -86,28 +101,49 @@ jobs:
86101
e2e:
87102
runs-on: ubuntu-latest
88103
if: "!github.event.push.repository.fork && github.actor != 'dependabot[bot]'"
104+
needs: [build, lint, test, setup-matrix]
89105

90106
strategy:
91107
fail-fast: false
92108
matrix:
93-
containers: [1, 2, 3, 4]
109+
spec-group: ${{ fromJson(needs.setup-matrix.outputs.matrix) }}
94110

95-
steps:
96-
- name: Checkout
97-
uses: actions/checkout@v2
111+
env:
112+
SHOULD_RECORD: ${{ contains(github.event.head_commit.message, '[e2e record]') || contains(join(github.event.pull_request.labels.*.name), 'e2e record') }}
98113

114+
steps:
115+
- uses: actions/checkout@v2
99116
- uses: actions/setup-node@v1
100117
with:
101118
node-version: 16.x
102119

120+
- name: Set Cypress Record Environment Variables
121+
if: env.SHOULD_RECORD == 'true'
122+
run: |
123+
echo "CYPRESS_GROUP=e2e-${{ matrix.spec-group.id }}" >> $GITHUB_ENV
124+
echo "CYPRESS_TAG=${{ github.event_name }}" >> $GITHUB_ENV
125+
echo "CYPRESS_CI_BUILD_ID=${{ github.run_id }}" >> $GITHUB_ENV
126+
127+
- name: Debug Environment Variables
128+
run: |
129+
echo "SHOULD_RECORD=${{ env.SHOULD_RECORD }}"
130+
echo "CI Build ID=${{ github.run_id }}"
131+
echo "Computed Group=${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_GROUP || '' }}"
132+
echo "Computed Tag=${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_TAG || '' }}"
133+
echo "Computed CI Build ID=${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_CI_BUILD_ID || '' }}"
134+
echo "Spec=${{ join(matrix.spec-group.tests, ',') }}"
135+
103136
- name: End-to-End tests
104137
uses: cypress-io/github-action@v2
105138
with:
106139
start: yarn d2-app-scripts start
107140
wait-on: 'http://localhost:3000'
108141
wait-on-timeout: 300
109-
record: true
110-
parallel: true
142+
record: ${{ env.SHOULD_RECORD }}
143+
parallel: ${{ env.SHOULD_RECORD }}
144+
group: ${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_GROUP || '' }}
145+
tag: ${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_TAG || '' }}
146+
ci-build-id: ${{ env.SHOULD_RECORD == 'true' && env.CYPRESS_CI_BUILD_ID || '' }}
111147
env:
112148
BROWSER: none
113149
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ A deployable `.zip` file can be found in `build/bundle`!
2828

2929
See the section about [building](https://platform.dhis2.nu/#/scripts/build) for more information.
3030

31+
## Conditional E2E Test Recording
32+
33+
To record e2e tests in Cypress Cloud, you can use one of the following methods based on your needs:
34+
35+
- **Commit Message**: Include `[e2e record]` in your commit messages to activate recording.
36+
- **GitHub Labels**: Apply the `e2e record` label to your pull request to trigger recording.
37+
38+
This setup helps in managing Cypress Cloud credits more efficiently, ensuring recordings are only made when explicitly required.
39+
3140
## Learn More
3241

3342
You can learn more about the platform in the [DHIS2 Application Platform Documentation](https://platform.dhis2.nu/).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
const getAllFiles = (dirPath, arrayOfFiles = []) => {
5+
const files = fs.readdirSync(dirPath)
6+
7+
files.forEach((file) => {
8+
if (fs.statSync(path.join(dirPath, file)).isDirectory()) {
9+
arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles)
10+
} else if (path.extname(file) === '.feature') {
11+
arrayOfFiles.push(path.join(dirPath, file))
12+
}
13+
})
14+
15+
return arrayOfFiles
16+
}
17+
18+
const createGroups = (files, numberOfGroups = 7) => {
19+
const groups = []
20+
for (let i = 0; i < numberOfGroups; i++) {
21+
groups.push([])
22+
}
23+
24+
files.forEach((file, index) => {
25+
groups[index % numberOfGroups].push(file)
26+
})
27+
28+
return groups.map((group, index) => ({ id: index + 1, tests: group }))
29+
}
30+
31+
const cypressSpecsPath = './cypress/integration'
32+
const specs = getAllFiles(cypressSpecsPath)
33+
const groupedSpecs = createGroups(specs)
34+
35+
console.log(JSON.stringify(groupedSpecs))

0 commit comments

Comments
 (0)