Skip to content

Commit f246791

Browse files
committed
preflight: add fork search on re-run, switch to jq
1 parent cbf3001 commit f246791

4 files changed

Lines changed: 151 additions & 23 deletions

File tree

.github/star_and_fork.jpg

-36 KB
Binary file not shown.

.github/workflows/build-macos-image.yml

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
id: check
4747
run: |
4848
# Third time's the charm
49-
if [ "$GITHUB_RUN_ATTEMPT" -ge 3 ] || [ "$GITHUB_RUN_NUMBER" -ge 3 ]; then
49+
if [ "$GITHUB_RUN_ATTEMPT" -ge 3 ]; then
5050
echo "_build=true" >> $GITHUB_OUTPUT
5151
exit 0
5252
fi
@@ -58,19 +58,85 @@ jobs:
5858
-H "Accept: application/vnd.github+json" \
5959
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
6060
"https://api.github.com/repos/LongQT-sea/macos-iso-builder/stargazers?per_page=100&page=${PAGE}")
61-
echo "$DATA" | grep -qi "\"login\".*\"$GITHUB_ACTOR\"" && FOUND=true && break
61+
echo "$DATA" | jq -e --arg user "$GITHUB_ACTOR" 'any(.[]; .login == $user)' > /dev/null && FOUND=true && break
6262
[ "$(echo "$DATA" | grep -c '"login"')" -lt 100 ] && break
6363
PAGE=$((PAGE + 1))
6464
done
6565
6666
if [ "$FOUND" = "false" ]; then
67-
echo "_build=false" >> $GITHUB_OUTPUT
68-
echo "## Before you build — check if someone already did it for you :point_down:" >> $GITHUB_STEP_SUMMARY
69-
echo "" >> $GITHUB_STEP_SUMMARY
70-
echo "Browse the **[Recent Forks List](https://github.com/LongQT-sea/mkmaciso/releases/latest)** — someone may have already built the macOS version you need." >> $GITHUB_STEP_SUMMARY
71-
echo "" >> $GITHUB_STEP_SUMMARY
72-
echo "Still need to build your own? Star [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder), then re-run the workflow :star:" >> $GITHUB_STEP_SUMMARY
73-
echo "<br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
67+
if [ "$GITHUB_RUN_ATTEMPT" -eq 1 ]; then
68+
echo "## Before you build — check if someone already did it for you :point_down:" >> $GITHUB_STEP_SUMMARY
69+
echo "Browse the **[Recent Forks List](https://github.com/LongQT-sea/macos-iso-builder/releases/latest)** — someone may have already built the macOS version you need." >> $GITHUB_STEP_SUMMARY
70+
echo "" >> $GITHUB_STEP_SUMMARY
71+
echo "> [!TIP]" >> $GITHUB_STEP_SUMMARY
72+
echo "> Too lazy to browse? Click **Re-run all jobs** and I'll search recent forks for you automatically. :mag:" >> $GITHUB_STEP_SUMMARY
73+
echo "> Otherwise, star :star: [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder) to go straight to build next time." >> $GITHUB_STEP_SUMMARY
74+
echo "> <br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
75+
else
76+
VERSION_NORMALIZED=$(echo "${{ inputs.macos_version }}" | tr ' ' '_')
77+
FORMAT="${{ inputs.image_format }}"
78+
FOUND_RUN_URL=""
79+
FOUND_ARTIFACT_NAME=""
80+
PAST_15_DAYS=$(date -u -d '15 days ago' +%Y-%m-%dT%H:%M:%SZ)
81+
PAGE=1
82+
83+
while true; do
84+
FORKS=$(curl -s \
85+
-H "Accept: application/vnd.github+json" \
86+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
87+
"https://api.github.com/repos/LongQT-sea/macos-iso-builder/forks?sort=newest&per_page=100&page=${PAGE}")
88+
[ "$(echo "$FORKS" | jq 'length')" -eq 0 ] && break
89+
RECENT=$(echo "$FORKS" | jq -r --arg date "$PAST_15_DAYS" \
90+
'.[] | select(.created_at > $date) | .full_name')
91+
[ -z "$RECENT" ] && break
92+
while IFS= read -r fork_full_name; do
93+
[ -z "$fork_full_name" ] && continue
94+
RUN_ID=$(curl -s \
95+
-H "Accept: application/vnd.github+json" \
96+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
97+
"https://api.github.com/repos/${fork_full_name}/actions/runs?status=success&per_page=10" \
98+
| jq -r --arg date "$PAST_15_DAYS" '
99+
.workflow_runs[]
100+
| select(.name == "Build Full Installer ISO/DMG image")
101+
| select(.created_at > $date)
102+
| .id' | head -1)
103+
[ -z "$RUN_ID" ] || [ "$RUN_ID" = "null" ] && continue
104+
MATCH=$(curl -s \
105+
-H "Accept: application/vnd.github+json" \
106+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
107+
"https://api.github.com/repos/${fork_full_name}/actions/runs/${RUN_ID}/artifacts" \
108+
| jq -r --arg v "macOS_${VERSION_NORMALIZED}" --arg f "$FORMAT" '
109+
.artifacts[]
110+
| select(.name | startswith($v) and (endswith($f) or endswith($f + ".img")))
111+
| select(.expired == false)
112+
| .name' | head -1)
113+
if [ -n "$MATCH" ] && [ "$MATCH" != "null" ]; then
114+
FOUND_RUN_URL="https://github.com/${fork_full_name}/actions/runs/${RUN_ID}"
115+
FOUND_ARTIFACT_NAME="$MATCH"
116+
break 2
117+
fi
118+
done < <(echo "$RECENT")
119+
PAGE=$((PAGE + 1))
120+
done
121+
122+
if [ -n "$FOUND_RUN_URL" ]; then
123+
echo "## Found **${FOUND_ARTIFACT_NAME}** in a recent fork" >> $GITHUB_STEP_SUMMARY
124+
echo "**[${FOUND_RUN_URL}](${FOUND_RUN_URL})**" >> $GITHUB_STEP_SUMMARY
125+
echo "" >> $GITHUB_STEP_SUMMARY
126+
echo "1. Open the link above" >> $GITHUB_STEP_SUMMARY
127+
echo "2. Scroll down to the **Artifacts** section" >> $GITHUB_STEP_SUMMARY
128+
echo "3. Click **${FOUND_ARTIFACT_NAME}** to download" >> $GITHUB_STEP_SUMMARY
129+
echo "" >> $GITHUB_STEP_SUMMARY
130+
echo "> [!TIP]" >> $GITHUB_STEP_SUMMARY
131+
echo "> Want to build your own latest version? Star :star: [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder) to skip this search and go straight to build next time." >> $GITHUB_STEP_SUMMARY
132+
echo "> <br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
133+
else
134+
echo "## No recent builds found in forks." >> $GITHUB_STEP_SUMMARY
135+
echo "Star :star: [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder) to skip this search and go straight to build next time." >> $GITHUB_STEP_SUMMARY
136+
echo "<br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
137+
echo "_build=true" >> $GITHUB_OUTPUT
138+
fi
139+
fi
74140
exit 0
75141
fi
76142

.github/workflows/build-recovery.yml

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
id: check
3939
run: |
4040
# Third time's the charm
41-
if [ "$GITHUB_RUN_ATTEMPT" -ge 3 ] || [ "$GITHUB_RUN_NUMBER" -ge 3 ]; then
41+
if [ "$GITHUB_RUN_ATTEMPT" -ge 3 ]; then
4242
echo "_build=true" >> $GITHUB_OUTPUT
4343
exit 0
4444
fi
@@ -50,19 +50,85 @@ jobs:
5050
-H "Accept: application/vnd.github+json" \
5151
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
5252
"https://api.github.com/repos/LongQT-sea/macos-iso-builder/stargazers?per_page=100&page=${PAGE}")
53-
echo "$DATA" | grep -qi "\"login\".*\"$GITHUB_ACTOR\"" && FOUND=true && break
53+
echo "$DATA" | jq -e --arg user "$GITHUB_ACTOR" 'any(.[]; .login == $user)' > /dev/null && FOUND=true && break
5454
[ "$(echo "$DATA" | grep -c '"login"')" -lt 100 ] && break
5555
PAGE=$((PAGE + 1))
5656
done
5757
5858
if [ "$FOUND" = "false" ]; then
59-
echo "_build=false" >> $GITHUB_OUTPUT
60-
echo "## Before you build — check if someone already did it for you :point_down:" >> $GITHUB_STEP_SUMMARY
61-
echo "" >> $GITHUB_STEP_SUMMARY
62-
echo "Browse the **[Recent Forks List](https://github.com/LongQT-sea/mkmaciso/releases/latest)** — someone may have already built the macOS version you need." >> $GITHUB_STEP_SUMMARY
63-
echo "" >> $GITHUB_STEP_SUMMARY
64-
echo "Still need to build your own? Star [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder), then re-run the workflow :star:" >> $GITHUB_STEP_SUMMARY
65-
echo "<br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
59+
if [ "$GITHUB_RUN_ATTEMPT" -eq 1 ]; then
60+
echo "## Before you build — check if someone already did it for you :point_down:" >> $GITHUB_STEP_SUMMARY
61+
echo "" >> $GITHUB_STEP_SUMMARY
62+
echo "Browse the **[Recent Forks List](https://github.com/LongQT-sea/macos-iso-builder)** — someone may have already built the macOS version you need." >> $GITHUB_STEP_SUMMARY
63+
echo "" >> $GITHUB_STEP_SUMMARY
64+
echo "> [!TIP]" >> $GITHUB_STEP_SUMMARY
65+
echo "> Too lazy to browse? Click **Re-run all jobs** and I'll search recent forks for you automatically. :mag:" >> $GITHUB_STEP_SUMMARY
66+
echo "> Otherwise, star :star: [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder) to go straight to build next time." >> $GITHUB_STEP_SUMMARY
67+
echo "> <br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
68+
else
69+
VERSION_NORMALIZED=$(echo "${{ inputs.macos_version }}" | tr ' ' '_')
70+
FOUND_RUN_URL=""
71+
FOUND_ARTIFACT_NAME=""
72+
PAST_15_DAYS=$(date -u -d '15 days ago' +%Y-%m-%dT%H:%M:%SZ)
73+
PAGE=1
74+
75+
while true; do
76+
FORKS=$(curl -s \
77+
-H "Accept: application/vnd.github+json" \
78+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
79+
"https://api.github.com/repos/LongQT-sea/macos-iso-builder/forks?sort=newest&per_page=100&page=${PAGE}")
80+
[ "$(echo "$FORKS" | jq 'length')" -eq 0 ] && break
81+
RECENT=$(echo "$FORKS" | jq -r --arg date "$PAST_15_DAYS" \
82+
'.[] | select(.created_at > $date) | .full_name')
83+
[ -z "$RECENT" ] && break
84+
while IFS= read -r fork_full_name; do
85+
[ -z "$fork_full_name" ] && continue
86+
RUN_ID=$(curl -s \
87+
-H "Accept: application/vnd.github+json" \
88+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
89+
"https://api.github.com/repos/${fork_full_name}/actions/runs?status=success&per_page=10" \
90+
| jq -r --arg date "$PAST_15_DAYS" '
91+
.workflow_runs[]
92+
| select(.name == "Build Recovery ISO image")
93+
| select(.created_at > $date)
94+
| .id' | head -1)
95+
[ -z "$RUN_ID" ] || [ "$RUN_ID" = "null" ] && continue
96+
MATCH=$(curl -s \
97+
-H "Accept: application/vnd.github+json" \
98+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
99+
"https://api.github.com/repos/${fork_full_name}/actions/runs/${RUN_ID}/artifacts" \
100+
| jq -r --arg v "macOS_${VERSION_NORMALIZED}_Recovery" '
101+
.artifacts[]
102+
| select(.name | startswith($v))
103+
| select(.expired == false)
104+
| .name' | head -1)
105+
if [ -n "$MATCH" ] && [ "$MATCH" != "null" ]; then
106+
FOUND_RUN_URL="https://github.com/${fork_full_name}/actions/runs/${RUN_ID}"
107+
FOUND_ARTIFACT_NAME="$MATCH"
108+
break 2
109+
fi
110+
done < <(echo "$RECENT")
111+
PAGE=$((PAGE + 1))
112+
done
113+
114+
if [ -n "$FOUND_RUN_URL" ]; then
115+
echo "## Found **${FOUND_ARTIFACT_NAME}** in a recent fork" >> $GITHUB_STEP_SUMMARY
116+
echo "**[${FOUND_RUN_URL}](${FOUND_RUN_URL})**" >> $GITHUB_STEP_SUMMARY
117+
echo "" >> $GITHUB_STEP_SUMMARY
118+
echo "1. Open the link above" >> $GITHUB_STEP_SUMMARY
119+
echo "2. Scroll down to the **Artifacts** section" >> $GITHUB_STEP_SUMMARY
120+
echo "3. Click **${FOUND_ARTIFACT_NAME}** to download" >> $GITHUB_STEP_SUMMARY
121+
echo "" >> $GITHUB_STEP_SUMMARY
122+
echo "> [!TIP]" >> $GITHUB_STEP_SUMMARY
123+
echo "> Want to build your own latest version? Star :star: [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder) to skip this search and go straight to build next time." >> $GITHUB_STEP_SUMMARY
124+
echo "> <br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
125+
else
126+
echo "## No recent builds found in forks." >> $GITHUB_STEP_SUMMARY
127+
echo "Star :star: [LongQT-sea/macos-iso-builder](https://github.com/LongQT-sea/macos-iso-builder) to skip this search and go straight to build next time." >> $GITHUB_STEP_SUMMARY
128+
echo "<br>![star](https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star.jpg)" >> $GITHUB_STEP_SUMMARY
129+
echo "_build=true" >> $GITHUB_OUTPUT
130+
fi
131+
fi
66132
exit 0
67133
fi
68134
@@ -182,9 +248,6 @@ jobs:
182248
if: github.event.inputs.release_tag != ''
183249
run: |
184250
tag="${{ github.event.inputs.release_tag }}"
185-
186-
# Replace spaces and other invalid characters with hyphens
187-
# Keep only alphanumeric, dots, hyphens, and underscores
188251
sanitized_tag=$(echo "$tag" | sed 's/[^a-zA-Z0-9._-]/-/g' | sed 's/--*/-/g' | sed 's/^-//;s/-$//')
189252
echo "sanitized_tag=$sanitized_tag" >> $GITHUB_OUTPUT
190253

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ Lion, Mountain Lion, Mavericks, Yosemite, El Capitan, Sierra, High Sierra, Mojav
4646
>
4747
> </details>
4848
49-
1. Star and [Fork](https://github.com/LongQT-sea/macos-iso-builder/fork) this repository (requires a GitHub account).<br>
50-
<img src="https://raw.githubusercontent.com/LongQT-sea/macos-iso-builder/main/.github/star_and_fork.jpg" width="500">
49+
1. [Click here](https://github.com/LongQT-sea/macos-iso-builder/fork) to Fork this repository (requires a GitHub account).
5150
2. Navigate to the **Actions** tab in your forked repository.
5251
3. Click the green **"I understand my workflows, go ahead and enable them"** button.
5352
4. Select a workflow from the left sidebar:

0 commit comments

Comments
 (0)