|
| 1 | +## @file |
| 2 | +# Azure Pipelines step template upload code coverage to codecov.io |
| 3 | +# |
| 4 | +# Follows the codecov.io documentation for uploading code coverage reports: |
| 5 | +# https://docs.codecov.com/docs/codecov-uploader |
| 6 | +# |
| 7 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 8 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 9 | +## |
| 10 | + |
| 11 | +parameters: |
| 12 | +- name: codecov_token |
| 13 | + displayName: Codecov.io Token |
| 14 | + type: string |
| 15 | + default: '' |
| 16 | +- name: report_dir |
| 17 | + displayName: Code Coverage Report |
| 18 | + type: string |
| 19 | + default: '' |
| 20 | +- name: install_dependencies |
| 21 | + displayName: Install Pypi Dependencies |
| 22 | + type: boolean |
| 23 | + default: true |
| 24 | + |
| 25 | +steps: |
| 26 | +- ${{ if eq(parameters.install_dependencies, true) }}: |
| 27 | + - script: | |
| 28 | + pip install requests |
| 29 | + displayName: Install Python Dependencies for Codecov Uploader |
| 30 | +
|
| 31 | +- task: PythonScript@0 |
| 32 | + displayName: Download and Verify Codecov Uploader |
| 33 | + inputs: |
| 34 | + scriptSource: inline |
| 35 | + script: | |
| 36 | + import platform |
| 37 | + import requests |
| 38 | + import hashlib |
| 39 | + import os |
| 40 | +
|
| 41 | + system = platform.system() |
| 42 | +
|
| 43 | + if system == 'Windows': |
| 44 | + url = 'https://uploader.codecov.io/latest/windows/codecov.exe' |
| 45 | + filename = 'codecov.exe' |
| 46 | + checksum_url = 'https://uploader.codecov.io/latest/windows/codecov.exe.SHA256SUM' |
| 47 | + checksum_filename = 'codecov.exe.SHA256SUM' |
| 48 | + print(f'##vso[task.setvariable variable=codecov_uploader_cmd].\{filename}') |
| 49 | + elif system == 'Linux': |
| 50 | + url = 'https://uploader.codecov.io/latest/linux/codecov' |
| 51 | + filename = 'codecov' |
| 52 | + checksum_url = 'https://uploader.codecov.io/latest/linux/codecov.SHA256SUM' |
| 53 | + checksum_filename = 'codecov.SHA256SUM' |
| 54 | + print(f'##vso[task.setvariable variable=codecov_uploader_cmd]./{filename}') |
| 55 | + else: |
| 56 | + print(f'##[error]Unsupported Host System! System = {system}.') |
| 57 | + print(f'##vso[task.complete result=Failed;]Unsupported Host System! System = {system}.') |
| 58 | +
|
| 59 | + response = requests.get(url) |
| 60 | + if response.status_code == 200: |
| 61 | + with open(filename, 'wb') as f: |
| 62 | + f.write(response.content) |
| 63 | + else: |
| 64 | + print(f'##[error]Failed to download Uploader. Error code: {response.status_code}.') |
| 65 | + print(f'##vso[task.complete result=Failed;]Failed to download Uploader. Error code: {response.status_code}.') |
| 66 | +
|
| 67 | + response = requests.get(checksum_url) |
| 68 | + if response.status_code == 200: |
| 69 | + with open(checksum_filename, 'wb') as f: |
| 70 | + f.write(response.content) |
| 71 | + else: |
| 72 | + print(f'##[error]Failed to download Checksum file. Error code: {response.status_code}.') |
| 73 | + print(f'##vso[task.complete result=Failed;]Failed to download Checksum file. Error code: {response.status_code}.') |
| 74 | +
|
| 75 | + with open(checksum_filename, 'r') as f: |
| 76 | + expected_hash = f.read().split(' ')[0] |
| 77 | +
|
| 78 | + actual_hash = hashlib.new('sha256') |
| 79 | + with open(filename, 'rb') as f: |
| 80 | + for chunk in iter(lambda: f.read(4096), b''): |
| 81 | + actual_hash.update(chunk) |
| 82 | +
|
| 83 | + if expected_hash != actual_hash.hexdigest(): |
| 84 | + print(f'##[error]Checksum did not match. Expected: {expected_hash}; Actual: {actual_hash.hexdigest()}.') |
| 85 | + print(f'##vso[task.complete result=Failed;]Hash Mismatch.') |
| 86 | +
|
| 87 | + if system == 'Linux': |
| 88 | + os.chmod(filename, 0o755) |
| 89 | +
|
| 90 | +- script: | |
| 91 | + $(codecov_uploader_cmd) -t ${{ parameters.codecov_token }} -s ${{ parameters.report_dir }} -Z |
| 92 | + displayName: Upload Code Coverage to Codecov.io |
0 commit comments