Page MenuHomeGitPull.it

/opt/reyboz/android/bin/publish-latest-branch-build.php

Authored By
valerio.bozzolan
May 14 2025, 00:34
Size
5 KB
Referenced Files
None
Subscribers
None

/opt/reyboz/android/bin/publish-latest-branch-build.php

#!/usr/bin/env php
<?php
################################################################
# This script just takes a built APK and moves it to a directory #
################################################################
# die on errors
declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Take environment variables.
$TELEGRAM_BOT_TOKEN = getenv('TELEGRAM_BOT_TOKEN');
$TELEGRAM_CHAT_ID = getenv('TELEGRAM_CHAT_ID');
// No env, no party.
if (!$TELEGRAM_BOT_TOKEN) {
throw new Exception("Missing ENV TELEGRAM_BOT_TOKEN");
}
if (!$TELEGRAM_CHAT_ID) {
throw new Exception("Missing ENV TELEGRAM_CHAT_ID");
}
// Usage: the first argument is the Differential ID number or empty if working on master.
// expected base URL
$EXPECTED_DEPLOY_URL = 'https://lab.reyboz.it/builds/libre-busto/';
// pathname to the repository built apk file
$APK_PATH = './app/build/outputs/apk/gitpull/app-gitpull-signed.apk';
// Git commit hash
$git_commit = trim(shell_exec('git rev-parse HEAD'));
$git_commit_title = trim(shell_exec('git show --pretty=format:%s -s HEAD'));
// repository build pathname
$BUILD_DIR = '/home/www-data/reyboz.it/lab/builds/libre-busto';
// document id of the page to be commented
$document_id = '';
// get the branch name
$branch_name = 'asd';
if (isset($argv[1]) && !empty($argv[1])) {
// it's a Differential revision
$branch_name = 'D' . $argv[1];
$document_id = $branch_name;
} else {
// get a ref name (probably "HEAD")
$remote_shit = trim(shell_exec('git name-rev --name-only HEAD'));
$branch_name = basename($remote_shit);
}
// give a name of this built apk
$APK_NAME = $branch_name . '-libre-busto.apk';
// complete build URL
$BUILD_URL = $EXPECTED_DEPLOY_URL . $APK_NAME;
// pathname to the APK file in the HTTP build directory
$BUILD_APK_FILE = $BUILD_DIR . '/' . $APK_NAME;
// pathname to the git show file in the HTTP build directory
$BUILD_GIT_SHOW_FILE = $BUILD_DIR . '/' . $branch_name . '-git-show.txt';
echo "Current directory:\n";
echo " " . getcwd() . "\n";
echo "Calculating sha256sum of:\n";
echo " $APK_PATH\n";
echo "File info\n";
system("ls -l $APK_PATH");
// get the digital signature of the last version
$APK_SHA256 = trim(shell_exec("sha256sum $APK_PATH | head -c 64"));
// overwrite the latest built APK for this branch name (force date update)
echo "Cleaning old builds\n";
if (file_exists($BUILD_APK_FILE)) {
unlink($BUILD_APK_FILE);
}
copy($APK_PATH, $BUILD_APK_FILE);
// overwrite the latest git show for this branch name (force date update)
if (file_exists($BUILD_GIT_SHOW_FILE)) {
unlink($BUILD_GIT_SHOW_FILE);
}
file_put_contents($BUILD_GIT_SHOW_FILE, shell_exec('git show'));
// update checksums
chdir($BUILD_DIR);
unlink('sha1sum.txt');
shell_exec('sha1sum * > sha1sum.txt');
chdir('..');
// show the build URL in the log
echo "Published here:\n";
echo "$BUILD_URL\n";
$apix = '`';
// Escaping texts for Telegram
$BUILD_URL_ESCAPED = escapeshellarg($BUILD_URL);
$APK_NAME_ESCAPED = escapeshellarg($APK_NAME);
// add a comment to the build URL
if (!empty($document_id) && empty(getenv('NO_COMMENT'))) {
// We have a Differentials revision under review!
$document_id_escaped = escapeshellarg($document_id);
// Send message to Telegram groups.
$telegram_chat_content = implode("\n", [
"โš™ Dear Android hackers, please review this promising patch:",
"https://gitpull.it/$document_id_escaped",
"",
"Here the related fresh test build:",
"[Download $APK_NAME_ESCAPED]($BUILD_URL_ESCAPED)",
"",
"APK sha256:",
"$apix$APK_SHA256$apix"
]);
echo "DEBUG TELEGRAM CONTENT\n";
echo "$telegram_chat_content\n";
shell_exec(sprintf(
"~/bin/telegram-comment.sh %s %s %s || true",
escapeshellarg($TELEGRAM_BOT_TOKEN),
escapeshellarg($TELEGRAM_CHAT_ID),
escapeshellarg($telegram_chat_content)
));
// Send message to Phorge.
echo "Adding bipbop Comment to $document_id\n";
shell_exec(sprintf(
"~/bin/phab-comment.php %s %s %s || true",
escapeshellarg($document_id),
escapeshellarg($BUILD_URL),
escapeshellarg($APK_SHA256)
));
} else {
// We have NOT a Differentials revision under review!
// So this is an atomic commit.
$git_commit_title_escaped = escapeshellarg($git_commit_title);
$telegram_chat_content = implode("\n", [
"๐ŸŒš New commit: *$git_commit_title_escaped*",
"https://gitpull.it/R4:$git_commit",
"",
"Try this fresh test APK:",
"[Download $APK_NAME_ESCAPED]($BUILD_URL_ESCAPED)",
"",
"APK fingerprint sha256:",
"$apix$APK_SHA256$apix"
]);
echo "DEBUG TELEGRAM CONTENT\n";
echo "$telegram_chat_content\n";
shell_exec(sprintf(
"~/bin/telegram-comment.sh %s %s %s || true",
escapeshellarg($TELEGRAM_BOT_TOKEN),
escapeshellarg($TELEGRAM_CHAT_ID),
escapeshellarg($telegram_chat_content)
));
}
// delete very old files
echo "Cleaning\n";
shell_exec(sprintf(
"find %s -type f -mtime +120 -delete",
escapeshellarg($BUILD_DIR)
));
// clean old APK in the repository to never re-use it by mistake
// as it happened when we moved everything to the /app directory
unlink($APK_PATH);
echo "Done\n";

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
958924
Default Alt Text
/opt/reyboz/android/bin/publish-latest-branch-build.php (5 KB)

Event Timeline