From 0aae03cad3f25acd829f8a661872768b17a01285 Mon Sep 17 00:00:00 2001 From: PiX <69745008+pixincreate@users.noreply.github.com> Date: Sat, 23 Mar 2024 01:27:28 +0530 Subject: [PATCH] ci(gh-actions): setup ci for auto update --- .github/workflows/build.yml | 99 +++++++++++++++++++++++++++++++ .github/workflows/update_fork.yml | 67 +++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 .github/workflows/update_fork.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1f19e97d9463..9fe4cd0c9c92 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,6 +13,13 @@ on: pull_request: branches: [master] workflow_dispatch: + inputs: + make_release: + default: false + description: Make a forced release + required: false + type: boolean + workflow_call: jobs: build: @@ -24,6 +31,7 @@ jobs: - name: Check out uses: actions/checkout@v4 with: + fetch-depth: 0 submodules: "recursive" - name: Setup environment @@ -31,6 +39,18 @@ jobs: with: is-asset-build: true + - name: Setup keystores + run: | + cat > config.prop<< EOF + keyStore=key.jks + keyStorePass=${{ secrets.KEYSTORE_ALIAS_PASSWORD }} + keyAlias=${{ secrets.KEYSTORE_ALIAS_NAME }} + keyPass=${{ secrets.KEYSTORE_PASSWORD }} + EOF + + echo '${{ secrets.KEYSTORE_FILE }}' > keystore.jks.asc + gpg -d --passphrase '${{ secrets.KEYSTORE_PASSWORD_GPG }}' --batch keystore.jks.asc > key.jks + - name: Build release run: ./build.py -vr all @@ -47,6 +67,85 @@ jobs: path: out compression-level: 9 + - name: Check for Release + id: check_release + run: | + # Set default value for force_release if not set + force_release=${{ inputs.make_release }} + if [ -z "$force_release" ]; then + force_release="false" + fi + + NUM_RELEASES=$(git log -10 --oneline | grep 'Release new canary build' | wc -l) + RELEASE_COMMIT=$(( NUM_RELEASES + RELEASE_COMMIT )) + echo "RELEASE_COMMIT=$RELEASE_COMMIT" >> $GITHUB_ENV + + if [[ "${force_release}" == "true" ]]; then + (( RELEASE_COMMIT += 1 )) + echo "RELEASE_COMMIT=$RELEASE_COMMIT" >> $GITHUB_ENV + fi + + if [[ $RELEASE_COMMIT -gt 0 ]]; then + echo "Found recent canary build releases." + else + echo "No recent canary build releases found." + fi + + - name: Setup Git + if: ${{ env.RELEASE_COMMIT > 0 }} + run: git config --global user.email ${{ secrets.EMAIL }} && git config --global user.name PiX + + - name: Fetch commit hash and Magisk version + if: ${{ env.RELEASE_COMMIT > 0 }} + run: | + git fetch origin + echo "COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + + echo "MAGISK_VERSION=$(grep 'magisk.versionCode=' gradle.properties | awk -F= '{print $2}')" >> $GITHUB_ENV + + - name: Update configs + if: ${{ env.RELEASE_COMMIT > 0 }} + run: | + git checkout magisk-files + echo "Updating Configs for the new release" + + jq --arg version "${COMMIT_HASH}" \ + --arg versionCode "${MAGISK_VERSION}" \ + '.magisk.version = $version | + .magisk.versionCode = $versionCode | + .magisk.link |= sub("canary-[0-9]{5}"; "canary-" + $versionCode) | + .magisk.note |= sub("canary-[0-9]{5}"; "canary-" + $versionCode)' canary.json > canary.json.tmp + mv canary.json.tmp canary.json + + jq --arg version "${COMMIT_HASH}" \ + --arg versionCode "${MAGISK_VERSION}" \ + '.magisk.version = $version | + .magisk.versionCode = $versionCode | + .magisk.link |= sub("canary-[0-9]{5}"; "canary-" + $versionCode) | + .magisk.note |= sub("canary-[0-9]{5}"; "canary-" + $versionCode)' debug.json > debug.json.tmp + mv debug.json.tmp debug.json + + git add canary.json debug.json + git commit -m "release: new canary build ${COMMIT_HASH}" + git push origin magisk-files + git checkout master + + wget -qO- "https://github.com/topjohnwu/Magisk/releases/download/canary-${MAGISK_VERSION}/notes.md" >> notes.md + echo -e '\n## Diffs to Official Magisk\n\nAdded support for GrapheneOS for personal use' >> notes.md + + - name: Release APK + if: ${{ env.RELEASE_COMMIT > 0 }} + uses: "dciborow/action-github-releases@v1.0.1" + with: + repo_token: "${{ secrets.GITHUB_TOKEN }}" + automatic_release_tag: "canary-${{ env.MAGISK_VERSION }}" + prerelease: true + title: "Magisk (${{ env.COMMIT_HASH }}) (${{ env.MAGISK_VERSION }})" + files: | + out/app-release.apk + out/app-debug.apk + notes.md + - name: Upload mapping and native debug symbols uses: actions/upload-artifact@v4 with: diff --git a/.github/workflows/update_fork.yml b/.github/workflows/update_fork.yml new file mode 100644 index 000000000000..fc79d99f1368 --- /dev/null +++ b/.github/workflows/update_fork.yml @@ -0,0 +1,67 @@ +name: Update Fork + +on: + workflow_dispatch: + schedule: + - cron: "30 5 * * *" # runs everyday at 05:30 UTC + +permissions: + contents: write + actions: write + +jobs: + update_fork: + runs-on: ubuntu-latest + + steps: + - name: Checkout Forked Repo + uses: actions/checkout@v4 + with: + repository: pixincreate/Magisk + submodules: "recursive" + fetch-depth: 0 + + - name: Setup Git + run: git config --global user.email ${{ secrets.EMAIL }} && git config --global user.name PiX + + - name: Fetch from Upstream + run: | + git remote add upstream https://github.com/topjohnwu/Magisk.git + git fetch upstream master + upstream_commit="$(git rev-parse upstream/master)" + echo "Upstream latest commit: ${upstream_commit}" + for forked_commit in $(git rev-list -n 20 master); do + if [ $upstream_commit != $forked_commit ]; then + has_new_commits=true + continue + else + has_new_commits=false + break + fi + done + if [ $has_new_commits == "true" ]; then + git checkout master + if ! git rebase upstream/master; then + git diff + echo "ERROR: Merge conflict encountered during rebase!" + git rebase --abort + exit 1 + fi + git submodule update --init --recursive # Update the submodule + git push -f origin master + echo "Rebase successful!" + else + echo "ERROR: No commits to be synced!" + exit 1 + fi + + build_app: + needs: update_fork + uses: ./.github/workflows/build.yml + secrets: inherit + +# References: +# https://stackoverflow.com/questions/75192546/is-it-possible-to-call-another-workflow-file-from-another-workflow-files-condit/75225285#75225285 +# https://stackoverflow.com/questions/75191443/how-to-check-if-upstreams-head-latest-commit-is-present-in-forked-repo +# https://stackoverflow.com/questions/75191328/why-does-git-rebase-upstream-main-behaves-differently-when-used-github-actions +# https://stackoverflow.com/questions/62750603/github-actions-trigger-another-action-after-one-action-is-completed