Skip to content

Commit

Permalink
MuDevOpsWrapper: Allow coverage uploads to ado or codecov.io (#270)
Browse files Browse the repository at this point in the history
## Description

This change reverts the changes made in #266 in favor of performing the
reformatting in the HostBasedUnitTestRunner plugin. From there it allows
the consumer of `MuDevOpsWrapper` to configure where code coverage data
is uploaded to, either `ado` (The azure devops pipeline that is running)
or `codecov` (The codecov account associated with the repository).

* Uploading to ado remains the same - A final job runs that merges
coverage data and uploads it to azure pipelines
* Due to the advanced capabilities, uploading to codecov runs per matrix
job and uploads using flags to separate code coverage data between
packages.

## Integration Instructions

The previous logic remains the default, that is to say code coverage is
uploaded to the azure devops pipeline that is running. Should a consumer
set `coverage_publish_target` to 'codecov', they must ensure that
`CODECOV_TOKEN` is set as a private variable in the pipeline.
  • Loading branch information
Javagedes authored Nov 13, 2023
1 parent bbb1cec commit 17c213b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .sync/Version.njk
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#}

{# The git ref value that files dependent on this repo will use. #}
{% set mu_devops = "v7.1.0" %}
{% set mu_devops = "v7.2.0" %}

{# The latest Project Mu release branch value. #}
{% set latest_mu_release_branch = "release/202302" %}
Expand Down
5 changes: 5 additions & 0 deletions .sync/azure_pipelines/MuDevOpsWrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ parameters:
- name: calculate_code_coverage
displayName: Calculate Code Coverage From Unit Tests
default: false
- name: coverage_publish_target
displayName: Code Coverage Publish Target
type: string
default: 'ado' # 'ado', 'codecov'
- name: container_build
displayName: Flag for whether this repo should do stuart_setup
type: boolean
Expand Down Expand Up @@ -93,6 +97,7 @@ jobs:
do_ci_setup: ${{ parameters.do_ci_setup }}
do_pr_eval: ${{ parameters.do_pr_eval }}
calculate_code_coverage: ${{ parameters.calculate_code_coverage }}
coverage_publish_target: ${{ parameters.coverage_publish_target }}
do_non_ci_setup: ${{ parameters.do_non_ci_setup }}
do_non_ci_build: ${{ parameters.do_non_ci_build }}
build_matrix: ${{ parameters.build_matrix }}
Expand Down
14 changes: 9 additions & 5 deletions Jobs/PrGate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ parameters:
displayName: Calculate Code Coverage
type: boolean
default: false
- name: coverage_publish_target
displayName: Code Coverage Publish Target
type: string
default: 'ado' # 'ado', 'codecov'
- name: extra_post_build_steps
displayName: Extra Post-Build Steps
type: stepList
Expand Down Expand Up @@ -159,6 +163,10 @@ jobs:
do_non_ci_build: ${{ parameters.do_non_ci_build }}
do_non_ci_setup: ${{ parameters.do_non_ci_setup }}
do_pr_eval: ${{ parameters.do_pr_eval }}
${{ if and(eq(parameters.calculate_code_coverage, true), eq(parameters.coverage_publish_target, 'codecov')) }}:
publish_coverage: true
${{ else }}:
publish_coverage: false
tool_chain_tag: ${{ parameters.tool_chain_tag }}
install_tools: ${{ and(not(eq(item.Value.SelfHostAgent, true)), not(parameters.container_build)) }}
extra_install_step: ${{ parameters.extra_install_step }}
Expand All @@ -173,7 +181,7 @@ jobs:
${{ else }}:
extra_build_args: ''

- ${{ if eq(parameters.calculate_code_coverage, true) }}:
- ${{ if and(eq(parameters.calculate_code_coverage, true), eq(parameters.coverage_publish_target, 'ado')) }}:
- job: PublishCoverage
dependsOn:
- ${{ each item in parameters.build_matrix }}:
Expand All @@ -188,7 +196,3 @@ jobs:

steps:
- template: ../Steps/PublishCodeCoverage.yml
parameters:
reorganize_by_inf: true
extra_parse_args: -t NOOPT TOOL_CHAIN_TAG=$(tool_chain_tag)
build_file: ${{ parameters.build_file }}
11 changes: 11 additions & 0 deletions Steps/PrGate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ parameters:
displayName: Perform Stuart PR Evaluation
type: boolean
default: true
- name: publish_coverage
displayName: Publish Code Coverage to codecov.io
type: boolean
default: false
- name: extra_build_args
displayName: Extra Build Command Arguments
type: string
Expand Down Expand Up @@ -162,6 +166,13 @@ steps:
script: stuart_ci_build -c ${{ parameters.build_file }} -p $(pkgs_to_build) -t ${{ parameters.build_targets}} -a ${{ parameters.build_archs}} TOOL_CHAIN_TAG=${{ parameters.tool_chain_tag}} ${{ parameters.extra_build_args}}
condition: and(gt(variables.pkg_count, 0), succeeded())

- ${{ if eq(parameters.publish_coverage, true) }}:
- ${{ each pkg in split(parameters.build_pkgs, ',') }}:
- template: UploadCodeCoverage.yml
parameters:
report_dir: 'Build/${{ pkg }}'
flag: ${{ pkg }}

# Potential post-build steps
- ${{ parameters.extra_post_build_steps }}

Expand Down
31 changes: 3 additions & 28 deletions Steps/PublishCodeCoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ parameters:
displayName: Setup Python
type: boolean
default: true
- name: reorganize_by_inf
displayName: Reorganize cobertura report by INF # Requires stuart_report & stuart_parse
type: boolean
default: false
- name: extra_parse_args
displayName: Extra arguments for stuart_parse
type: string
default: ''
- name: build_file
displayName: Stuart Build File
type: string
default: ".pytool/CISettings.py"

steps:
- ${{ if eq(parameters.checkout_self, true) }}:
Expand All @@ -46,10 +34,10 @@ steps:
inputs:
buildType: 'current'
targetPath: '$(Build.ArtifactStagingDirectory)/coverage/'
itemPattern: "**/*coverage.xml"
itemPattern: "**/*_coverage.xml"

- powershell: |
$coverage_file_count=(Get-ChildItem $(Build.ArtifactStagingDirectory)/coverage/ -Recurse -Include *coverage.xml).count
$coverage_file_count=(Get-ChildItem $(Build.ArtifactStagingDirectory)/coverage/ -Recurse -Include *_coverage.xml).count
Write-Host echo "##vso[task.setvariable variable=coverage_file_count]$coverage_file_count"
displayName: Check For Coverage Files

Expand All @@ -58,22 +46,9 @@ steps:
inputs:
script: |
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:$(Build.ArtifactStagingDirectory)/coverage/**/*coverage.xml -targetdir:$(Build.ArtifactStagingDirectory)/Coverage -reporttypes:Cobertura
reportgenerator -reports:$(Build.ArtifactStagingDirectory)/coverage/**/*_coverage.xml -targetdir:$(Build.ArtifactStagingDirectory)/Coverage -reporttypes:Cobertura
condition: gt(variables.coverage_file_count, 0)

- ${{ if eq(parameters.reorganize_by_inf, true) }}:
- task: CmdLine@2
displayName: Parse Workspace
inputs:
script: stuart_parse -c ${{ parameters.build_file }} ${{ parameters.extra_parse_args }}
condition: gt(variables.coverage_file_count, 0)

- task: CmdLine@2
displayName: Organize report by INF
inputs:
script: stuart_report coverage $(Build.ArtifactStagingDirectory)/Coverage/Cobertura.xml --by-package -o $(Build.ArtifactStagingDirectory)/Coverage/Cobertura.xml
condition: gt(variables.coverage_file_count, 0)

- task: PublishCodeCoverageResults@1
displayName: Publish Code Coverage
inputs:
Expand Down
31 changes: 28 additions & 3 deletions Steps/UploadCodeCoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ parameters:
displayName: Code Coverage Report
type: string
default: ''
- name: flag
displayName: Use Package Flags
type: string
default: ''
- name: install_dependencies
displayName: Install Pypi Dependencies
type: boolean
Expand Down Expand Up @@ -87,6 +91,27 @@ steps:
if system == 'Linux':
os.chmod(filename, 0o755)
- script: |
$(codecov_uploader_cmd) -t ${{ parameters.codecov_token }} -s ${{ parameters.report_dir }} -Z
displayName: Upload Code Coverage to Codecov.io
- task: PythonScript@0
displayName: Run Codecov Uploader ${{ parameters.flag }}
env:
COV_FLAG: ${{ parameters.flag }}
REPORT_DIR: ${{ parameters.report_dir }}
UPLOAD_CMD: $(codecov_uploader_cmd)
inputs:
scriptSource: inline
script: |
from pathlib import Path
from edk2toollib.utility_functions import RunCmd
import io
import os
COV_FLAG = os.environ['COV_FLAG']
REPORT_DIR = os.environ['REPORT_DIR']
UPLOAD_CMD = os.environ['UPLOAD_CMD']
for cov_file in Path(REPORT_DIR).rglob('*coverage.xml'):
outstream = io.StringIO()
params = f'-f {cov_file} -Z'
if COV_FLAG:
params += f' -F {COV_FLAG}'
RunCmd(UPLOAD_CMD, params, outstream=outstream, raise_exception_on_nonzero=True)

0 comments on commit 17c213b

Please sign in to comment.