Skip to content

Commit 1627e87

Browse files
committed
ci(gh-actions): setup ci for auto update
1 parent d9b2199 commit 1627e87

File tree

2 files changed

+210
-2
lines changed

2 files changed

+210
-2
lines changed

.github/workflows/build.yml

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,42 @@ on:
1111
pull_request:
1212
branches: [master]
1313
workflow_dispatch:
14+
inputs:
15+
make_release:
16+
default: false
17+
description: Make a forced release
18+
required: false
19+
type: boolean
20+
workflow_call:
1421

1522
jobs:
1623
build:
1724
name: Build Magisk artifacts
1825
runs-on: macos-15
19-
strategy:
20-
fail-fast: false
2126
steps:
2227
- name: Check out
2328
uses: actions/checkout@v4
2429
with:
30+
fetch-depth: 0
2531
submodules: "recursive"
2632

2733
- name: Setup environment
2834
uses: ./.github/actions/setup
2935
with:
3036
is-asset-build: true
3137

38+
- name: Setup keystores
39+
run: |
40+
cat > config.prop<< EOF
41+
keyStore=key.jks
42+
keyStorePass=${{ secrets.KEYSTORE_ALIAS_PASSWORD }}
43+
keyAlias=${{ secrets.KEYSTORE_ALIAS_NAME }}
44+
keyPass=${{ secrets.KEYSTORE_PASSWORD }}
45+
EOF
46+
47+
echo '${{ secrets.KEYSTORE_FILE }}' > keystore.jks.asc
48+
gpg -d --passphrase '${{ secrets.KEYSTORE_PASSWORD_GPG }}' --batch keystore.jks.asc > key.jks
49+
3250
- name: Build release
3351
run: ./build.py -vr all
3452

@@ -45,6 +63,129 @@ jobs:
4563
path: out
4664
compression-level: 9
4765

66+
- name: Check for Release
67+
id: check_release
68+
run: |
69+
git fetch --all --tags
70+
DO_RELEASE=false
71+
IS_PRERELEASE=true
72+
RELEASE_COMMIT_HASH=""
73+
COMMIT_RANGE_TO_SCAN=""
74+
75+
# Handle forced releases from workflow_dispatch first
76+
if [[ "${{ inputs.make_release }}" == "true" ]]; then
77+
DO_RELEASE=true
78+
RELEASE_COMMIT_HASH=${{ github.sha }}
79+
echo "Forcing a pre-release due to workflow input for commit $RELEASE_COMMIT_HASH."
80+
# Handle push and scheduled events
81+
elif [[ "${{ github.event_name }}" == "push" || "${{ github.event_name }}" == "schedule" ]]; then
82+
# On a new branch or a force-push, the before..after range is often invalid.
83+
# In these cases, we fall back to scanning the last N commits from HEAD.
84+
if [[ "${{ github.event.created }}" == "true" || "${{ github.event.forced }}" == "true" || "${{ github.event_name }}" == "schedule" ]]; then
85+
if [[ "${{ github.event.created }}" == "true" ]]; then
86+
echo "New branch detected. Scanning recent commits for a release."
87+
elif [[ "${{ github.event.forced }}" == "true" ]]; then
88+
echo "Force push detected. Scanning recent commits for a release."
89+
else
90+
echo "Scheduled run detected. Scanning recent commits for missed releases."
91+
fi
92+
# Scan the history of the current HEAD. Limit to 20 to be safe.
93+
COMMIT_RANGE_TO_SCAN="--max-count=20 ${{ github.sha }}"
94+
else
95+
# For a normal push to an existing branch, the range is reliable.
96+
COMMIT_RANGE_TO_SCAN="${{ github.event.before }}..${{ github.sha }}"
97+
echo "Scanning commit range: $COMMIT_RANGE_TO_SCAN"
98+
fi
99+
100+
# The `|| true` prevents the script from exiting if git log returns a non-zero status (e.g., invalid range)
101+
# Search for both "Release Magisk" and "Release new canary build" commits
102+
RELEASE_COMMIT_HASH=$(git log $COMMIT_RANGE_TO_SCAN --grep="Release Magisk" --grep="Release new canary build" -E --max-count=1 --pretty=%H || true)
103+
104+
if [[ -n "$RELEASE_COMMIT_HASH" ]]; then
105+
DO_RELEASE=true
106+
echo "Found release-triggering commit: $RELEASE_COMMIT_HASH"
107+
fi
108+
fi
109+
110+
if [[ "$DO_RELEASE" == "true" ]]; then
111+
LATEST_TAG=$(git describe --tags --exact-match $RELEASE_COMMIT_HASH 2>/dev/null || true)
112+
113+
# Determine if this is a stable release based on tag format
114+
if [[ -n "$LATEST_TAG" && "$LATEST_TAG" =~ ^v[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
115+
# Major version release (e.g., v28.1, v29.0) - not a pre-release
116+
IS_PRERELEASE=false
117+
echo "Detected stable release tag: $LATEST_TAG"
118+
else
119+
# Canary, beta, or forced releases are pre-releases
120+
IS_PRERELEASE=true
121+
echo "Detected pre-release tag: $LATEST_TAG (or untagged forced release)"
122+
fi
123+
124+
echo "DO_RELEASE=true" >> $GITHUB_ENV
125+
echo "IS_PRERELEASE=$IS_PRERELEASE" >> $GITHUB_ENV
126+
echo "RELEASE_COMMIT_HASH=$RELEASE_COMMIT_HASH" >> $GITHUB_ENV
127+
echo "RELEASE_TAG=$LATEST_TAG" >> $GITHUB_ENV
128+
else
129+
echo "No release-triggering commit or input found."
130+
echo "DO_RELEASE=false" >> $GITHUB_ENV
131+
fi
132+
133+
- name: Setup Git
134+
if: ${{ env.DO_RELEASE == 'true' }}
135+
run: git config --global user.email ${{ secrets.EMAIL }} && git config --global user.name PiX
136+
137+
- name: Fetch Release Info
138+
if: ${{ env.DO_RELEASE == 'true' }}
139+
id: fetch_info
140+
run: |
141+
COMMIT_HASH=$(git rev-parse --short $RELEASE_COMMIT_HASH)
142+
MAGISK_VERSION=$(grep 'magisk.versionCode=' app/gradle.properties | awk -F= '{print $2}')
143+
echo "COMMIT_HASH=$COMMIT_HASH" >> $GITHUB_ENV
144+
echo "MAGISK_VERSION=$MAGISK_VERSION" >> $GITHUB_ENV
145+
146+
# Use the tag found in the previous step, or create a fallback for untagged forced releases
147+
RELEASE_TAG=${{ env.RELEASE_TAG }}
148+
if [[ -z "$RELEASE_TAG" ]]; then
149+
RELEASE_TAG="canary-$MAGISK_VERSION"
150+
fi
151+
echo "RELEASE_TAG=$RELEASE_TAG" >> $GITHUB_ENV
152+
153+
# Construct a release title
154+
if [[ "$RELEASE_TAG" == v* ]]; then
155+
RELEASE_TITLE="Magisk $RELEASE_TAG"
156+
else
157+
RELEASE_TITLE="Magisk ($COMMIT_HASH) ($MAGISK_VERSION)"
158+
fi
159+
echo "RELEASE_TITLE=$RELEASE_TITLE" >> $GITHUB_ENV
160+
161+
- name: Create Release Notes
162+
if: ${{ env.DO_RELEASE == 'true' }}
163+
run: |
164+
# Try to extract notes from the annotated git tag of the release commit
165+
if [[ -n "${{ env.RELEASE_TAG }}" ]]; then
166+
git tag -l --format='%(contents)' "${{ env.RELEASE_TAG }}" > notes.md
167+
fi
168+
# If notes are still empty, add a placeholder
169+
if [ ! -s notes.md ]; then
170+
echo "This is a new build of Magisk." > notes.md
171+
fi
172+
echo -e '\n## Diffs to Official Magisk\n\nAdded support for GrapheneOS for personal use' >> notes.md
173+
echo "Generated Release Notes:"
174+
cat notes.md
175+
176+
- name: Release APK
177+
if: ${{ env.DO_RELEASE == 'true' && env.RELEASE_TAG != '' }}
178+
uses: "dciborow/[email protected]"
179+
with:
180+
repo_token: "${{ secrets.GITHUB_TOKEN }}"
181+
automatic_release_tag: "${{ env.RELEASE_TAG }}"
182+
prerelease: ${{ env.IS_PRERELEASE }}
183+
title: "${{ env.RELEASE_TITLE }}"
184+
files: |
185+
out/app-release.apk
186+
out/app-debug.apk
187+
notes.md
188+
48189
- name: Upload mapping and native debug symbols
49190
uses: actions/upload-artifact@v4
50191
with:

.github/workflows/update_fork.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Update Fork
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "30 5 */3 * *" # runs once in 3 days at 05:30 UTC
7+
8+
permissions:
9+
contents: write
10+
actions: write
11+
12+
jobs:
13+
update_fork:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout Forked Repo
18+
uses: actions/checkout@v4
19+
with:
20+
repository: pixincreate/Magisk
21+
submodules: "recursive"
22+
fetch-depth: 0
23+
24+
- name: Setup Git
25+
run: git config --global user.email ${{ secrets.EMAIL }} && git config --global user.name PiX
26+
27+
- name: Fetch from Upstream
28+
run: |
29+
git remote add upstream https://github.com/topjohnwu/Magisk.git
30+
git fetch upstream master
31+
upstream_commit="$(git rev-parse upstream/master)"
32+
echo "Upstream latest commit: ${upstream_commit}"
33+
for forked_commit in $(git rev-list -n 20 master); do
34+
if [ $upstream_commit != $forked_commit ]; then
35+
has_new_commits=true
36+
continue
37+
else
38+
has_new_commits=false
39+
break
40+
fi
41+
done
42+
if [ $has_new_commits == "true" ]; then
43+
git checkout master
44+
if ! git rebase upstream/master; then
45+
git diff
46+
echo "ERROR: Merge conflict encountered during rebase!"
47+
git rebase --abort
48+
exit 1
49+
fi
50+
git submodule update --init --recursive # Update the submodule
51+
git push -f origin master
52+
echo "Rebase successful!"
53+
else
54+
echo "ERROR: No commits to be synced!"
55+
exit 1
56+
fi
57+
58+
build_app:
59+
needs: update_fork
60+
uses: ./.github/workflows/build.yml
61+
secrets: inherit
62+
63+
# References:
64+
# https://stackoverflow.com/questions/75192546/is-it-possible-to-call-another-workflow-file-from-another-workflow-files-condit/75225285#75225285
65+
# https://stackoverflow.com/questions/75191443/how-to-check-if-upstreams-head-latest-commit-is-present-in-forked-repo
66+
# https://stackoverflow.com/questions/75191328/why-does-git-rebase-upstream-main-behaves-differently-when-used-github-actions
67+
# https://stackoverflow.com/questions/62750603/github-actions-trigger-another-action-after-one-action-is-completed

0 commit comments

Comments
 (0)