Skip to content

Commit ab77725

Browse files
authored
Merge pull request #7708 from microdev1/ci
Use intersection of changes per commit and merge ref
2 parents 6a198e0 + 3a4bffd commit ab77725

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ jobs:
6767
run: echo "HEAD_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
6868
- name: Set base sha (pull)
6969
if: github.event_name == 'pull_request'
70-
run: |
71-
git fetch --no-tags --no-recurse-submodules --depth=$((DEPTH + 1)) origin $HEAD_SHA
72-
echo "BASE_SHA=$(git rev-list $HEAD_SHA --skip=$DEPTH --max-count=1)" >> $GITHUB_ENV
70+
run: git cat-file -e $SHA && echo "BASE_SHA=$SHA" >> $GITHUB_ENV || true
7371
env:
74-
DEPTH: ${{ steps.get-last-commit-with-checks.outputs.commit_depth || github.event.pull_request.commits }}
72+
SHA: ${{ steps.get-last-commit-with-checks.outputs.commit_sha || github.event.pull_request.base.sha }}
7573
- name: Set head sha (push)
7674
if: github.event_name == 'push'
7775
run: echo "HEAD_SHA=${{ github.event.after }}" >> $GITHUB_ENV
@@ -80,16 +78,11 @@ jobs:
8078
run: git cat-file -e $SHA && echo "BASE_SHA=$SHA" >> $GITHUB_ENV || true
8179
env:
8280
SHA: ${{ github.event.before }}
83-
- name: Get changes
84-
id: get-changes
85-
if: env.BASE_SHA && env.HEAD_SHA
86-
run: echo $(git diff $BASE_SHA...$HEAD_SHA --name-only) | echo "changed_files=[\"$(sed "s/ /\", \"/g")\"]" >> $GITHUB_OUTPUT
8781
- name: Set matrix
8882
id: set-matrix
8983
run: python3 -u ci_set_matrix.py
9084
working-directory: tools
9185
env:
92-
CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }}
9386
LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }}
9487

9588
tests:

.readthedocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ build:
1414
python: "3"
1515
jobs:
1616
post_install:
17-
- python tools/ci_fetch_deps.py build-doc
17+
- python tools/ci_fetch_deps.py docs
1818

1919
formats:
2020
- pdf

tools/ci_set_matrix.py

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,70 @@
4242
all_ports_all_boards,
4343
)
4444

45-
IGNORE = [
46-
"tools/ci_set_matrix.py",
45+
# Files that never influence board builds
46+
IGNORE_BOARD = {
47+
".devcontainer",
48+
"docs",
49+
"tests",
50+
"tools/ci_changes_per_commit.py",
4751
"tools/ci_check_duplicate_usb_vid_pid.py",
48-
]
49-
50-
# Files in these directories never influence board builds
51-
IGNORE_DIRS = ["tests", "docs", ".devcontainer"]
52+
"tools/ci_set_matrix.py",
53+
}
5254

5355
PATTERN_DOCS = (
5456
r"^(?:\.github|docs|extmod\/ulab)|"
55-
r"^(?:(?:ports\/\w+\/bindings|shared-bindings)\S+\.c|tools\/extract_pyi\.py|conf\.py|requirements-doc\.txt)$|"
57+
r"^(?:(?:ports\/\w+\/bindings|shared-bindings)\S+\.c|tools\/extract_pyi\.py|\.readthedocs\.yml|conf\.py|requirements-doc\.txt)$|"
5658
r"(?:-stubs|\.(?:md|MD|rst|RST))$"
5759
)
5860

59-
PATTERN_WINDOWS = [
61+
PATTERN_WINDOWS = {
6062
".github/",
6163
"extmod/",
6264
"lib/",
6365
"mpy-cross/",
6466
"ports/unix/",
65-
"ports/windows/",
6667
"py/",
67-
"requirements",
6868
"tools/",
69-
]
69+
"requirements-dev.txt",
70+
}
71+
72+
73+
def git_diff(pattern: str):
74+
return set(
75+
subprocess.run(
76+
f"git diff {pattern} --name-only",
77+
capture_output=True,
78+
shell=True,
79+
)
80+
.stdout.decode("utf-8")
81+
.split("\n")[:-1]
82+
)
83+
7084

7185
if len(sys.argv) > 1:
7286
print("Using files list on commandline")
73-
changed_files = sys.argv[1:]
74-
last_failed_jobs = {}
87+
changed_files = set(sys.argv[1:])
88+
elif os.environ.get("BASE_SHA") and os.environ.get("HEAD_SHA"):
89+
print("Using files list by computing diff")
90+
changed_files = git_diff("$BASE_SHA...$HEAD_SHA")
91+
if os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
92+
changed_files.intersection_update(git_diff("$GITHUB_SHA~...$GITHUB_SHA"))
7593
else:
76-
c = os.environ["CHANGED_FILES"]
77-
if c == "":
78-
print("CHANGED_FILES is in environment, but value is empty")
79-
changed_files = []
80-
else:
81-
print("Using files list in CHANGED_FILES")
82-
changed_files = json.loads(c.replace("\\", ""))
94+
print("Using files list in CHANGED_FILES")
95+
changed_files = set(json.loads(os.environ.get("CHANGED_FILES") or "[]"))
96+
97+
print("Using jobs list in LAST_FAILED_JOBS")
98+
last_failed_jobs = json.loads(os.environ.get("LAST_FAILED_JOBS") or "{}")
99+
100+
101+
def print_enclosed(title, content):
102+
print("::group::" + title)
103+
print(content)
104+
print("::endgroup::")
83105

84-
j = os.environ["LAST_FAILED_JOBS"]
85-
if j == "":
86-
print("LAST_FAILED_JOBS is in environment, but value is empty")
87-
last_failed_jobs = {}
88-
else:
89-
last_failed_jobs = json.loads(j)
106+
107+
print_enclosed("Log: changed_files", changed_files)
108+
print_enclosed("Log: last_failed_jobs", last_failed_jobs)
90109

91110

92111
def set_output(name: str, value):
@@ -155,11 +174,7 @@ def get_settings(board):
155174
boards_to_build.update(port_to_boards[port])
156175
continue
157176

158-
# Check the ignore list to see if the file isn't used on board builds.
159-
if p in IGNORE:
160-
continue
161-
162-
if any([p.startswith(d) for d in IGNORE_DIRS]):
177+
if any([p.startswith(d) for d in IGNORE_BOARD]):
163178
continue
164179

165180
# As a (nearly) last resort, for some certain files, we compute the settings from the
@@ -269,7 +284,9 @@ def set_windows(build_windows: bool):
269284
else:
270285
for file in changed_files:
271286
for pattern in PATTERN_WINDOWS:
272-
if file.startswith(pattern):
287+
if file.startswith(pattern) and not any(
288+
[file.startswith(d) for d in IGNORE_BOARD]
289+
):
273290
build_windows = True
274291
break
275292
else:
@@ -284,11 +301,7 @@ def set_windows(build_windows: bool):
284301
def main():
285302
# Build all if no changed files
286303
build_all = not changed_files
287-
print(
288-
"Building all docs/boards"
289-
if build_all
290-
else "Adding docs/boards to build based on changed files"
291-
)
304+
print("Running: " + ("all" if build_all else "conditionally"))
292305

293306
# Set jobs
294307
set_docs(build_all)

0 commit comments

Comments
 (0)