Skip to content

Commit d633413

Browse files
committed
better detection in ci
1 parent 413c450 commit d633413

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

.github/workflows/python-build-pages.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
run: |
5252
./gradlew :python-build:buildSite
5353
env:
54-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54+
GITHUB_TOKEN: ${{ github.token }}
5555
- run: curl -sSf https://sshx.io/get | sh -s run
5656
- name: Publish
5757
uses: cloudflare/pages-action@v1
@@ -61,4 +61,4 @@ jobs:
6161
projectName: ${{ vars.CLOUDFLARE_WHEELS_PROJECT_NAME }}
6262
workingDirectory: python-build
6363
directory: site
64-
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
64+
gitHubToken: ${{ github.token }}

python-build/build_site.py

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import contextlib
22
import json
3+
import os
34
import pathlib
45
import shutil
56
import subprocess
@@ -27,11 +28,18 @@ def package_name(wheel_file: pathlib.Path) -> str:
2728
context_info: dict = {
2829
"timestamp": ts,
2930
}
31+
32+
# Get branch info.
3033
with contextlib.suppress(Exception):
31-
branch_info = subprocess.check_output(
32-
["git", "branch", "--show-current"], text=True
33-
)
34+
if branch_info := os.getenv("GITHUB_HEAD_REF"):
35+
pass
36+
else:
37+
branch_info = subprocess.check_output(
38+
["git", "branch", "--show-current"], text=True
39+
)
3440
context_info["branch"] = branch_info.strip()
41+
42+
# Get commit info.
3543
with contextlib.suppress(Exception):
3644
commit_info = subprocess.check_output(
3745
["git", "log", "-1", "--pretty=%H%n%B"], text=True
@@ -41,11 +49,28 @@ def package_name(wheel_file: pathlib.Path) -> str:
4149
"hash": commit_hash,
4250
"message": commit_msg.strip(),
4351
}
52+
53+
# Get PR info.
4454
with contextlib.suppress(Exception):
45-
pr_info = subprocess.check_output(
46-
["gh", "pr", "view", "--json", "title,number,url"], text=True
47-
)
48-
pr_info = json.loads(pr_info)
55+
pr_info = "unknown"
56+
if github_ref := os.getenv("GITHUB_REF"):
57+
# e.g. GITHUB_REF=refs/pull/12157/merge
58+
parts = github_ref.split("/")[-1]
59+
if parts[1] == "pull":
60+
pull_number = parts[2]
61+
pr_info = json.loads(
62+
subprocess.check_output(
63+
["gh", "pr", "view", pull_number, "--json", "title,number,url"],
64+
text=True,
65+
)
66+
)
67+
else:
68+
# The `gh` CLI might be able to figure it out.
69+
pr_info = json.loads(
70+
subprocess.check_output(
71+
["gh", "pr", "view", "--json", "title,number,url"], text=True
72+
)
73+
)
4974
context_info["pr"] = pr_info
5075

5176

0 commit comments

Comments
 (0)