diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a8018ea..75ee7fc1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,6 +57,12 @@ jobs: distribution: temurin java-version: 18 + - name: Set up Git author + shell: bash + run: | + git config user.name "GitHub Actions" + git config user.email noreply@github.com + - name: Decrypt secrets run: | ./scripts/setup-age.sh @@ -77,6 +83,6 @@ jobs: run: scripts/signing-cleanup.sh - name: Deploy snapshot - run: scripts/deploy-snapshot.sh + run: scripts/deploy-snapshot.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/scripts/deploy-snapshot.py b/scripts/deploy-snapshot.py new file mode 100755 index 00000000..fd2a495f --- /dev/null +++ b/scripts/deploy-snapshot.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +import subprocess +import os +from typing import Optional +from pathlib import Path +import glob +import tempfile + +NIGHTLY_TAG = "nightly" + + +def exec(command_str: str, shell: bool = False) -> Optional[int]: + print(f"Executing '{command_str}'") + proc = None + if shell: + proc = subprocess.run(command_str, text=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + else: + proc = subprocess.run( + command_str.split(" "), text=True, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + ) + result = proc.returncode + print(f"{proc.stdout}") + return result + + +def exec_out(command_str: str, shell: bool = False) -> str: + print(f"Executing '{command_str}'") + proc = None + if shell: + proc = subprocess.run(command_str, capture_output=True, text=True, shell=True) + else: + proc = subprocess.run( + command_str.split(" "), capture_output=True, text=True, shell=False + ) + data = proc.stdout + return data + + +def get_current_rev() -> str: + return exec_out("git rev-parse --short HEAD") + + +def get_asset_directory() -> Path: + workspace = os.getenv("GITHUB_WORKSPACE") + return Path(workspace) / "android" / "apk" + + +def overwrite_local_tag(): + exec(f"git tag -f {NIGHTLY_TAG} -m 'Nightly release'", shell=True) + + +def overwrite_remote_tag(): + exec(f"git push -f origin {NIGHTLY_TAG}") + + +def has_release() -> bool: + result = exec(f"gh release view {NIGHTLY_TAG}") + return result is not None and result == 0 + + +def delete_release(): + exec(f"gh release delete --yes {NIGHTLY_TAG}") + + +def create_release(): + with tempfile.NamedTemporaryFile("w+") as cf: + cf.write(f"Latest release for Claw from revision {get_current_rev()}") + cf.flush() + cwd = os.getcwd() + os.chdir(get_asset_directory()) + apks = " ".join(glob.glob("*.apk")) + exec( + "gh release create --prerelease " + +f"--title 'Latest snapshot build' --notes-file {cf.name} " + +f"{NIGHTLY_TAG} {apks}", + shell=True + ) + os.chdir(cwd) + + +def main(): + overwrite_local_tag() + if has_release(): + delete_release() + overwrite_remote_tag() + create_release() + + +if __name__ == "__main__": + main() diff --git a/scripts/deploy-snapshot.sh b/scripts/deploy-snapshot.sh deleted file mode 100755 index 37556f9e..00000000 --- a/scripts/deploy-snapshot.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash - -set -euxo pipefail - -NIGHTLY_TAG="nightly" -CURRENT_REV="$(git rev-parse --short HEAD)" -ASSET_DIRECTORY="${GITHUB_WORKSPACE:?}/android/apk" - -function overwrite_local_tag() { - git tag -f "${NIGHTLY_TAG}" -} - -function overwrite_remote_tag() { - git push -f origin "${NIGHTLY_TAG}" -} - -function has_release() { - gh release view "${NIGHTLY_TAG}" &>/dev/null - echo "$?" -} - -function delete_release() { - gh release delete --yes "${NIGHTLY_TAG}" -} - -function create_release() { - local CHANGELOG_FILE - CHANGELOG_FILE="$(mktemp)" - echo "Latest release for Claw from revision ${CURRENT_REV}" | tee "${CHANGELOG_FILE}" - pushd "${ASSET_DIRECTORY}" || return - gh release create --prerelease --title "Latest snapshot build" --notes-file "${CHANGELOG_FILE}" "${NIGHTLY_TAG}" ./*.apk - popd || return -} - -overwrite_local_tag - -if [[ "$(has_release)" -eq 0 ]]; then - delete_release -fi - -overwrite_remote_tag - -create_release