1
1
---
2
2
name : Workflow-Pipeline
3
3
4
+ # Define when this workflow should run
4
5
on :
6
+ # Run on push events to main or development branches
5
7
push :
6
8
branches :
7
9
- main
8
10
- development
9
11
paths-ignore :
10
- - ' docs/**'
12
+ - ' docs/**' # Ignore changes to docs folder
13
+ # Run on pull requests to main or development branches
11
14
pull_request :
12
15
branches :
13
16
- main
14
17
- development
15
18
paths-ignore :
16
- - ' docs/**'
19
+ - ' docs/**' # Ignore changes to docs folder
17
20
21
+ # Define the jobs that this workflow will run
18
22
jobs :
23
+ # Job for testing the examples directory
19
24
Example-Unit-Testing :
20
25
name : Example Unit Testing (v${{ matrix.go-version }})🛠
21
26
runs-on : ubuntu-latest
27
+ # Define a matrix strategy to test against multiple Go versions
22
28
strategy :
23
29
matrix :
24
30
go-version : ['1.24','1.23', '1.22']
31
+ # Continue with other jobs if one version fails
32
+ fail-fast : false
25
33
34
+ # Define service containers that tests depend on
26
35
services :
36
+ # Kafka service
27
37
kafka :
28
38
image : bitnami/kafka:3.4
29
39
ports :
@@ -41,12 +51,14 @@ jobs:
41
51
ALLOW_PLAINTEXT_LISTENER : yes
42
52
KAFKA_CFG_NODE_ID : 1
43
53
54
+ # Redis service
44
55
redis :
45
56
image : redis:7.0.5
46
57
ports :
47
58
- " 2002:6379"
48
59
options : " --entrypoint redis-server"
49
60
61
+ # MySQL service
50
62
mysql :
51
63
image : mysql:8.2.0
52
64
ports :
@@ -55,12 +67,14 @@ jobs:
55
67
MYSQL_ROOT_PASSWORD : " password"
56
68
MYSQL_DATABASE : " test"
57
69
70
+ # Steps to execute for this job
58
71
steps :
59
72
- name : Checkout code into go module directory
60
73
uses : actions/checkout@v4
61
74
with :
62
- fetch-depth : 0
75
+ fetch-depth : 0 # Full git history for accurate testing
63
76
77
+ # Set up the Go environment with the specified version
64
78
- name : Set up Go ${{ matrix.go-version }}
65
79
uses : actions/setup-go@v5
66
80
with :
@@ -74,26 +88,38 @@ jobs:
74
88
- name : Start Zipkin
75
89
run : docker run -d -p 2005:9411 openzipkin/zipkin:latest
76
90
77
- - name : Test
78
- run : |
79
- export APP_ENV=test
80
- go test gofr.dev/examples/... -v -short -coverprofile packageWithpbgo.cov -coverpkg=gofr.dev/examples/...
81
- grep -vE '^gofr\.dev\/.*\.pb\.go' packageWithpbgo.cov > profile.cov
82
- go tool cover -func profile.cov
83
-
91
+ # Run tests with automatic retry on failures
92
+ - name : Test with Retry Logic
93
+ id : test
94
+ uses : nick-fields/retry@v2
95
+ with :
96
+ timeout_minutes : 5 # Maximum time for the tests to run
97
+ max_attempts : 2 # Retry up to 2 times if tests fail
98
+ command : |
99
+ export APP_ENV=test
100
+ # Run tests for the examples directory with coverage
101
+ go test gofr.dev/examples/... -v -short -coverprofile packageWithpbgo.cov -coverpkg=gofr.dev/examples/...
102
+ # Filter out generated protobuf files from coverage report
103
+ grep -vE '^gofr\.dev\/.*\.pb\.go' packageWithpbgo.cov > profile.cov
104
+ # Display coverage statistics
105
+ go tool cover -func profile.cov
106
+
107
+ # Upload coverage report for the 1.24 Go version only
84
108
- name : Upload Test Coverage
85
- if : ${{ matrix.go-version == '1.22 '}}
109
+ if : ${{ matrix.go-version == '1.24 '}}
86
110
uses : actions/upload-artifact@v4
87
111
with :
88
112
name : Example-Test-Report
89
113
path : profile.cov
90
114
115
+ # Job for testing the pkg directory
91
116
PKG-Unit-Testing :
92
117
name : PKG Unit Testing (v${{ matrix.go-version }})🛠
93
118
runs-on : ubuntu-latest
94
119
strategy :
95
120
matrix :
96
121
go-version : ['1.24','1.23', '1.22']
122
+ fail-fast : false
97
123
98
124
steps :
99
125
- name : Checkout code into go module directory
@@ -111,58 +137,74 @@ jobs:
111
137
run : |
112
138
go mod download
113
139
114
- - name : Test
115
- run : |
116
- export APP_ENV=test
117
- go test gofr.dev/pkg/... -v -short -coverprofile package.cov -coverpkg=gofr.dev/pkg/...
118
- grep -v '/mock_' package.cov > profile.cov
119
- go tool cover -func profile.cov
120
-
140
+ # Run pkg tests with automatic retry logic
141
+ - name : Test with Retry Logic
142
+ id : test
143
+ uses : nick-fields/retry@v2
144
+ with :
145
+ timeout_minutes : 5
146
+ max_attempts : 2
147
+ command : |
148
+ export APP_ENV=test
149
+ # Run tests for the pkg directory with coverage
150
+ go test gofr.dev/pkg/... -v -short -coverprofile package.cov -coverpkg=gofr.dev/pkg/...
151
+ # Filter out mock files from coverage report
152
+ grep -v '/mock_' package.cov > profile.cov
153
+ # Display coverage statistics
154
+ go tool cover -func profile.cov
155
+
156
+ # Upload coverage report for the 1.24 Go version only
121
157
- name : Upload Test Coverage
122
158
if : ${{ matrix.go-version == '1.24'}}
123
159
uses : actions/upload-artifact@v4
124
160
with :
125
161
name : PKG-Coverage-Report
126
162
path : profile.cov
127
163
164
+ # Job for analyzing and reporting code coverage
128
165
parse_coverage :
129
166
name : Code Coverage
130
167
runs-on : ubuntu-latest
131
- needs : [ Example-Unit-Testing,PKG-Unit-Testing]
168
+ # This job runs after both Example and PKG testing are complete
169
+ needs : [ Example-Unit-Testing,PKG-Unit-Testing ]
132
170
steps :
133
171
- name : Check out code into the Go module directory
134
172
uses : actions/checkout@v4
135
173
174
+ # Download coverage reports from previous jobs
136
175
- name : Download Coverage Report
137
176
uses : actions/download-artifact@v4
138
177
with :
139
178
path : artifacts
140
179
141
-
180
+ # Merge the coverage reports from Example and PKG tests
142
181
- name : Merge Coverage Files
143
182
working-directory : artifacts
144
183
run : |
145
184
awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov
146
185
tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
147
186
187
+ # Calculate and output the total code coverage percentage
148
188
- name : Parse code-coverage value
149
189
working-directory : artifacts
150
190
run : |
151
191
codeCoverage=$(go tool cover -func=merged_profile.cov | grep total | awk '{print $3}')
152
192
codeCoverage=${codeCoverage%?}
153
193
echo "CODE_COVERAGE=$codeCoverage" >> $GITHUB_ENV
154
-
155
194
# - name: Check if code-coverage is greater than threshold
156
195
# run: |
157
196
# codeCoverage=${{ env.CODE_COVERAGE }}
158
197
# codeCoverage=${codeCoverage%??}
159
198
# if [[ $codeCoverage -lt 92 ]]; then echo "code coverage cannot be less than 92%, currently its ${{ env.CODE_COVERAGE }}%" && exit 1; fi;
199
+
200
+ # Job for testing submodules inside the pkg directory
160
201
Submodule-Unit-Testing :
161
202
name : Submodule Unit Testing (v${{ matrix.go-version }})🛠
162
203
runs-on : ubuntu-latest
163
204
strategy :
164
205
matrix :
165
206
go-version : ['1.24','1.23', '1.22']
207
+ fail-fast : false
166
208
167
209
steps :
168
210
- name : Checkout code into go module directory
@@ -176,55 +218,90 @@ jobs:
176
218
go-version : ${{ matrix.go-version }}
177
219
id : Go
178
220
179
- - name : Detect and Test Submodules
221
+ # Find all submodules (directories with go.mod files) in the pkg directory
222
+ - name : Detect Submodules
223
+ id : detect_submodules
180
224
run : |
181
225
# Find all directories containing a go.mod file within 'pkg'
182
- for module in $(find pkg -name "go.mod" -exec dirname {} \;); do
183
- echo "Testing module: $module"
184
- cd $module
185
-
186
- # Extract module name (replace '/' with '_')
187
- module_name=$(echo $module | tr '/' '_')
188
-
189
- # Download dependencies for the submodule
190
- go mod download
191
- go mod tidy
226
+ SUBMODULES=$(find pkg -name "go.mod" -exec dirname {} \; | jq -R -s -c 'split("\n") | map(select(length > 0))')
227
+ echo "submodules=$SUBMODULES" >> $GITHUB_OUTPUT
192
228
193
- # Run tests for the submodule and generate coverage
229
+ # Test all submodules in parallel with retry logic
230
+ - name : Test Submodules with Retry and Parallelism
231
+ id : test_submodules
232
+ uses : nick-fields/retry@v2
233
+ with :
234
+ timeout_minutes : 5
235
+ max_attempts : 2
236
+ command : |
194
237
export APP_ENV=test
195
- go test ./... -v -short -coverprofile=${module_name}.cov -coverpkg=./...
196
-
197
- # Return to the root directory
198
- cd -
199
- done
200
-
238
+ # Create a directory for coverage reports
239
+ mkdir -p coverage_reports
240
+
241
+ # Get the list of submodules
242
+ SUBMODULES='${{ steps.detect_submodules.outputs.submodules }}'
243
+
244
+ # Process each submodule in parallel with a maximum of 4 parallel jobs
245
+ echo $SUBMODULES | jq -c '.[]' | xargs -I{} -P 4 bash -c '
246
+ module={}
247
+ echo "Testing module: $module"
248
+ cd $module
249
+
250
+ # Extract module name (replace / with _)
251
+ module_name=$(echo $module | tr "/" "_")
252
+
253
+ # Download dependencies for the submodule
254
+ go mod download
255
+ go mod tidy
256
+
257
+ # Run tests with a focus on failed tests first
258
+ go test ./... -v -short -coverprofile=${module_name}.cov -coverpkg=./...
259
+
260
+ # Copy coverage file to the coverage_reports directory
261
+ cp ${module_name}.cov ../../../coverage_reports/
262
+
263
+ cd -
264
+ '
265
+
266
+ # Upload submodule coverage reports as an artifact
201
267
- name : Upload Coverage Reports
202
268
uses : actions/upload-artifact@v4
203
269
with :
204
- name : coverage-reports
205
- path : pkg/**/*.cov
206
-
270
+ name : submodule-coverage-reports
271
+ path : coverage_reports/*.cov
207
272
273
+ # Job for uploading coverage to external services (CodeClimate)
208
274
upload_coverage :
209
275
name : Upload Coverage📊
210
276
runs-on : ubuntu-latest
211
- needs : [Example-Unit-Testing,PKG-Unit-Testing]
277
+ # This job only needs example and pkg test results, not submodules
278
+ needs : [Example-Unit-Testing, PKG-Unit-Testing]
279
+ # Only run this job on pushes to the development branch
212
280
if : ${{ github.event_name == 'push' && github.ref == 'refs/heads/development'}}
213
281
steps :
214
282
- name : Check out code into the Go module directory
215
283
uses : actions/checkout@v4
216
284
285
+ # Download coverage artifacts
217
286
- name : Download Coverage Report
218
287
uses : actions/download-artifact@v4
219
288
with :
220
289
path : artifacts
221
290
291
+ # Merge coverage from example and pkg tests only
222
292
- name : Merge Coverage Files
223
293
working-directory : artifacts
224
294
run : |
225
- awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov
295
+ echo "mode: set" > merged_profile.cov
296
+ tail -n +2 ./Example-Test-Report/profile.cov >> merged_profile.cov
226
297
tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov
227
-
298
+
299
+ # Generate and print total coverage percentage
300
+ echo "Total Coverage:"
301
+ go tool cover -func=merged_profile.cov | tail -n 1
302
+ shell : bash
303
+
304
+ # Upload merged coverage to CodeClimate for analysis
228
305
- name : Upload
229
306
230
307
env :
@@ -233,6 +310,7 @@ jobs:
233
310
coverageLocations : artifacts/merged_profile.cov:gocov
234
311
prefix : gofr.dev
235
312
313
+ # Job for code quality checks
236
314
code_quality :
237
315
name : Code Quality🎖️
238
316
runs-on : ubuntu-latest
@@ -246,6 +324,7 @@ jobs:
246
324
go-version : ' 1.24'
247
325
cache : false
248
326
327
+ # Install the linting tool
249
328
- name : Install golangci-lint
250
329
run : |
251
330
go install github.com/golangci/golangci-lint/cmd/[email protected]
@@ -254,10 +333,12 @@ jobs:
254
333
run : |
255
334
go mod tidy
256
335
336
+ # Run linter on the root module
257
337
- name : Lint Root Module
258
338
run : |
259
339
golangci-lint run --out-format=colored-line-number --timeout=5m
260
340
341
+ # Run linter on each submodule
261
342
- name : Lint Submodules
262
343
run : |
263
344
echo "Searching for submodules..."
@@ -276,6 +357,7 @@ jobs:
276
357
exit 1 # Fail the job if there are linting errors in submodules
277
358
fi
278
359
360
+ # Job for checking filename conventions
279
361
linting_party :
280
362
name : Linting Party🥳
281
363
runs-on : ubuntu-latest
@@ -288,6 +370,7 @@ jobs:
288
370
with :
289
371
go-version : 1.24
290
372
373
+ # Check file naming conventions using ls-lint
291
374
- name : Check for file names errors
292
375
293
376
with :
0 commit comments