diff --git a/.github/workflows/build-and-release.yml b/.github/workflows/build-and-release.yml new file mode 100644 index 00000000..7eb2a546 --- /dev/null +++ b/.github/workflows/build-and-release.yml @@ -0,0 +1,244 @@ +name: Build and Release + +on: + workflow_dispatch: + inputs: &release-inputs + release_ref: + description: Branch or tag to build from + required: false + default: master + publish_release: + description: Publish the GitHub Release immediately (disable to keep it as a draft) + type: boolean + default: true + prerelease: + description: Mark the GitHub Release as a pre-release + type: boolean + default: false + workflow_call: + inputs: *release-inputs + +concurrency: + group: build-and-release-${{ github.ref_name }} + cancel-in-progress: false + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + + env: + RELEASE_REF: ${{ inputs.release_ref || 'master' }} + + steps: + - name: Checkout source + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ env.RELEASE_REF }} + + - name: Ensure release ref is merged into master + shell: bash + run: | + set -euo pipefail + git fetch origin master + if ! git merge-base --is-ancestor HEAD origin/master; then + echo "::error title=Release blocked::${RELEASE_REF} is not merged into master. Merge the release PR first." + exit 1 + fi + + - name: Checkout published F-Droid repo (if available) + id: checkout_pages + continue-on-error: true + uses: actions/checkout@v4 + with: + ref: gh-pages + path: gh-pages + + - name: Restore previous F-Droid artifacts + if: steps.checkout_pages.outcome == 'success' + run: | + set -euo pipefail + if [[ -d gh-pages/fdroid ]]; then + rm -rf repo + mkdir -p repo + cp -a gh-pages/fdroid/. repo/ + else + mkdir -p repo + fi + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: '17' + cache: gradle + + - name: Validate signing configuration + id: signing + shell: bash + env: + KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }} + KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} + KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} + KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + run: | + set -euo pipefail + missing=0 + for var in KEYSTORE_B64 KEYSTORE_PASSWORD KEY_ALIAS KEY_PASSWORD; do + if [[ -z "${!var:-}" ]]; then + echo "::error title=Missing secret::${var} is not set." + missing=1 + fi + done + if [[ "$missing" -ne 0 ]]; then + exit 1 + fi + + echo "$KEYSTORE_B64" | base64 -d > my-release-key.keystore + chmod 600 my-release-key.keystore + + { + echo "keystore_password=$KEYSTORE_PASSWORD" + echo "key_alias=$KEY_ALIAS" + echo "key_password=$KEY_PASSWORD" + } >> "$GITHUB_ENV" + + - name: Capture version metadata + id: version + shell: bash + run: | + set -euo pipefail + python3 <<'PY' + import os + import pathlib + import re + + gradle = pathlib.Path("presentation/build.gradle").read_text() + code_match = re.search(r"^\s*versionCode\s+(\d+)", gradle, re.MULTILINE) + name_match = re.search(r"^\s*versionName\s+['\"]([\d\.]+)['\"]", gradle, re.MULTILINE) + if not code_match or not name_match: + raise SystemExit("Unable to read versionCode/versionName from presentation/build.gradle") + out = pathlib.Path(os.environ["GITHUB_OUTPUT"]) + out.write_text(f"version_code={code_match.group(1)}\nversion_name={name_match.group(1)}\ntag_name=v{name_match.group(1)}\n") + PY + + - name: Validate Gradle Wrapper + uses: gradle/wrapper-validation-action@v1 + + - name: Build signed release APK + run: ./gradlew clean assembleRelease + + - name: Locate release artifact + id: artifact + shell: bash + run: | + set -euo pipefail + shopt -s nullglob + files=(presentation/build/outputs/apk/release/QUIK-v*-release.apk) + if [[ "${#files[@]}" -ne 1 ]]; then + echo "::error title=APK lookup failed::Expected 1 APK, found ${#files[@]} (${files[*]:-none})." + exit 1 + fi + echo "apk_path=${files[0]}" >> "$GITHUB_OUTPUT" + + - name: Generate release notes + id: notes + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const tagName = 'v${{ steps.version.outputs.version_name }}'; + const {owner, repo} = context.repo; + + const {data: tags} = await github.rest.repos.listTags({ + owner, + repo, + per_page: 100, + }); + + const previousTag = tags.find(tag => /^v\d/.test(tag.name) && tag.name !== tagName)?.name; + + const payload = { + owner, + repo, + tag_name: tagName, + target_commitish: '${{ env.RELEASE_REF }}', + }; + + if (previousTag) { + payload.previous_tag_name = previousTag; + } + + const response = await github.request('POST /repos/{owner}/{repo}/releases/generate-notes', payload); + const notes = response.data.body || ''; + core.setOutput('release_notes', notes); + + - name: Sanitize release notes + id: clean_notes + shell: bash + env: + NOTES: ${{ steps.notes.outputs.release_notes }} + run: | + set -euo pipefail + python3 <<'PY' + import os + import pathlib + import re + + notes = os.environ.get("NOTES", "") + clean = re.sub(r'(?> "$GITHUB_OUTPUT" + + - name: Publish GitHub release + uses: softprops/action-gh-release@v2 + with: + tag_name: ${{ steps.version.outputs.tag_name }} + name: QUIK v${{ steps.version.outputs.version_name }} + draft: ${{ inputs.publish_release != true }} + prerelease: ${{ inputs.prerelease }} + body: ${{ steps.clean_notes.outputs.clean_notes }} + files: ${{ steps.artifact.outputs.apk_path }} + fail_on_unmatched_files: true + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Install F-Droid tooling + run: | + sudo apt-get update + sudo apt-get install -y fdroidserver aapt apksigner + + - name: Update F-Droid repo contents + env: + APK_PATH: ${{ steps.artifact.outputs.apk_path }} + VERSION_NAME: ${{ steps.version.outputs.version_name }} + VERSION_CODE: ${{ steps.version.outputs.version_code }} + run: | + set -euo pipefail + + mkdir -p repo + target_apk="repo/dev.octoshrimpy.quik_${VERSION_CODE}.apk" + cp "$APK_PATH" "$target_apk" + + fdroid update --config config.yml --pretty + + - name: Publish F-Droid repo to GitHub Pages + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_branch: gh-pages + publish_dir: repo + destination_dir: fdroid + user_name: github-actions[bot] + user_email: github-actions[bot]@users.noreply.github.com + + - name: Cleanup signing key + if: always() + run: rm -f my-release-key.keystore diff --git a/.github/workflows/release-after-merge.yml b/.github/workflows/release-after-merge.yml new file mode 100644 index 00000000..3f7e8e5d --- /dev/null +++ b/.github/workflows/release-after-merge.yml @@ -0,0 +1,197 @@ +name: Release After Merge + +on: + pull_request: + types: + - closed + branches: + - master + +jobs: + release: + if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release/') + uses: ./.github/workflows/build-and-release.yml + with: + release_ref: master + publish_release: true + prerelease: false + secrets: inherit + + fdroid-mr: + if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release/') + needs: release + runs-on: ubuntu-latest + env: + FDROID_FORK: ${{ vars.FDROID_GITLAB_FORK }} + FDROID_TARGET: ${{ vars.FDROID_GITLAB_TARGET != '' && vars.FDROID_GITLAB_TARGET || 'fdroid/fdroiddata' }} + PR_URL: ${{ github.event.pull_request.html_url }} + PR_NUMBER: ${{ github.event.pull_request.number }} + steps: + - name: Validate configuration + id: cfg + shell: bash + env: + TOKEN: ${{ secrets.FDROID_GITLAB_TOKEN }} + run: | + set -euo pipefail + missing=0 + if [[ -z "${FDROID_FORK:-}" ]]; then + echo "::warning title=FDroid MR skipped::Set repository variable FDROID_GITLAB_FORK (e.g. user/fdroiddata)." + missing=1 + fi + if [[ -z "${TOKEN:-}" ]]; then + echo "::warning title=FDroid MR skipped::Set secret FDROID_GITLAB_TOKEN with a GitLab PAT (api scope)." + missing=1 + fi + if [[ $missing -ne 0 ]]; then + echo "ready=false" >> "$GITHUB_OUTPUT" + else + echo "ready=true" >> "$GITHUB_OUTPUT" + fi + + - name: Check out repository + if: steps.cfg.outputs.ready == 'true' + uses: actions/checkout@v4 + with: + ref: master + + - name: Determine release version + if: steps.cfg.outputs.ready == 'true' + id: version + shell: bash + run: | + set -euo pipefail + python3 <<'PY' + import os + import pathlib + import re + + gradle = pathlib.Path("presentation/build.gradle").read_text() + code_match = re.search(r"^\s*versionCode\s+(\d+)", gradle, re.MULTILINE) + name_match = re.search(r"^\s*versionName\s+['\"]([\d\.]+)['\"]", gradle, re.MULTILINE) + if not code_match or not name_match: + raise SystemExit("Unable to read versionCode/versionName from presentation/build.gradle") + version_name = name_match.group(1) + version_code = code_match.group(1) + branch = f"fdroid-release-v{version_name}" + out = pathlib.Path(os.environ["GITHUB_OUTPUT"]) + out.write_text(f"version_name={version_name}\nversion_code={version_code}\nbranch_name={branch}\n") + PY + + - name: Sync fdroiddata fork + if: steps.cfg.outputs.ready == 'true' + env: + TOKEN: ${{ secrets.FDROID_GITLAB_TOKEN }} + BRANCH_NAME: ${{ steps.version.outputs.branch_name }} + run: | + set -euo pipefail + git clone "https://oauth2:${TOKEN}@gitlab.com/${FDROID_FORK}.git" fdroiddata + cd fdroiddata + git remote add upstream "https://gitlab.com/${FDROID_TARGET}.git" + git fetch upstream master + git checkout -B "${BRANCH_NAME}" upstream/master + + - name: Update metadata + if: steps.cfg.outputs.ready == 'true' + env: + BRANCH_NAME: ${{ steps.version.outputs.branch_name }} + run: | + set -euo pipefail + cp fdroid/metadata/dev.octoshrimpy.quik.yml fdroiddata/metadata/dev.octoshrimpy.quik.yml + cd fdroiddata + if git diff --quiet; then + echo "no_changes=true" >> "$GITHUB_ENV" + else + echo "no_changes=false" >> "$GITHUB_ENV" + fi + + - name: Commit and push metadata update + if: steps.cfg.outputs.ready == 'true' && env.no_changes == 'false' + env: + TOKEN: ${{ secrets.FDROID_GITLAB_TOKEN }} + VERSION_NAME: ${{ steps.version.outputs.version_name }} + BRANCH_NAME: ${{ steps.version.outputs.branch_name }} + run: | + set -euo pipefail + cd fdroiddata + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add metadata/dev.octoshrimpy.quik.yml + git commit -m "Update QUIK to v${VERSION_NAME}" + git push --force-with-lease origin "${BRANCH_NAME}" + + - name: Create GitLab Merge Request + if: steps.cfg.outputs.ready == 'true' && env.no_changes == 'false' + env: + TOKEN: ${{ secrets.FDROID_GITLAB_TOKEN }} + BRANCH_NAME: ${{ steps.version.outputs.branch_name }} + VERSION_NAME: ${{ steps.version.outputs.version_name }} + PR_URL: ${{ env.PR_URL }} + TARGET_PROJECT: ${{ env.FDROID_TARGET }} + FORK_PROJECT: ${{ env.FDROID_FORK }} + shell: bash + run: | + set -euo pipefail + python3 <<'PY' + import json + import os + import urllib.parse + import urllib.request + import sys + + token = os.environ["TOKEN"] + branch = os.environ["BRANCH_NAME"] + version = os.environ["VERSION_NAME"] + pr_url = os.environ["PR_URL"] + target = os.environ["TARGET_PROJECT"] + fork = os.environ["FORK_PROJECT"] + + headers = {"PRIVATE-TOKEN": token, "Content-Type": "application/x-www-form-urlencoded"} + + def project_id(path): + url = f"https://gitlab.com/api/v4/projects/{urllib.parse.quote(path, safe='')}" + req = urllib.request.Request(url, headers=headers) + with urllib.request.urlopen(req) as resp: + return json.loads(resp.read())["id"] + + target_id = project_id(target) + fork_id = project_id(fork) + + # Check for an existing open MR from this branch + params = urllib.parse.urlencode({ + "source_branch": branch, + "state": "opened", + "source_project_id": fork_id, + }) + req = urllib.request.Request( + f"https://gitlab.com/api/v4/projects/{target_id}/merge_requests?{params}", + headers=headers, + ) + with urllib.request.urlopen(req) as resp: + existing = json.loads(resp.read()) + if existing: + print("Merge request already exists:", existing[0]["web_url"]) + sys.exit(0) + + description = ( + f"Automated update after {pr_url}\n\n" + f"GitHub release: https://github.com/octoshrimpy/quik/releases/tag/v{version}" + ) + + payload = urllib.parse.urlencode({ + "source_branch": branch, + "target_branch": "master", + "title": f"Update QUIK to v{version}", + "description": description, + "source_project_id": fork_id, + }).encode() + + req = urllib.request.Request( + f"https://gitlab.com/api/v4/projects/{target_id}/merge_requests", + data=payload, + headers=headers, + ) + with urllib.request.urlopen(req) as resp: + data = json.loads(resp.read()) + print("Created merge request:", data["web_url"]) + PY