name: PR Release Publish run-name: sign PR #${{ inputs.pr_number }} with the release key # This is deliberately manual and exists on main. PR CI builds without secrets. A maintainer # dispatches this workflow after reviewing the PR; it checks that the current head has a fresh # maintainer approval and a successful Build APK run before it calls the release signer. on: workflow_dispatch: inputs: pr_number: description: Open, approved pull request to sign and publish required: true type: number permissions: actions: read contents: read pull-requests: read concurrency: group: pr-release-${{ inputs.pr_number }} cancel-in-progress: false jobs: authorize: name: verify maintainer approval and PR build if: github.ref == 'refs/heads/main' runs-on: ubuntu-24.04 timeout-minutes: 10 outputs: run_id: ${{ steps.authorize.outputs.run_id }} pr_number: ${{ steps.authorize.outputs.pr_number }} head_sha: ${{ steps.authorize.outputs.head_sha }} label: ${{ steps.authorize.outputs.label }} apk_name: ${{ steps.authorize.outputs.apk_name }} steps: - uses: actions/checkout@v4 with: ref: main persist-credentials: false - name: Authorize this exact PR revision id: authorize env: GH_TOKEN: ${{ github.token }} PR_NUMBER: ${{ inputs.pr_number }} run: | set -euo pipefail [[ "$GITHUB_REF" == refs/heads/main ]] || { echo "::error::Run this workflow from main"; exit 1; } [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]] || { echo "::error::PR number must be a positive integer"; exit 1; } pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER") state=$(jq -r '.state' <<<"$pr") draft=$(jq -r '.draft' <<<"$pr") base_ref=$(jq -r '.base.ref' <<<"$pr") base_sha=$(jq -r '.base.sha' <<<"$pr") head_sha=$(jq -r '.head.sha' <<<"$pr") [[ "$state" == open && "$draft" == false && "$base_ref" == main ]] || { echo "::error::PR #$PR_NUMBER must be open, ready for review, and target main"; exit 1; } # Find the newest PR CI run for this commit. A newer failed or in-progress run prevents # signing even if an older attempt for the same commit passed. runs=$(gh run list --repo "$GITHUB_REPOSITORY" --workflow build.yml \ --event pull_request --commit "$head_sha" \ --json databaseId,headSha,status,conclusion,attempt,createdAt -L 100) latest=$(jq -ce --arg sha "$head_sha" ' [ .[] | select(.headSha == $sha) ] | sort_by([.createdAt, .attempt]) | last ' <<<"$runs") [[ $(jq -r '.status' <<<"$latest") == completed && $(jq -r '.conclusion' <<<"$latest") == success ]] || { echo "::error::The latest Build APK run for this PR commit is not green"; exit 1; } run_id=$(jq -r '.databaseId' <<<"$latest") run=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$run_id") run_pr=$(jq -ce --argjson number "$PR_NUMBER" ' [ .pull_requests[]? | select(.number == $number) ] | first ' <<<"$run") [[ $(jq -r '.event' <<<"$run") == pull_request && \ $(jq -r '.head_sha' <<<"$run") == "$head_sha" && \ $(jq -r '.status' <<<"$run") == completed && \ $(jq -r '.conclusion' <<<"$run") == success && \ $(jq -r '.head.sha' <<<"$run_pr") == "$head_sha" && \ $(jq -r '.base.ref' <<<"$run_pr") == main && \ $(jq -r '.base.sha' <<<"$run_pr") == "$base_sha" ]] || { echo "::error::The successful CI artifact is not for the current PR head and base"; exit 1; } # Use the last public stable APK as the versionCode baseline. Main can be ahead of the # shipped release, but testers still need to switch back to that release in place. sdk=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}} [[ -n "$sdk" && -d "$sdk/build-tools" ]] || { echo "::error::Android build-tools are unavailable"; exit 1; } build_tools=$(find "$sdk/build-tools" -mindepth 1 -maxdepth 1 -type d | sort -V | tail -1) stable_repo=The412Banner/DroidDeck stable_tag=$(gh api "repos/$stable_repo/releases/latest" --jq '.tag_name') [[ "$stable_tag" =~ ^[0-9A-Za-z._-]+$ ]] || { echo "::error::Could not identify the latest stable release"; exit 1; } mkdir -p "$RUNNER_TEMP/stable-apk" gh release download "$stable_tag" --repo "$stable_repo" \ --pattern "DroidDeck-$stable_tag.apk" --dir "$RUNNER_TEMP/stable-apk" stable_apk="$RUNNER_TEMP/stable-apk/DroidDeck-$stable_tag.apk" test -s "$stable_apk" || { echo "::error::The latest stable release has no standard APK"; exit 1; } stable_badging=$("$build_tools/aapt" dump badging "$stable_apk") stable_package=$(sed -n "s/^package: name='\\([^']*\\)'.*/\\1/p" <<<"$stable_badging") expected_code=$(sed -n "s/^package:.*versionCode='\\([^']*\\)'.*/\\1/p" <<<"$stable_badging") [[ "$stable_package" == com.droiddeck.launcher && "$expected_code" =~ ^[0-9]+$ ]] || { echo "::error::Could not read the standard package and versionCode from stable release $stable_tag"; exit 1; } mkdir -p "$RUNNER_TEMP/pr-apk" gh run download "$run_id" --repo "$GITHUB_REPOSITORY" --name droiddeck-apk --dir "$RUNNER_TEMP/pr-apk" mapfile -t apks < <(find "$RUNNER_TEMP/pr-apk" -type f -name '*.apk' -print) [[ ${#apks[@]} -eq 1 ]] || { echo "::error::Expected exactly one APK in droiddeck-apk"; exit 1; } badging=$("$build_tools/aapt" dump badging "${apks[0]}") apk_package=$(sed -n "s/^package: name='\\([^']*\\)'.*/\\1/p" <<<"$badging") apk_code=$(sed -n "s/^package:.*versionCode='\\([^']*\\)'.*/\\1/p" <<<"$badging") [[ "$apk_package" == com.droiddeck.launcher ]] || { echo "::error::Expected the standard package, got '$apk_package'"; exit 1; } [[ "$apk_code" == "$expected_code" ]] || { echo "::error::PR APK versionCode $apk_code differs from stable release $stable_tag versionCode $expected_code; keep PR builds at the stable code so testers can switch back in place"; exit 1; } # Check approval after the build and artifact checks, as close as possible to signing. # The reviewer must approve this exact head commit and have write or admin access. reviews=$(gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews?per_page=100" | jq 'add') approved_reviewers=$(jq -r --arg sha "$head_sha" ' [ .[] | select(.commit_id == $sha) | { login: .user.login, state: .state, submitted_at: .submitted_at } ] | sort_by(.submitted_at) | group_by(.login) | map(last) | map(select(.state == "APPROVED")) | .[].login ' <<<"$reviews") maintainer_approved=false while IFS= read -r reviewer; do [[ "$reviewer" =~ ^[A-Za-z0-9-]{1,39}$ ]] || continue if permission=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$reviewer/permission" --jq '.permission' 2>/dev/null); then case "$permission" in write|admin) maintainer_approved=true ;; esac fi done <<<"$approved_reviewers" [[ "$maintainer_approved" == true ]] || { echo "::error::No reviewer with write or admin access has approved the current PR commit $head_sha"; exit 1; } # Re-read PR head, base, and review decision after the permission lookups. latest_pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER") decision=$(gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --json reviewDecision --jq '.reviewDecision') [[ $(jq -r '.state' <<<"$latest_pr") == open && \ $(jq -r '.head.sha' <<<"$latest_pr") == "$head_sha" && \ $(jq -r '.base.sha' <<<"$latest_pr") == "$base_sha" && \ "$decision" == APPROVED ]] || { echo "::error::The PR or its approval changed while authorization was being checked"; exit 1; } short_sha=${head_sha:0:7} label="PR.$PR_NUMBER-$short_sha" apk_name="DroidDeck-$label.apk" { echo "run_id=$run_id" echo "pr_number=$PR_NUMBER" echo "head_sha=$head_sha" echo "label=$label" echo "apk_name=$apk_name" } >> "$GITHUB_OUTPUT" { echo "### Authorized PR signing" echo printf 'PR #%s at %s has an approving maintainer review and a green Build APK run (%s).\n' "$PR_NUMBER" "$short_sha" "$run_id" printf 'The APK versionCode matches stable release %s (%s).\n' "$stable_tag" "$expected_code" } >> "$GITHUB_STEP_SUMMARY" sign: name: sign with the release identity needs: authorize if: needs.authorize.result == 'success' uses: ./.github/workflows/sign.yml with: environment: signing-main variants: '["standard"]' run_id: ${{ needs.authorize.outputs.run_id }} label: ${{ needs.authorize.outputs.label }} artifact: droiddeck-pr-signed secrets: inherit publish: name: publish the approved PR APK needs: [authorize, sign] if: needs.authorize.result == 'success' && needs.sign.result == 'success' runs-on: ubuntu-24.04 timeout-minutes: 10 permissions: actions: read contents: read pull-requests: read steps: - uses: actions/checkout@v4 with: ref: main persist-credentials: false - uses: actions/download-artifact@v4 with: name: droiddeck-pr-signed-standard path: signed - name: Verify the release signer and checksum env: APK_NAME: ${{ needs.authorize.outputs.apk_name }} run: | set -euo pipefail apk="signed/$APK_NAME" test -s "$apk" || { echo "::error::Signed APK is missing: $APK_NAME"; exit 1; } expected_sha256=$(tr -d '[:space:]' < keystore/release-signer.sha256) [[ "$expected_sha256" =~ ^[[:xdigit:]]{64}$ ]] || { echo "::error::keystore/release-signer.sha256 is not a SHA-256 digest"; exit 1; } sdk=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}} [[ -n "$sdk" && -d "$sdk/build-tools" ]] || { echo "::error::Android build-tools are unavailable"; exit 1; } tools=$(find "$sdk/build-tools" -mindepth 1 -maxdepth 1 -type d | sort -V | tail -1) certs=$("$tools/apksigner" verify --min-sdk-version 28 --print-certs "$apk") digest=$(sed -n 's/.*certificate SHA-256 digest: //p' <<<"$certs" | sort -u) [[ "$digest" == "$expected_sha256" ]] || { printf '%s\n' "$certs" >&2 echo "::error::PR APK is not signed with the release certificate"; exit 1; } (cd signed && sha256sum -- "$APK_NAME" > SHA256SUMS.txt) cat signed/SHA256SUMS.txt - name: Publish to DroidDeck-CI when its token is configured env: GH_TOKEN: ${{ secrets.DROIDDECK_CI_TOKEN }} PR_NUMBER: ${{ needs.authorize.outputs.pr_number }} HEAD_SHA: ${{ needs.authorize.outputs.head_sha }} APK_NAME: ${{ needs.authorize.outputs.apk_name }} SOURCE_REPO: ${{ github.repository }} TARGET_REPO: Droid-Deck/DroidDeck-CI run: | set -euo pipefail if [ -z "$GH_TOKEN" ]; then echo "::warning::DROIDDECK_CI_TOKEN is not configured; the release-signed APK remains available as this workflow run's artifact" exit 0 fi pr=$(gh api "repos/$SOURCE_REPO/pulls/$PR_NUMBER") title=$(jq -r '.title' <<<"$pr" | tr '\r\n' ' ' | sed 's/[[:space:]]\+/ /g; s/ $//') url=$(jq -r '.html_url' <<<"$pr") signing_cert_sha256=$(tr -d '[:space:]' < keystore/release-signer.sha256) apk_sha256=$(awk 'NR == 1 { print $1 }' signed/SHA256SUMS.txt) tag="pr-$PR_NUMBER" notes=$(mktemp) trap 'rm -f "$notes"' EXIT { printf '# PR #%s\n\n' "$PR_NUMBER" printf 'Title: %s\n\n' "$title" printf 'Source PR: %s\n' "$url" printf "Source commit: \`%s\`\n" "$HEAD_SHA" printf "APK SHA-256: \`%s\`\n" "$apk_sha256" printf "Signed with DroidDeck release certificate SHA-256 \`%s\`.\n" "$signing_cert_sha256" printf 'This is a review-approved test build. Back up app data before installing test builds.\n' } > "$notes" if view_out=$(gh api "repos/$TARGET_REPO/releases/tags/$tag" 2>&1); then : elif printf '%s' "$view_out" | grep -qi 'Not Found'; then gh release create "$tag" --repo "$TARGET_REPO" --target main \ --title "PR #$PR_NUMBER: $title" --notes-file "$notes" --prerelease else printf '%s\n' "$view_out" exit 1 fi gh release upload "$tag" "signed/$APK_NAME" --repo "$TARGET_REPO" --clobber gh release edit "$tag" --repo "$TARGET_REPO" --title "PR #$PR_NUMBER: $title" --notes-file "$notes" --prerelease gh api "repos/$TARGET_REPO/releases/tags/$tag" | jq -r --arg keep "$APK_NAME" '.assets[] | select(.name | endswith(".apk")) | select(.name != $keep) | .id' | while IFS= read -r asset_id; do gh api --method DELETE "repos/$TARGET_REPO/releases/assets/$asset_id" >/dev/null done gh release view "$tag" --repo "$TARGET_REPO" --json url --jq '"Published: \(.url)"'