selfprivacy.org.app/ci.py

233 lines
11 KiB
Python
Raw Normal View History

2022-09-14 13:22:58 +00:00
#!/usr/bin/env python3
import os
import subprocess
2022-09-20 03:49:59 +00:00
import requests
import yaml
import argparse
2022-09-14 16:52:26 +00:00
CONTAINER_IMAGE = "localhost/flutter-build-env"
HOST_HOME = "/var/lib/drone-runner-exec"
2022-09-14 13:21:46 +00:00
CONTAINER_HOME = "/tmp/builder"
2022-09-20 03:49:59 +00:00
HOST_MOUNTED_VOLUME = f"{HOST_HOME}/.local/share/containers/storage/volumes/release/_data"
2022-09-14 21:12:50 +00:00
APP_NAME = "org.selfprivacy.app"
2022-09-17 05:59:25 +00:00
APP_NAME_NIGHTLY = "org.selfprivacy.app.nightly"
APP_VERSION_FULL = yaml.safe_load(open("pubspec.yaml", "r"))['version']
APP_SEMVER = APP_VERSION_FULL[:APP_VERSION_FULL.find("+")]
APP_BUILD_ID = APP_VERSION_FULL[APP_VERSION_FULL.find("+"):][1::]
2022-09-17 06:24:47 +00:00
APP_BUILD_ID_NIGHTLY = subprocess.run(["git", "rev-list", "--first-parent", "--count", "HEAD"], check=True, stdout=subprocess.PIPE).stdout.decode().strip()
2022-09-20 03:49:59 +00:00
GITEA_HOST_URL = "https://git.selfprivacy.org"
GITEA_REPO_FULL = os.environ.get("DRONE_REPO") or "kherel/selfprivacy.org.app"
GITEA_REPO_OWNER = os.environ.get("DRONE_REPO_OWNER") or "kherel"
GITEA_REPO_NAME = os.environ.get("DRONE_REPO_NAME") or "selfprivacy.org.app"
# Environments
def podman_offline(dir, *args):
2022-09-14 16:52:26 +00:00
subprocess.run(["podman", "run", "--rm", "--network=none", "--cap-add=CHOWN", f"--workdir={dir}",
2022-09-14 13:21:46 +00:00
"-v", os.getcwd() + f":{CONTAINER_HOME}/src",
"-v", f"{HOST_HOME}/fdroid:{CONTAINER_HOME}/fdroid",
2022-09-15 01:21:33 +00:00
"-v", f"{HOST_HOME}/fdroid-keystore:{CONTAINER_HOME}/fdroid-keystore",
"-v", f"{HOST_HOME}/standalone-keystore:{CONTAINER_HOME}/standalone-keystore",
"-v", f"{HOST_HOME}/google-keystore:{CONTAINER_HOME}/google-keystore",
2022-09-15 02:24:54 +00:00
"--env", "FDROID_KEYSTORE_PASS=" + os.environ.get("FDROID_KEYSTORE_PASS"),
"--env", "STANDALONE_KEYSTORE_PASS=" + os.environ.get("STANDALONE_KEYSTORE_PASS"),
"--env", "GOOGLE_KEYSTORE_PASS=" + os.environ.get("GOOGLE_KEYSTORE_PASS"),
2022-09-14 13:25:33 +00:00
"--user", os.getuid().__str__() + ":" + os.getgid().__str__(), "--userns=keep-id",
CONTAINER_IMAGE, "bash", "-c", ' '.join(args)
2022-09-14 16:58:55 +00:00
], check=True)
def podman_online(dir, *args):
2022-09-14 16:52:26 +00:00
subprocess.run(["podman", "run", "--rm", "--cap-add=CHOWN", f"--workdir={dir}",
2022-09-14 13:21:46 +00:00
"-v", os.getcwd() + f":{CONTAINER_HOME}/src",
2022-09-14 13:25:33 +00:00
"--user", os.getuid().__str__() + ":" + os.getgid().__str__(), "--userns=keep-id",
CONTAINER_IMAGE, "bash", "-c", ' '.join(args)
2022-09-14 16:58:55 +00:00
], check=True)
2022-09-20 03:49:59 +00:00
# Utilities
def gitea_create_release():
url = f"{GITEA_HOST_URL}/api/v1/repos/{GITEA_REPO_FULL}/releases"
token = os.environ.get("GITEA_RELEASE_TOKEN")
params = {"access_token": f"{token}"}
2022-09-20 05:11:07 +00:00
json = {"tag_name": f"{os.environ.get('DRONE_SEMVER') or APP_SEMVER}", "name": f"{os.environ.get('DRONE_SEMVER' or APP_SEMVER)}", "prerelease": True}
2022-09-20 03:49:59 +00:00
request = requests.post(url, params=params, json=json)
try:
request.raise_for_status()
except requests.exceptions.HTTPError as error:
print(error)
return error
def gitea_get_release_id():
2022-09-20 05:31:20 +00:00
url = f"{GITEA_HOST_URL}/api/v1/repos/{GITEA_REPO_FULL}/releases/tags/{os.environ.get('DRONE_SEMVER') or APP_SEMVER}"
2022-09-20 03:49:59 +00:00
request = requests.get(url)
try:
request.raise_for_status()
except requests.exceptions.HTTPError as error:
print(error)
return error
return request.json()['id']
def gitea_upload_attachment(file):
id = gitea_get_release_id()
url = f"{GITEA_HOST_URL}/api/v1/repos/{GITEA_REPO_FULL}/releases/{id}/assets"
token = os.environ.get("GITEA_RELEASE_TOKEN")
2022-09-20 04:30:09 +00:00
params = {"access_token": f"{token}", "name": f"{os.path.basename(file)}"}
2022-09-20 03:49:59 +00:00
files = {"attachment": open(f"{file}", "br")}
request = requests.post(url, params=params, files=files)
try:
request.raise_for_status()
except requests.exceptions.HTTPError as error:
print(error)
return error
# Targets
def build_linux():
2022-09-14 18:08:00 +00:00
podman_offline(f"{CONTAINER_HOME}/src", "chown -R $(id -u):$(id -g) /tmp/flutter_pub_cache",
"&& flutter pub get --offline",
2022-09-14 16:52:26 +00:00
"&& flutter build linux")
def build_apk():
2022-09-14 18:12:02 +00:00
podman_offline(f"{CONTAINER_HOME}/src", "chown -R $(id -u):$(id -g) /tmp/gradle /tmp/flutter_pub_cache",
2022-09-14 16:52:26 +00:00
"&& flutter pub get --offline",
2022-09-14 22:11:05 +00:00
"&& flutter build apk --flavor production")
2022-09-15 01:21:33 +00:00
def build_bundle():
podman_offline(f"{CONTAINER_HOME}/src", "chown -R $(id -u):$(id -g) /tmp/gradle /tmp/flutter_pub_cache",
"&& flutter pub get --offline",
"&& flutter build appbundle --flavor production")
def sign_apk_standalone():
2022-09-14 13:21:46 +00:00
podman_offline(f"{CONTAINER_HOME}/src",
2022-09-14 22:11:05 +00:00
"zipalign -f -v 4 build/app/outputs/flutter-apk/app-production-release.apk",
f"standalone_{APP_NAME}-{APP_SEMVER}.apk")
2022-09-14 20:30:07 +00:00
podman_offline(f"{CONTAINER_HOME}/src",
2022-09-15 01:21:33 +00:00
f"apksigner sign --ks {CONTAINER_HOME}/standalone-keystore --ks-key-alias standalone --ks-pass",
f"env:STANDALONE_KEYSTORE_PASS standalone_{APP_NAME}-{APP_SEMVER}.apk")
def sign_apk_fdroid():
podman_offline(f"{CONTAINER_HOME}/fdroid", f"rm -rf {CONTAINER_HOME}/fdroid/unsigned/*")
podman_offline(f"{CONTAINER_HOME}/fdroid",
f"test ! -f {CONTAINER_HOME}/fdroid/repo/{APP_NAME}_{APP_BUILD_ID}.apk",
2022-09-14 22:11:05 +00:00
"&& cp ../src/build/app/outputs/flutter-apk/app-production-release.apk",
f"unsigned/{APP_NAME}_{APP_BUILD_ID}.apk || echo exist")
podman_offline(f"{CONTAINER_HOME}/fdroid", "fdroid publish")
podman_offline(f"{CONTAINER_HOME}/fdroid", "fdroid update")
2022-09-17 05:59:25 +00:00
def sign_apk_fdroid_nightly():
podman_offline(f"{CONTAINER_HOME}/fdroid", f"rm -rf {CONTAINER_HOME}/fdroid/unsigned/*")
podman_offline(f"{CONTAINER_HOME}/fdroid",
f"test ! -f {CONTAINER_HOME}/fdroid/repo/{APP_NAME_NIGHTLY}_{APP_BUILD_ID_NIGHTLY}.apk",
"&& cp ../src/build/app/outputs/flutter-apk/app-nightly-release.apk",
2022-09-17 06:09:47 +00:00
f"unsigned/{APP_NAME_NIGHTLY}_{APP_BUILD_ID_NIGHTLY}.apk || echo exist")
2022-09-17 05:59:25 +00:00
podman_offline(f"{CONTAINER_HOME}/fdroid", "fdroid publish")
podman_offline(f"{CONTAINER_HOME}/fdroid", "fdroid update")
2022-09-15 16:46:44 +00:00
def sign_bundle():
podman_offline(f"{CONTAINER_HOME}/src",
2022-09-15 21:04:46 +00:00
f"jarsigner -sigalg SHA256withRSA -digestalg SHA-256 -keystore {CONTAINER_HOME}/google-keystore -signedjar {APP_NAME}-{APP_SEMVER}.aab -storepass:env GOOGLE_KEYSTORE_PASS build/app/outputs/bundle/productionRelease/app-production-release.aab google")
2022-09-15 16:46:44 +00:00
def package_linux_appimage():
podman_online(f"{CONTAINER_HOME}/src", "appimage-builder --recipe appimage.yml")
def package_linux_flatpak():
2022-09-14 16:58:55 +00:00
subprocess.run(["flatpak-builder", "--force-clean", "--repo=flatpak-repo", "flatpak-build", "flatpak.yml"], check=True)
2022-09-14 23:33:02 +00:00
subprocess.run(["flatpak", "build-bundle", "flatpak-repo", f"{APP_NAME}-{APP_SEMVER}.flatpak", f"{APP_NAME}"], check=True)
def package_linux_archive():
podman_online(f"{CONTAINER_HOME}/src", f"tar -C build/linux/x64/release/bundle -vacf {APP_NAME}-{APP_SEMVER}.tar.zstd .")
def deploy_gitea_release():
2022-09-20 03:49:59 +00:00
gitea_upload_attachment(f"{HOST_MOUNTED_VOLUME}/standalone_{APP_NAME}-{APP_SEMVER}.apk")
gitea_upload_attachment(f"{HOST_MOUNTED_VOLUME}/standalone_{APP_NAME}-{APP_SEMVER}.apk.idsig")
gitea_upload_attachment(f"{HOST_MOUNTED_VOLUME}/SelfPrivacy-{APP_SEMVER}-x86_64.AppImage")
gitea_upload_attachment(f"{HOST_MOUNTED_VOLUME}/SelfPrivacy-{APP_SEMVER}-x86_64.AppImage.zsync")
gitea_upload_attachment(f"{HOST_MOUNTED_VOLUME}/{APP_NAME}-{APP_SEMVER}.flatpak")
gitea_upload_attachment(f"{HOST_MOUNTED_VOLUME}/{APP_NAME}-{APP_SEMVER}.tar.zstd")
def deploy_fdroid_repo():
subprocess.run([f"""eval $(ssh-agent -s) &&
echo \"$SSH_PRIVATE_KEY\" | tr -d '\r' | ssh-add - &&
scp -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -r {HOST_HOME}/fdroid/repo/* deployer@selfprivacy.org:/var/www/fdroid.selfprivacy.org
2022-09-14 16:58:55 +00:00
"""], shell=True, check=True)
2022-09-14 13:21:46 +00:00
def ci_build_linux():
2022-09-14 18:08:00 +00:00
podman_online(f"{CONTAINER_HOME}/src", "chown -R $(id -u):$(id -g) /tmp/flutter_pub_cache",
"&& flutter build linux --debug")
2022-09-14 13:21:46 +00:00
def ci_build_apk():
2022-09-14 18:12:02 +00:00
podman_online(f"{CONTAINER_HOME}/src", "chown -R $(id -u):$(id -g) /tmp/gradle /tmp/flutter_pub_cache",
"&& flutter build apk --flavor nightly")
2022-09-14 13:21:46 +00:00
def ci_run_tests():
2022-09-14 21:05:44 +00:00
podman_online(f"{CONTAINER_HOME}/src", "flutter test",
"&& flutter test --machine --coverage > tests.output")
# Arguments
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
2022-09-14 16:52:26 +00:00
group.add_argument("--build-linux", action="store_true")
group.add_argument("--build-apk", action="store_true")
2022-09-15 16:46:44 +00:00
group.add_argument("--build-bundle", action="store_true")
group.add_argument("--sign-apk-standalone", action="store_true", help="depends on $STANDALONE_KEYSTORE_PASS")
group.add_argument("--sign-apk-fdroid", action="store_true", help="depends on $FDROID_KEYSTORE_PASS")
2022-09-17 05:59:25 +00:00
group.add_argument("--sign-apk-fdroid-nightly", action="store_true", help="depends on $FDROID_KEYSTORE_PASS")
2022-09-15 16:46:44 +00:00
group.add_argument("--sign-bundle", action="store_true", help="depends on $GOOGLE_KEYSTORE_PASS")
group.add_argument("--package-linux-appimage", action="store_true")
group.add_argument("--package-linux-flatpak", action="store_true")
group.add_argument("--package-linux-archive", action="store_true")
group.add_argument("--deploy-gitea-release", action="store_true", help="depends on $GITEA_RELEASE_TOKEN")
group.add_argument("--deploy-fdroid-repo", action="store_true", help="depends on $SSH_PRIVATE_KEY")
2022-09-14 13:21:46 +00:00
group.add_argument("--ci-build-linux", action="store_true")
group.add_argument("--ci-build-apk", action="store_true")
group.add_argument("--ci-run-tests", action="store_true")
2022-09-20 03:49:59 +00:00
group.add_argument("--gitea-create-release", action="store_true")
group.add_argument("--gitea-upload-attachment", action="store")
args = parser.parse_args()
if args.build_linux:
build_linux()
elif args.build_apk:
build_apk()
2022-09-15 01:23:14 +00:00
elif args.build_bundle:
build_bundle()
elif args.sign_apk_standalone:
sign_apk_standalone()
elif args.sign_apk_fdroid:
sign_apk_fdroid()
2022-09-17 05:59:25 +00:00
elif args.sign_apk_fdroid_nightly:
sign_apk_fdroid_nightly()
2022-09-15 16:47:39 +00:00
elif args.sign_bundle:
2022-09-15 20:50:42 +00:00
sign_bundle()
elif args.package_linux_appimage:
package_linux_appimage()
elif args.package_linux_flatpak:
package_linux_flatpak()
elif args.package_linux_archive:
package_linux_archive()
elif args.deploy_gitea_release:
deploy_gitea_release()
elif args.deploy_fdroid_repo:
deploy_fdroid_repo()
2022-09-14 13:21:46 +00:00
elif args.ci_build_linux:
ci_build_linux()
elif args.ci_build_apk:
ci_build_apk()
elif args.ci_run_tests:
ci_run_tests()
2022-09-20 03:49:59 +00:00
elif args.gitea_create_release:
gitea_create_release()
elif args.gitea_upload_attachment:
gitea_upload_attachment(args.gitea_upload_attachment)