3333 GenerateReleaseWorkflow ( component ) ;
3434}
3535
36+ GenerateUploadTestResultsWorkflow ( ) ;
37+
38+
3639void GenerateCiWorkflow ( Component component )
3740{
38- var workflow = new Workflow ( $ " { component . Name } /ci" ) ;
41+ var workflow = new Workflow ( component . CiWorkflowName ) ;
3942 var paths = new [ ]
4043 {
4144 $ ".github/workflows/{ component . Name } -**",
@@ -76,9 +79,11 @@ void GenerateCiWorkflow(Component component)
7679
7780 foreach ( var testProject in component . Tests )
7881 {
79- job . StepTestAndReport ( component . Name , testProject ) ;
82+ job . StepTest ( component . Name , testProject ) ;
8083 }
8184
85+ job . StepUploadTestResultsAsArtifact ( component ) ;
86+
8287 job . StepToolRestore ( ) ;
8388
8489 foreach ( var project in component . Projects )
@@ -100,7 +105,7 @@ void GenerateCiWorkflow(Component component)
100105
101106void GenerateReleaseWorkflow ( Component component )
102107{
103- var workflow = new Workflow ( $ " { component . Name } /release" ) ;
108+ var workflow = new Workflow ( component . ReleaseWorkflowName ) ;
104109
105110 workflow . On
106111 . WorkflowDispatch ( )
@@ -166,14 +171,49 @@ git tag -a {component.TagPrefix}-{contexts.Event.Input.Version} -m ""Release v{c
166171 WriteWorkflow ( workflow , fileName ) ;
167172}
168173
174+ void GenerateUploadTestResultsWorkflow ( )
175+ {
176+ var workflow = new Workflow ( "generate-test-reports" ) ;
177+ workflow . On
178+ . WorkflowRun ( )
179+ . Workflows ( components . Select ( x => x . CiWorkflowName ) . ToArray ( ) )
180+ . Types ( "completed" ) ;
181+
182+ var job = workflow
183+ . Job ( "report" )
184+ . Name ( "report" )
185+ . RunsOn ( GitHubHostedRunners . UbuntuLatest ) ;
186+
187+ job . Permissions (
188+ actions : Permission . Read ,
189+ contents : Permission . Read ,
190+ checks : Permission . Write ,
191+ packages : Permission . Write ) ;
192+
193+ foreach ( var component in components )
194+ {
195+ foreach ( var testProject in component . Tests )
196+ {
197+ job . StepGenerateReportFromTestArtifact ( component , testProject ) ;
198+ }
199+ }
200+
201+ var fileName = $ "generate-test-reports";
202+ WriteWorkflow ( workflow , fileName ) ;
203+ }
204+
169205void WriteWorkflow ( Workflow workflow , string fileName )
170206{
171207 var filePath = $ "../workflows/{ fileName } .yml";
172208 workflow . WriteYaml ( filePath ) ;
173209 Console . WriteLine ( $ "Wrote workflow to { filePath } ") ;
174210}
175211
176- record Component ( string Name , string [ ] Projects , string [ ] Tests , string TagPrefix ) ;
212+ record Component ( string Name , string [ ] Projects , string [ ] Tests , string TagPrefix )
213+ {
214+ public string CiWorkflowName => $ "{ Name } /ci";
215+ public string ReleaseWorkflowName => $ "{ Name } /release";
216+ }
177217
178218public static class StepExtensions
179219{
@@ -190,28 +230,43 @@ public static void StepSetupDotNet(this Job job)
190230 public static Step IfRefMain ( this Step step )
191231 => step . If ( "github.ref == 'refs/heads/main'" ) ;
192232
193- public static Job RunEitherOnBranchOrAsPR ( this Job job )
194- => job . If (
195- "(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || (github.event_name == 'push')" ) ;
196-
197- public static void StepTestAndReport ( this Job job , string componentName , string testProject )
233+ public static void StepTest ( this Job job , string componentName , string testProject )
198234 {
199235 var path = $ "test/{ testProject } ";
200- var logFileName = "Tests .trx";
236+ var logFileName = $ " { testProject } .trx";
201237 var flags = $ "--logger \" console;verbosity=normal\" " +
202238 $ "--logger \" trx;LogFileName={ logFileName } \" " +
203239 $ "--collect:\" XPlat Code Coverage\" ";
204240 job . Step ( )
205241 . Name ( $ "Test - { testProject } ")
206242 . Run ( $ "dotnet test -c Release { path } { flags } ") ;
207243
244+ }
245+
246+ internal static void StepUploadTestResultsAsArtifact ( this Job job , Component component )
247+ {
248+ job . Step ( )
249+ . Name ( $ "Test report")
250+ . If ( "success() || failure()" )
251+ . Uses ( "actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882" ) // 4.4.3
252+ . With (
253+ ( "name" , "test-results" ) ,
254+ ( "path" , string . Join ( Environment . NewLine , component . Tests
255+ . Select ( testProject => $ "{ component . Name } /test/{ testProject } /TestResults/{ testProject } .trx") ) ) ,
256+ ( "retention-days" , "5" ) ) ;
257+ }
258+
259+ internal static void StepGenerateReportFromTestArtifact ( this Job job , Component component , string testProject )
260+ {
261+ var path = $ "test/{ testProject } ";
208262 job . Step ( )
209- . Name ( $ "Test report - { testProject } ")
263+ . Name ( $ "Test report - { component . Name } - { testProject } ")
210264 . Uses ( "dorny/test-reporter@31a54ee7ebcacc03a09ea97a7e5465a47b84aea5" ) // v1.9.1
211- . If ( "github.event_name == 'push' && (success() || failure()) ")
265+ . If ( $ "event.workflow.name == '{ component . CiWorkflowName } ' ")
212266 . With (
267+ ( "artifact" , "test-results" ) ,
213268 ( "name" , $ "Test Report - { testProject } ") ,
214- ( "path" , $ "{ componentName } / { path } /TestResults/ { logFileName } ") ,
269+ ( "path" , $ "{ testProject } .trx ") ,
215270 ( "reporter" , "dotnet-trx" ) ,
216271 ( "fail-on-error" , "true" ) ,
217272 ( "fail-on-empty" , "true" ) ) ;
@@ -272,7 +327,7 @@ public static void StepUploadArtifacts(this Job job, string componentName)
272327 var path = $ "{ componentName } /artifacts/*.nupkg";
273328 job . Step ( )
274329 . Name ( "Upload Artifacts" )
275- . IfRefMain ( )
330+ . IfGithubEventIsPush ( )
276331 . Uses ( "actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882" ) // 4.4.3
277332 . With (
278333 ( "name" , "artifacts" ) ,
0 commit comments