onnxruntime
153 строки · 5.1 Кб
1#!/usr/bin/env python3
2# Copyright (c) Microsoft Corporation. All rights reserved.
3# Licensed under the MIT License.
4
5import argparse6import json7import os8import subprocess9import sys10import typing11
12from run_CIs_for_external_pr import get_pipeline_names13from util.platform_helpers import is_windows14
15
16class DefaultArgsRawHelpFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):17pass18
19
20def _parse_args():21parser = argparse.ArgumentParser(22os.path.basename(__file__),23formatter_class=DefaultArgsRawHelpFormatter,24description="""Run the CIs used to validate PRs for the specified branch.25
26If not specified, the branch will be inferred (if possible) by running `git branch --show-current`.
27
28If specified, the `--include` filter is applied first, followed by any `--exclude` filter.
29`--include` and `--exclude` can be specified multiple times to accumulate values to include/exclude.
30
31Requires the Azure CLI with DevOps extension to be installed.
32Azure CLI: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli
33DevOps extension: https://github.com/Azure/azure-devops-cli-extension
34
35Configuration:
36Login:`az login`
37Configure ORT repo as default:
38`az devops configure --defaults organization=https://dev.azure.com/onnxruntime project=onnxruntime`
39
40Example usage:
41List all CIs
42`python run_CIs_for_branch.py --dry-run my/BranchName`
43Run all CIs
44`python run_CIs_for_branch.py my/BranchName`
45Run only Linux CIs
46`python run_CIs_for_branch.py --include linux my/BranchName`
47Exclude training CIs
48`python run_CIs_for_branch.py --exclude training my/BranchName`
49Run non-training Linux CIs
50`python run_CIs_for_branch.py --include linux --exclude training my/BranchName`
51""",52)53
54current_branch = None55get_branch_result = subprocess.run(["git", "branch", "--show-current"], capture_output=True, text=True, check=False)56if get_branch_result.returncode == 0:57current_branch = get_branch_result.stdout.strip()58
59parser.add_argument(60"-i", "--include", action="append", type=str, help="Include CIs that match this string. Case insensitive."61)62parser.add_argument(63"-e", "--exclude", action="append", type=str, help="Exclude CIs that match this string. Case insensitive."64)65parser.add_argument("--dry-run", action="store_true", help="Print selected CIs but do not run them.")66parser.add_argument(67"branch",68type=str,69nargs="?",70default=current_branch,71help="Specify the branch to run. Default is current branch if available.",72)73
74args = parser.parse_args()75if not args.branch:76raise ValueError("Branch was unable to be inferred and must be specified")77
78return args79
80
81def _run_az_pipelines_command(command: typing.List[str]):82try:83az = "az.cmd" if is_windows() else "az"84az_output = subprocess.run([az, "pipelines", *command], capture_output=True, text=True, check=True)85except subprocess.CalledProcessError as cpe:86print(cpe)87print(cpe.stderr)88sys.exit(-1)89
90return az_output91
92
93def main():94args = _parse_args()95branch = args.branch96
97# To debug available pipelines:98# az_out = az_pipelines = _run_az_pipelines_command(["list"])99# pipeline_info = json.loads(az_out.stdout)100# print(pipeline_info)101
102pipelines = get_pipeline_names()103pipelines_to_run = []104if args.include:105values = [i.lower().strip() for i in args.include]106for p in pipelines:107include = False108for value in values:109if value in p.lower():110include = True111break112
113if include:114print(f"Including {p}")115pipelines_to_run.append(p)116else:117pipelines_to_run = pipelines118
119if args.exclude:120values = [e.lower().strip() for e in args.exclude]121cur_pipelines = pipelines_to_run122pipelines_to_run = []123for p in cur_pipelines:124exclude = False125for value in values:126if value in p.lower():127exclude = True128break129
130if exclude:131print(f"Excluding {p}")132else:133pipelines_to_run.append(p)134
135print(f"Pipelines to run for {args.branch}:")136for p in pipelines_to_run:137print(f"\t{p}")138
139if args.dry_run:140sys.exit(0)141
142for pipeline in pipelines_to_run:143az_out = _run_az_pipelines_command(["run", "--branch", branch, "--name", pipeline])144run_output = json.loads(az_out.stdout)145if "id" in run_output:146build_url = f"https://dev.azure.com/onnxruntime/onnxruntime/_build/results?buildId={run_output['id']}"147print(f"{pipeline} build results: {build_url}&view=results")148else:149raise ValueError("Build id was not found in az output:\n" + run_output)150
151
152if __name__ == "__main__":153main()154