Skip to content

Commit e57d6af

Browse files
authored
Packaging on Jenkins (#3263)
1 parent 8b6155f commit e57d6af

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

azure-sdk-tools/packaging_tools/__init__.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
2+
import os
23
from pathlib import Path
3-
from typing import Dict, Any
4+
from typing import Dict, Any, Optional, List
45

56
from jinja2 import Template, PackageLoader, Environment
67
from .conf import read_conf, build_default_conf, CONF_NAME
@@ -29,7 +30,28 @@ def build_config(config : Dict[str, Any]) -> Dict[str, str]:
2930
# Return result
3031
return result
3132

32-
def build_packaging(package_name: str, output_folder: str, build_conf: bool = False) -> None:
33+
34+
def build_packaging(output_folder: str, gh_token: Optional[str]=None, jenkins: bool = False, packages: List[str]=None, build_conf: bool = False) -> None:
35+
package_names = set(packages) or set()
36+
if jenkins:
37+
sdk_id = os.environ["ghprbGhRepository"]
38+
pr_number = int(os.environ["ghprbPullId"])
39+
40+
from github import Github
41+
con = Github(gh_token)
42+
repo = con.get_repo(sdk_id)
43+
sdk_pr = repo.get_pull(pr_number)
44+
# "get_files" of Github only download the first 300 files. Might not be enough.
45+
package_names |= {f.filename.split('/')[0] for f in sdk_pr.get_files() if f.filename.startswith("azure")}
46+
47+
if not package_names:
48+
raise ValueError("Was unable to find out the package names.")
49+
50+
for package_name in package_names:
51+
build_packaging_by_package_name(package_name, output_folder, build_conf)
52+
53+
54+
def build_packaging_by_package_name(package_name: str, output_folder: str, build_conf: bool = False) -> None:
3355
_LOGGER.info("Building template %s", package_name)
3456
package_folder = Path(output_folder) / Path(package_name)
3557

azure-sdk-tools/packaging_tools/__main__.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import logging
3+
import os
34
import sys
45

56
from . import build_packaging
@@ -23,16 +24,28 @@
2324
parser.add_argument("--build-conf",
2425
dest="build_conf", action="store_true",
2526
help="Build a default TOML file, with package name, fake pretty name, as beta package and no doc page. Do nothing if the file exists, remove manually the file if needed.")
26-
parser.add_argument('package_name', help='The package name.')
27+
parser.add_argument("--jenkins",
28+
dest="jenkins", action="store_true",
29+
help="In Jenkins mode, try to find what to generate from Jenkins env variables. Package names are then optional.")
30+
parser.add_argument('package_names', nargs='*', help='The package name.')
2731

2832
args = parser.parse_args()
2933

3034
main_logger = logging.getLogger()
3135
logging.basicConfig()
3236
main_logger.setLevel(logging.DEBUG if args.debug else logging.INFO)
3337

38+
if not args.package_names and not args.jenkins:
39+
raise ValueError("At least one package name or Jenkins mode is required")
40+
3441
try:
35-
build_packaging(args.package_name, args.output, build_conf=args.build_conf)
42+
build_packaging(
43+
args.output,
44+
os.environ.get("GH_TOKEN", None),
45+
args.jenkins,
46+
args.package_names,
47+
build_conf=args.build_conf
48+
)
3649
except Exception as err:
3750
if args.debug:
3851
_LOGGER.exception(err)

0 commit comments

Comments
 (0)