Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 80 additions & 17 deletions .github/workflows/nightly_android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ env:
APK_NAME: sdai-full-nightly.apk
NIGHTLY_TAG: nightly
NIGHTLY_RELEASE_NAME: Android Full Nightly
TELEGRAM_BOT_API_REVISION: 7232881b39dfcd048a0d569f1983c5184249ee05

jobs:
nightly:
Expand Down Expand Up @@ -86,7 +87,7 @@ jobs:

git diff --name-only "${NIGHTLY_TAG}..HEAD" > "${RUNNER_TEMP}/nightly-changed-files.txt"

functional_pattern='^(app/|core/|data/|demo/|domain/|feature/|network/|presentation/|storage/|build-logic/|gradle/|build\.gradle\.kts$|settings\.gradle\.kts$|gradle\.properties$|gradlew$|gradlew\.bat$|\.github/workflows/nightly_android\.yml$)'
functional_pattern='^(app/|core/|data/|demo/|domain/|feature/|network/|presentation/|storage/|build-logic/|gradle/|scripts/nightly/|build\.gradle\.kts$|settings\.gradle\.kts$|gradle\.properties$|gradlew$|gradlew\.bat$|\.github/workflows/nightly_android\.yml$)'
if grep -Eq "${functional_pattern}" "${RUNNER_TEMP}/nightly-changed-files.txt"; then
echo "should_build=true" >> "${GITHUB_OUTPUT}"
echo "reason=Functional files changed since the previous nightly." >> "${GITHUB_OUTPUT}"
Expand Down Expand Up @@ -333,22 +334,17 @@ jobs:
--verify-tag
fi

- name: Announce nightly on Discord
- name: Prepare nightly announcements
id: announcement
if: steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true'
shell: bash
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_NIGHTLY_WEBHOOK_URL }}
NIGHTLY_VERSION_NAME: ${{ steps.version.outputs.version_name }}
NIGHTLY_BUILD_DATE: ${{ steps.version.outputs.build_date }}
NIGHTLY_COMMIT: ${{ steps.plan.outputs.target_sha }}
run: |
set -euo pipefail

if [[ -z "${DISCORD_WEBHOOK_URL}" ]]; then
echo "::notice::Discord notification skipped: DISCORD_NIGHTLY_WEBHOOK_URL is not configured."
exit 0
fi

output_dir="${RUNNER_TEMP}/nightly-output"
release_url="https://github.com/${GITHUB_REPOSITORY}/releases/tag/${NIGHTLY_TAG}"
download_url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${NIGHTLY_TAG}/${APK_NAME}"
Expand All @@ -362,16 +358,49 @@ jobs:
--arg run_url "https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" \
--arg feedback_url "https://github.com/${GITHUB_REPOSITORY}/issues" \
--rawfile changes "${output_dir}/changes.txt" \
'{allowed_mentions: {parse: []}, embeds: [{
'{
title: ("SDAI Dev · Nightly · " + $date),
url: $release_url,
color: 7684269,
description: ("**" + $version + "**\nInstalls alongside stable SDAI with separate app data.\n\n" +
$changes[:1000] + "\n[Download latest APK](" + $download_url + ") · [Release details](" + $release_url +
")\n[Commit " + $commit[:8] + "](" + $commit_url + ") · [Build](" + $run_url +
") · [Report an issue](" + $feedback_url + ")"),
footer: {text: "Preview build for testing. The download link always serves the latest nightly."}
}]}' > "${output_dir}/discord.json"
version: $version,
installation_note: "Installs alongside stable SDAI with separate app data.",
changes: $changes[:1000],
links: [
[{text: "Download latest APK", url: $download_url}, {text: "Release details", url: $release_url}],
[{text: ("Commit " + $commit[:8]), url: $commit_url}, {text: "Build", url: $run_url},
{text: "Report an issue", url: $feedback_url}]
],
footer: "Preview build for testing. The download link always serves the latest nightly."
}' > "${output_dir}/announcement.json"

jq '{allowed_mentions: {parse: []}, embeds: [{
title: .title,
url: .links[0][1].url,
color: 7684269,
description: ("**" + .version + "**\n" + .installation_note + "\n\n" + .changes + "\n" +
(.links | map(map("[" + .text + "](" + .url + ")") | join(" · ")) | join("\n"))),
footer: {text: .footer}
}]}' "${output_dir}/announcement.json" > "${output_dir}/discord.json"

# Escape all dynamic content for Telegram HTML, including commit subjects.
jq -r '"<b>" + (.title | @html) + "</b>\n\n<b>" + (.version | @html) + "</b>\n" +
(.installation_note | @html) + "\n\n" + (.changes | @html) + "\n" +
(.links | map(map("<a href=\"" + (.url | @html) + "\">" + (.text | @html) + "</a>") |
join(" · ")) | join("\n")) + "\n\n" + (.footer | @html)' \
"${output_dir}/announcement.json" > "${output_dir}/telegram.html"

- name: Announce nightly on Discord
if: steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true'
shell: bash
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_NIGHTLY_WEBHOOK_URL }}
run: |
set -euo pipefail

if [[ -z "${DISCORD_WEBHOOK_URL}" ]]; then
echo "::notice::Discord notification skipped: DISCORD_NIGHTLY_WEBHOOK_URL is not configured."
exit 0
fi

output_dir="${RUNNER_TEMP}/nightly-output"

# Do not retry POST automatically: a lost response could duplicate the announcement.
curl --fail --silent --show-error --connect-timeout 15 --max-time 60 \
Expand All @@ -380,3 +409,37 @@ jobs:
--output "${output_dir}/discord-response.json" \
--url-query 'wait=true' \
"${DISCORD_WEBHOOK_URL}"

- name: Checkout nightly notification tools
id: telegram_tools
# Attempt both destinations even if Discord delivery fails.
if: ${{ !cancelled() && steps.announcement.outcome == 'success' && steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true' }}
uses: actions/checkout@v4
with:
# Notification tools belong to the workflow revision, not the APK's target_ref.
ref: ${{ github.workflow_sha }}
path: .nightly-ci
sparse-checkout: scripts/nightly
persist-credentials: false

- name: Cache Telegram Bot API binary
id: telegram_cache
if: ${{ !cancelled() && steps.telegram_tools.outcome == 'success' }}
uses: actions/cache@v4
with:
path: ${{ runner.temp }}/telegram-bot-api/bin
key: telegram-bot-api-ubuntu24-${{ runner.arch }}-${{ env.TELEGRAM_BOT_API_REVISION }}-${{ hashFiles('.nightly-ci/scripts/nightly/build_telegram_bot_api.sh') }}

- name: Announce nightly and attach APK in Telegram comments
if: ${{ !cancelled() && steps.telegram_tools.outcome == 'success' && steps.telegram_cache.outcome == 'success' }}
shell: bash
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_NIGHTLY_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ vars.TELEGRAM_NIGHTLY_CHAT_ID || secrets.TELEGRAM_NIGHTLY_CHAT_ID }}
TELEGRAM_REPLY_TO_MESSAGE_ID: ${{ vars.TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID || secrets.TELEGRAM_NIGHTLY_REPLY_TO_MESSAGE_ID }}
TELEGRAM_API_ID: ${{ secrets.TELEGRAM_API_ID }}
TELEGRAM_API_HASH: ${{ secrets.TELEGRAM_API_HASH }}
NIGHTLY_SIGNED_APK: ${{ steps.sign.outputs.signed_apk }}
run: |
set -euo pipefail
python3 .nightly-ci/scripts/nightly/telegram_announce.py
8 changes: 8 additions & 0 deletions NIGHTLY_BUILDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ Give ordinary members View Channel and Read Message History, deny Send Messages,

The APK is hosted on GitHub rather than attached to Discord because Full APKs exceed Discord's default upload limit. The fixed download URL always serves the latest nightly, including when opened from an older announcement. Mention parsing is disabled so commit subjects cannot notify roles or everyone.

## Telegram Notifications

After successfully publishing a nightly, the workflow posts a comment under the configured Telegram announcement in the channel's linked discussion group. It uses the same title, version, recent changes, links and disclaimer as Discord, rendered as HTML text with link previews disabled. The exact signed APK published to GitHub is sent as a file in a reply to that comment, with its version as the caption. The full announcement stays in a separate text message to avoid Telegram's shorter document caption limit.

Uploads use a temporary local Bot API server on the runner, supporting files up to 2 GB. Its executable is built from pinned Telegram source and cached separately from bot session data. The server listens only on loopback, logs out the bot before discarding its session and stops when delivery finishes or the step is cancelled. Notification tools come from the workflow's commit, independently of the APK's build target.

Skipped builds and runs with `publish=false` do not post. A missing reply target fails delivery instead of posting an unrelated group message. Telegram delivery is attempted even if Discord delivery fails. Neither destination retries automatically, since a lost response could duplicate an announcement.

## Manual Run

GitHub only exposes `workflow_dispatch` for workflow files that exist on the repository default branch. Keep `.github/workflows/nightly_android.yml` on `master`; the workflow still builds `develop` through the `target_ref` input.
Expand Down
23 changes: 23 additions & 0 deletions scripts/nightly/build_telegram_bot_api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Build only the pinned upstream executable; bot sessions never enter this directory.
set -euo pipefail

: "${RUNNER_TEMP:?Missing runner temporary directory.}"
: "${TELEGRAM_BOT_API_REVISION:?Missing Telegram Bot API source revision.}"
if [[ ! "${TELEGRAM_BOT_API_REVISION}" =~ ^[0-9a-f]{40}$ ]]; then
echo "Telegram Bot API revision must be a full commit SHA." >&2
exit 1
fi

source_dir="${RUNNER_TEMP}/telegram-bot-api/source"
binary_dir="${RUNNER_TEMP}/telegram-bot-api/bin"
sudo apt-get update
sudo apt-get install --yes --no-install-recommends cmake g++ make gperf libssl-dev zlib1g-dev
git init "${source_dir}"
git -C "${source_dir}" fetch --depth 1 https://github.com/tdlib/telegram-bot-api.git "${TELEGRAM_BOT_API_REVISION}"
git -C "${source_dir}" checkout --detach FETCH_HEAD
git -C "${source_dir}" submodule update --init --recursive --depth 1
cmake -S "${source_dir}" -B "${source_dir}/build" -DCMAKE_BUILD_TYPE=Release
cmake --build "${source_dir}/build" --target telegram-bot-api --parallel 2
install -d "${binary_dir}"
install -m 755 "${source_dir}/build/telegram-bot-api" "${binary_dir}/telegram-bot-api"
161 changes: 161 additions & 0 deletions scripts/nightly/telegram_announce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/env python3
"""Publish the nightly announcement and its signed APK through a temporary local Bot API."""

import json
import os
from pathlib import Path
import re
import signal
import socket
import subprocess
import sys
import tempfile
import time
import urllib.error
import urllib.request


def request(base_url, token, method, payload=None, timeout=30):
"""Call Telegram without retries or exceptions that disclose credential-bearing URLs."""
data = None if payload is None else json.dumps(payload).encode()
query = urllib.request.Request(
f"{base_url}/bot{token}/{method}",
data=data,
headers={"Content-Type": "application/json"},
)
try:
with urllib.request.urlopen(query, timeout=timeout) as response:
result = json.load(response)
except urllib.error.HTTPError as error:
raise RuntimeError(f"Telegram {method} failed (HTTP {error.code}).") from None
except (OSError, ValueError, urllib.error.URLError):
raise RuntimeError(f"Telegram {method} failed; delivery may be unconfirmed.") from None
if result.get("ok") is not True:
raise RuntimeError(f"Telegram {method} was not confirmed.")
return result["result"]


def cancel(signum, _frame):
"""Unwind the owned server and session when the runner cancels this step."""
raise SystemExit(128 + signum)


def main():
"""Validate configuration before building, then send both comments and clean up the session."""
names = (
"TELEGRAM_BOT_TOKEN",
"TELEGRAM_CHAT_ID",
"TELEGRAM_REPLY_TO_MESSAGE_ID",
"TELEGRAM_API_ID",
"TELEGRAM_API_HASH",
)
settings = {name: os.environ.get(name, "") for name in names}
if not any(settings.values()):
print("::notice::Telegram notification skipped: nightly Telegram settings are not configured.")
return
if not all(settings.values()):
raise RuntimeError("Telegram requires the bot token, chat ID, reply ID, API ID and API hash.")
patterns = {
"TELEGRAM_CHAT_ID": r"-[0-9]+",
"TELEGRAM_REPLY_TO_MESSAGE_ID": r"[1-9][0-9]*",
"TELEGRAM_API_ID": r"[1-9][0-9]*",
"TELEGRAM_API_HASH": r"[0-9a-fA-F]{32}",
}
if any(not re.fullmatch(pattern, settings[name]) for name, pattern in patterns.items()):
raise RuntimeError("Telegram IDs or API hash have an invalid format.")

apk = Path(os.environ["NIGHTLY_SIGNED_APK"]).resolve(strict=True)
apk_size = apk.stat().st_size
if not apk.is_file() or not 0 < apk_size <= 2_000_000_000:
raise RuntimeError("The signed nightly APK must be a nonempty file within Telegram's 2 GB limit.")
runner_temp = Path(os.environ["RUNNER_TEMP"])
output_dir = runner_temp / "nightly-output"
text = (output_dir / "telegram.html").read_text()
announcement = json.loads((output_dir / "announcement.json").read_text())
binary = runner_temp / "telegram-bot-api/bin/telegram-bot-api"
if not binary.is_file():
build_env = {key: value for key, value in os.environ.items() if key not in names}
subprocess.run(
["bash", str(Path(__file__).with_name("build_telegram_bot_api.sh"))],
env=build_env,
check=True,
)

token = settings["TELEGRAM_BOT_TOKEN"]
chat_id = int(settings["TELEGRAM_CHAT_ID"])
reply_id = int(settings["TELEGRAM_REPLY_TO_MESSAGE_ID"])
# Choose an available loopback port for this owned server.
with socket.socket() as reservation:
reservation.bind(("127.0.0.1", 0))
port = reservation.getsockname()[1]
base_url = f"http://127.0.0.1:{port}"
signal.signal(signal.SIGTERM, cancel)
signal.signal(signal.SIGINT, cancel)
request("https://api.telegram.org", token, "logOut", {})
with tempfile.TemporaryDirectory(prefix="sdai-telegram-session-", dir=runner_temp) as session_dir:
server = subprocess.Popen(
[str(binary), "--local", "--http-ip-address=127.0.0.1", f"--http-port={port}",
f"--dir={session_dir}", f"--temp-dir={session_dir}", "--verbosity=0"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
try:
deadline = time.monotonic() + 120
while True:
if server.poll() is not None:
raise RuntimeError("The local Telegram Bot API server exited before becoming ready.")
try:
request(base_url, token, "getMe", timeout=5)
break
except RuntimeError:
if time.monotonic() >= deadline:
raise RuntimeError("The local Telegram Bot API server did not become ready.") from None
time.sleep(2)

# Preserve the full text rather than truncate it to Telegram's document caption limit.
message = request(base_url, token, "sendMessage", {
"chat_id": chat_id,
"text": text,
"parse_mode": "HTML",
"link_preview_options": {"is_disabled": True},
"reply_parameters": {"message_id": reply_id, "allow_sending_without_reply": False},
})
# Local mode reads the exact signed APK from disk without a second download or memory copy.
document = request(base_url, token, "sendDocument", {
"chat_id": chat_id,
"document": apk.as_uri(),
"caption": announcement["version"],
"reply_parameters": {
"message_id": message["message_id"],
"allow_sending_without_reply": False,
},
}, timeout=900)
if document.get("document", {}).get("file_size") != apk_size:
raise RuntimeError("Telegram did not confirm the expected APK file size.")
print("Telegram confirmed the nightly announcement and signed APK attachment.")
finally:
has_delivery_failed = sys.exc_info()[0] is not None
try:
# Invalidate this session before the disposable runner removes its state.
request(base_url, token, "logOut", {}, timeout=30)
except RuntimeError:
print("::error::Telegram session logout failed.", file=sys.stderr)
if not has_delivery_failed:
raise
finally:
server.terminate()
try:
server.wait(timeout=30)
except subprocess.TimeoutExpired:
server.kill()
server.wait()


if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, KeyError, ValueError, subprocess.CalledProcessError) as error:
# Only deliberate errors are displayed; library diagnostics may contain a token or API hash.
message = str(error) if isinstance(error, RuntimeError) else type(error).__name__
print(f"::error::{message}", file=sys.stderr)
sys.exit(1)
Loading