onnxruntime

Форк
0
/
run_CIs_for_branch.py 
153 строки · 5.1 Кб
1
#!/usr/bin/env python3
2
# Copyright (c) Microsoft Corporation. All rights reserved.
3
# Licensed under the MIT License.
4

5
import argparse
6
import json
7
import os
8
import subprocess
9
import sys
10
import typing
11

12
from run_CIs_for_external_pr import get_pipeline_names
13
from util.platform_helpers import is_windows
14

15

16
class DefaultArgsRawHelpFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter):
17
    pass
18

19

20
def _parse_args():
21
    parser = argparse.ArgumentParser(
22
        os.path.basename(__file__),
23
        formatter_class=DefaultArgsRawHelpFormatter,
24
        description="""Run the CIs used to validate PRs for the specified branch.
25

26
        If not specified, the branch will be inferred (if possible) by running `git branch --show-current`.
27

28
        If 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

31
        Requires the Azure CLI with DevOps extension to be installed.
32
          Azure CLI: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli
33
          DevOps extension: https://github.com/Azure/azure-devops-cli-extension
34

35
        Configuration:
36
          Login:`az login`
37
          Configure ORT repo as default:
38
            `az devops configure --defaults organization=https://dev.azure.com/onnxruntime project=onnxruntime`
39

40
        Example usage:
41
          List all CIs
42
            `python run_CIs_for_branch.py --dry-run my/BranchName`
43
          Run all CIs
44
            `python run_CIs_for_branch.py my/BranchName`
45
          Run only Linux CIs
46
            `python run_CIs_for_branch.py --include linux my/BranchName`
47
          Exclude training CIs
48
            `python run_CIs_for_branch.py --exclude training my/BranchName`
49
          Run non-training Linux CIs
50
            `python run_CIs_for_branch.py --include linux --exclude training my/BranchName`
51
        """,
52
    )
53

54
    current_branch = None
55
    get_branch_result = subprocess.run(["git", "branch", "--show-current"], capture_output=True, text=True, check=False)
56
    if get_branch_result.returncode == 0:
57
        current_branch = get_branch_result.stdout.strip()
58

59
    parser.add_argument(
60
        "-i", "--include", action="append", type=str, help="Include CIs that match this string. Case insensitive."
61
    )
62
    parser.add_argument(
63
        "-e", "--exclude", action="append", type=str, help="Exclude CIs that match this string. Case insensitive."
64
    )
65
    parser.add_argument("--dry-run", action="store_true", help="Print selected CIs but do not run them.")
66
    parser.add_argument(
67
        "branch",
68
        type=str,
69
        nargs="?",
70
        default=current_branch,
71
        help="Specify the branch to run. Default is current branch if available.",
72
    )
73

74
    args = parser.parse_args()
75
    if not args.branch:
76
        raise ValueError("Branch was unable to be inferred and must be specified")
77

78
    return args
79

80

81
def _run_az_pipelines_command(command: typing.List[str]):
82
    try:
83
        az = "az.cmd" if is_windows() else "az"
84
        az_output = subprocess.run([az, "pipelines", *command], capture_output=True, text=True, check=True)
85
    except subprocess.CalledProcessError as cpe:
86
        print(cpe)
87
        print(cpe.stderr)
88
        sys.exit(-1)
89

90
    return az_output
91

92

93
def main():
94
    args = _parse_args()
95
    branch = args.branch
96

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

102
    pipelines = get_pipeline_names()
103
    pipelines_to_run = []
104
    if args.include:
105
        values = [i.lower().strip() for i in args.include]
106
        for p in pipelines:
107
            include = False
108
            for value in values:
109
                if value in p.lower():
110
                    include = True
111
                    break
112

113
            if include:
114
                print(f"Including {p}")
115
                pipelines_to_run.append(p)
116
    else:
117
        pipelines_to_run = pipelines
118

119
    if args.exclude:
120
        values = [e.lower().strip() for e in args.exclude]
121
        cur_pipelines = pipelines_to_run
122
        pipelines_to_run = []
123
        for p in cur_pipelines:
124
            exclude = False
125
            for value in values:
126
                if value in p.lower():
127
                    exclude = True
128
                    break
129

130
            if exclude:
131
                print(f"Excluding {p}")
132
            else:
133
                pipelines_to_run.append(p)
134

135
    print(f"Pipelines to run for {args.branch}:")
136
    for p in pipelines_to_run:
137
        print(f"\t{p}")
138

139
    if args.dry_run:
140
        sys.exit(0)
141

142
    for pipeline in pipelines_to_run:
143
        az_out = _run_az_pipelines_command(["run", "--branch", branch, "--name", pipeline])
144
        run_output = json.loads(az_out.stdout)
145
        if "id" in run_output:
146
            build_url = f"https://dev.azure.com/onnxruntime/onnxruntime/_build/results?buildId={run_output['id']}"
147
            print(f"{pipeline} build results: {build_url}&view=results")
148
        else:
149
            raise ValueError("Build id was not found in az output:\n" + run_output)
150

151

152
if __name__ == "__main__":
153
    main()
154

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.