TWRP Builder #21
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Copyright (C) 2024 Antonino Scordino | |
# Copyright (C) 2024 Souhrud Reddy | |
# | |
# SPDX-License-Identifier: Apache-2.0 | |
# | |
name: TWRP Builder | |
on: | |
workflow_dispatch: | |
# Various inputs to simplify usage of workflow. | |
inputs: | |
BASE_PROJECT: | |
description: 'Choose a base project:' | |
required: true | |
default: 'TWRP 12.1' | |
type: choice | |
options: | |
- 'TWRP 12.1' | |
BUILD_DIFFERENT_ROM: | |
description: "Command to initialize a different 'repo' project:" | |
required: false | |
default: "echo 'Build Starting!'" | |
REMOVALS: | |
description: "Folders to be removed before syncing:" | |
required: false | |
LOCAL_MANIFEST: | |
description: "Personal local manifest [repository or raw]:" | |
required: true | |
default: 'https://github.com/sounddrill31/local_manifests' | |
LOCAL_MANIFEST_BRANCH: | |
description: "Personal local manifest's branch:" | |
required: false | |
default: 'twrp-oxygen-12.1' | |
DEVICE_NAME: | |
description: "Device's codename:" | |
required: true | |
default: "oxygen" | |
PRODUCT_NAME: | |
description: "Product to build:" | |
required: true | |
default: "twrp_oxygen" | |
BUILD_COMMAND: | |
description: 'Command to be used for compiling:' | |
required: true | |
default: 'make recoveryimage -j\$(nproc --all)' | |
BUILD_TYPE: | |
description: 'Type of build:' | |
required: true | |
default: 'userdebug' | |
type: choice | |
options: | |
- 'eng' | |
- 'userdebug' | |
- 'user' | |
CLEAN_BUILD: | |
description: 'Build with a clean workspace?' | |
required: true | |
default: 'no' | |
type: choice | |
options: | |
- 'yes' | |
- 'no' | |
jobs: | |
prepare: | |
name: Prepare for Building | |
runs-on: ubuntu-latest | |
steps: | |
- name: Display Run Parameters # Credit to azwhikaru for this part | |
run: | | |
echo "::group::User Environment Variables" | |
echo "Base Project: ${{ github.event.inputs.BASE_PROJECT }}" | |
echo "Extra Repo Init: ${{ github.event.inputs.BUILD_DIFFERENT_ROM }}" | |
echo "Removals: ${{ github.event.inputs.REMOVALS }}" | |
echo "Local Manifest: ${{ github.event.inputs.LOCAL_MANIFEST }}" | |
echo "Local Manifest Branch: ${{ github.event.inputs.LOCAL_MANIFEST_BRANCH }}" | |
echo "Device Name: ${{ github.event.inputs.DEVICE_NAME }}" | |
echo "Lunch Target: ${{ github.event.inputs.PRODUCT_NAME }} ${{ github.event.inputs.BUILD_TYPE }}" | |
echo "Build Command: ${{ github.event.inputs.BUILD_COMMAND }}" | |
echo "Clean Build: ${{ github.event.inputs.CLEAN_BUILD }}" | |
echo "::endgroup::" | |
echo "Displaying Local Manifests" | |
if [[ -z "${{ secrets.DISPLAY_FALSE }}" ]]; then | |
git clone ${{ github.event.inputs.LOCAL_MANIFEST }} --depth 1 -b ${{ github.event.inputs.LOCAL_MANIFEST_BRANCH }} local_manifests | |
cat local_manifests/*.xml | |
else | |
echo "Displaying is disabled through secrets." | |
fi | |
- name: Set Project variables | |
id: proj-variables | |
run: | | |
case "${{ github.event.inputs.BASE_PROJECT }}" in | |
"TWRP 12.1") | |
export PROJECTFOLDER="/crave-devspaces/TWRP12" | |
export PROJECTID="78" | |
export REPO_INIT="repo init -u https://github.com/minimal-manifest-twrp/platform_manifest_twrp_aosp.git -b twrp-12.1 --depth=1" | |
;; | |
esac | |
echo "Building on ${{ github.event.inputs.BASE_PROJECT }} project" | |
echo "PROJECTFOLDER=$PROJECTFOLDER" >> "$GITHUB_OUTPUT" | |
echo "PROJECTID=$PROJECTID" >> "$GITHUB_OUTPUT" | |
echo "REPO_INIT=$REPO_INIT" >> "$GITHUB_OUTPUT" | |
outputs: | |
PROJECTFOLDER: ${{ steps.proj-variables.outputs.PROJECTFOLDER }} | |
PROJECTID: ${{ steps.proj-variables.outputs.PROJECTID }} | |
REPO_INIT: ${{ steps.proj-variables.outputs.REPO_INIT }} | |
test: | |
name: Test Local Manifests | |
needs: prepare | |
runs-on: ubuntu-latest | |
steps: | |
# Accept Project Variables | |
- name: Set Repo Project | |
run: | | |
PROJECTFOLDER="${{ needs.prepare.outputs.PROJECTFOLDER }}" | |
PROJECTID="${{ needs.prepare.outputs.PROJECTID }}" | |
REPO_INIT="${{ needs.prepare.outputs.REPO_INIT }}" | |
echo "PROJECTFOLDER=$PROJECTFOLDER" >> "$GITHUB_ENV" | |
echo "PROJECTID=$PROJECTID" >> "$GITHUB_ENV" | |
echo "REPO_INIT=$REPO_INIT" >> "$GITHUB_ENV" | |
# Download and configure 'repo'. | |
- name: Configure the 'repo' environment | |
run: | | |
# Check if repo is already installed | |
if ! command -v repo >/dev/null 2>&1; then | |
echo "Repo not found. Installing now..." | |
# Create bin directory if it doesn't exist | |
mkdir -p ~/bin | |
# Download repo script | |
curl https://storage.googleapis.com/git-repo-downloads/repo >> ~/bin/repo | |
# Make repo script executable | |
chmod a+x ~/bin/repo | |
# Create symbolic link to /usr/bin/repo | |
sudo ln -sf "/home/$(whoami)/bin/repo" "/usr/bin/repo" | |
echo "Repo installation complete." | |
else | |
echo "Repo already installed." | |
fi | |
continue-on-error: true | |
# Generate 'git' credential in base of the workflow's author. | |
- name: Set-up 'git' credential(s) | |
run: | | |
git config --global user.name "${{ github.actor }}" | |
git config --global user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" | |
# Test Your Personal 'Local Manifests' against The ROM | |
- name: Test Local Manifests | |
if: ${{ github.event.inputs.BUILD_DIFFERENT_ROM != 'skip' }} | |
run: | | |
rm -rf tester | |
mkdir tester | |
cd tester | |
if [ "${{ github.event.inputs.BUILD_DIFFERENT_ROM }}" == "echo 'Build Starting!'" ]; then | |
$REPO_INIT | |
else | |
${{ github.event.inputs.BUILD_DIFFERENT_ROM }} | |
fi | |
git clone ${{ github.event.inputs.LOCAL_MANIFEST }} --depth 1 -b ${{ github.event.inputs.LOCAL_MANIFEST_BRANCH }} .repo/local_manifests && \ | |
if [ ! $? == 0 ]; then \ | |
curl -o .repo/local_manifests ${{ github.event.inputs.LOCAL_MANIFEST }}; \ | |
echo "Git clone failed, downloading through curl instead..."; \ | |
fi \ | |
timeout 1m repo sync --force-sync || { exit_code=$?; [ $exit_code -eq 124 ] || (echo "Error: Process failed with exit code $exit_code"; exit $exit_code); } | |
du -csh . # Output Size when done | |
rm -rf .repo | |
timeout-minutes: 10 | |
outputs: | |
PROJECTFOLDER: ${{ needs.prepare.outputs.PROJECTFOLDER }} | |
PROJECTID: ${{ needs.prepare.outputs.PROJECTID }} | |
REPO_INIT: ${{ needs.prepare.outputs.REPO_INIT }} | |
build: | |
name: Build using foss.crave.io | |
needs: test | |
# Change this to self-hosted after setting up devspace as github actions runner | |
runs-on: ubuntu-latest | |
concurrency: | |
group: ${{ github.actor }}-small | |
steps: | |
# Accept Project Variables | |
- name: Set Repo Project | |
run: | | |
PROJECTFOLDER="${{ needs.test.outputs.PROJECTFOLDER }}" | |
PROJECTID="${{ needs.test.outputs.PROJECTID }}" | |
REPO_INIT="${{ needs.test.outputs.REPO_INIT }}" | |
echo "PROJECTFOLDER=$PROJECTFOLDER" >> "$GITHUB_ENV" | |
echo "PROJECTID=$PROJECTID" >> "$GITHUB_ENV" | |
echo "REPO_INIT=$REPO_INIT" >> "$GITHUB_ENV" | |
# Create a project folder | |
- name: Create Project Folders | |
run: | | |
if [ "${DCDEVSPACE}" != "1" ]; then | |
echo "Symlinking devspace folder" | |
mkdir -p devspace | |
sudo mkdir -p /crave-devspaces | |
sudo ln -sf ${pwd}/devspace /crave-devspaces | |
sudo chmod 777 /crave-devspaces | |
else | |
echo "We are already running in devspace... Skipping Symlinks" | |
fi | |
if grep -q "$PROJECTFOLDER" <(crave clone list --json | jq -r '.clones[]."Cloned At"') && [ "${DCDEVSPACE}" == "1" ]; then | |
crave clone destroy -y $PROJECTFOLDER || echo "Error removing $PROJECTFOLDER" | |
else | |
echo "Skipping removal" | |
fi | |
if [ "${DCDEVSPACE}" == "1" ]; then | |
crave clone create --projectID $PROJECTID $PROJECTFOLDER || echo "Crave clone create failed!" | |
else | |
mkdir $PROJECTFOLDER | |
fi | |
# Check-out in order to access the repository's files. | |
- name: Check-out to repository | |
uses: actions/checkout@v4 | |
# Set-up a spearate directory for the device. | |
- name: Set-up workspace environment | |
run: | | |
mkdir ${{ github.event.inputs.DEVICE_NAME }} | |
cd ${{ github.event.inputs.DEVICE_NAME }} | |
continue-on-error: true | |
# Download and configure 'repo'. | |
- name: Configure the 'repo' environment | |
run: | | |
# Check if repo is already installed | |
if ! command -v repo >/dev/null 2>&1; then | |
echo "Repo not found. Installing now..." | |
# Create bin directory if it doesn't exist | |
mkdir -p ~/bin | |
# Download repo script | |
curl https://storage.googleapis.com/git-repo-downloads/repo >> ~/bin/repo | |
# Make repo script executable | |
chmod a+x ~/bin/repo | |
# Create symbolic link to /usr/bin/repo | |
sudo ln -sf "/home/$(whoami)/bin/repo" "/usr/bin/repo" | |
echo "Repo installation complete." | |
else | |
echo "Repo already installed." | |
fi | |
continue-on-error: true | |
# Generate 'git' credential in base of the workflow's author. | |
- name: Set-up 'git' credential(s) | |
run: | | |
git config --global user.name "${{ github.actor }}" | |
git config --global user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" | |
# Send Build 'start' notification | |
- name: Telegram Notification | |
continue-on-error: true | |
run: | | |
send_telegram_message() { | |
local message="$1" | |
curl -s -X POST \ | |
https://api.telegram.org/bot$botToken/sendMessage \ | |
-d chat_id=$chatId \ | |
-d text="$message" \ | |
-d parse_mode="Markdown" | |
} | |
send_telegram_message "Crave Project: ${{ github.event.inputs.BASE_PROJECT }}%0ACrave ProjectID: $PROJECTID %0ABuild for ${{ github.event.inputs.DEVICE_NAME }} has been queued %0ACheck Progress at: %0Ahttps://github.com/$(echo "${{ github.repository }}" | sed 's@_@\\_@g')/actions/runs/${{ github.run_id }}" | |
env: | |
chatId: ${{ secrets.TELEGRAM_TO }} | |
botToken: ${{ secrets.TELEGRAM_TOKEN }} | |
jobStatus: "Build Started" | |
skipSuccess: false | |
# Download and configure 'crave'. | |
- name: Configure the 'crave' environment | |
run: | | |
mkdir -p $PROJECTFOLDER/.repo/manifests | |
if [ "${DCDEVSPACE}" == "1" ]; then | |
echo 'No need to set up crave, we are already running in devspace!' | |
else | |
curl -s https://raw.githubusercontent.com/accupara/crave/master/get_crave.sh | bash -s -- | |
mv ${PWD}/crave ${HOME}/bin/ | |
sudo ln -sf /home/${USER}/bin/crave /usr/bin/crave | |
envsubst < ${PWD}/crave.conf.sample >> ${PWD}/crave.conf | |
rm -rf ${PWD}/crave.conf.sample | |
cp crave.conf $PROJECTFOLDER | |
fi | |
if [[ -z "${{ secrets.CUSTOM_YAML }}" ]]; then | |
cp configs/crave/crave.yaml.twrp $PROJECTFOLDER/.repo/manifests/crave.yaml | |
echo "No Custom Configuration Found, Using Template!" | |
else | |
touch $PROJECTFOLDER/.repo/manifests/crave.yaml || true | |
echo "${{ secrets.CUSTOM_YAML }}" > $PROJECTFOLDER/.repo/manifests/crave.yaml | |
echo "Custom Configuration Found!" | |
fi | |
env: | |
CRAVE_USERNAME: ${{ secrets.CRAVE_USERNAME }} | |
CRAVE_TOKEN: ${{ secrets.CRAVE_TOKEN }} | |
# Initialize the previously choosen 'repo' project. | |
- name: Set the 'crave' project | |
run: | | |
cd $PROJECTFOLDER | |
echo "Running $REPO_INIT" | |
$REPO_INIT | |
if [ "${{ github.event.inputs.BUILD_DIFFERENT_ROM }}" == "echo 'Build Starting!'" ]; then | |
export BUILD_DIFFERENT_ROM="$REPO_INIT" | |
echo "Building $BUILD_DIFFERENT_ROM" | |
else | |
export BUILD_DIFFERENT_ROM="${{ github.event.inputs.BUILD_DIFFERENT_ROM }}" | |
echo "Building $BUILD_DIFFERENT_ROM" | |
fi | |
echo "BUILD_DIFFERENT_ROM=$BUILD_DIFFERENT_ROM" >> "$GITHUB_ENV" | |
# Create a 'crave' job for building. | |
- name: Start compilation through 'crave' | |
if: ${{ github.event.inputs.BUILD_DIFFERENT_ROM != 'skip' }} | |
run: | | |
cd $PROJECTFOLDER | |
if [ "${{ github.event.inputs.CLEAN_BUILD }}" == "yes" ]; then | |
export CLEAN="clean" | |
fi | |
crave ${{ secrets.CRAVE_FLAGS }} run --no-patch --${CLEAN} "rm -rf .repo/local_manifests/ ${{ github.event.inputs.REMOVALS }} && \ | |
# Clone local_manifests repository | |
$BUILD_DIFFERENT_ROM ; \ | |
git clone ${{ github.event.inputs.LOCAL_MANIFEST }} --depth 1 -b ${{ github.event.inputs.LOCAL_MANIFEST_BRANCH }} .repo/local_manifests && \ | |
if [ ! \$? == 0 ]; then \ | |
curl -o .repo/local_manifests ${{ github.event.inputs.LOCAL_MANIFEST }}; \ | |
echo "Git clone failed, downloading through curl instead..."; \ | |
fi \ | |
# Sync the repositories | |
/opt/crave/resync.sh && \ | |
# Set up build environment | |
export BUILD_USERNAME=${{ github.actor }} ; \ | |
export BUILD_HOSTNAME=crave ; \ | |
source build/envsetup.sh && \ | |
echo "Repository: ${{ github.repository }}"; \ | |
echo "Run ID: ${{ github.run_id }}"; \ | |
# Build the ROM | |
lunch ${{ github.event.inputs.PRODUCT_NAME }}-${{ github.event.inputs.BUILD_TYPE }} && \ | |
make installclean && \ | |
${{ github.event.inputs.BUILD_COMMAND }}" | |
# Send Build 'status' notification (Success) | |
- name: Telegram Notification | |
if: ${{ success() }} | |
continue-on-error: true | |
run: | | |
send_telegram_message() { | |
local message="$1" | |
curl -s -X POST \ | |
https://api.telegram.org/bot$botToken/sendMessage \ | |
-d chat_id=$chatId \ | |
-d text="$message" \ | |
-d parse_mode="Markdown" | |
} | |
send_telegram_message "Crave Project: ${{ github.event.inputs.BASE_PROJECT }}%0ACrave ProjectID: $PROJECTID %0ABuild for ${{ github.event.inputs.DEVICE_NAME }} has status: $jobStatus %0ACheck Progress at: %0Ahttps://github.com/$(echo "${{ github.repository }}" | sed 's@_@\\_@g')/actions/runs/${{ github.run_id }}%0ADownload at: https://github.com/$(echo "${{ github.repository }}" | sed 's@_@\\_@g')/releases/tag/${{ github.run_id }}" | |
env: | |
chatId: ${{ secrets.TELEGRAM_TO }} | |
botToken: ${{ secrets.TELEGRAM_TOKEN }} | |
jobStatus: ${{ job.status }} | |
skipSuccess: false | |
# Send Build 'status' notification (Fail/Cancel) | |
- name: Telegram Notification | |
if: ${{ cancelled() || failure() }} | |
continue-on-error: true | |
run: | | |
send_telegram_message() { | |
local message="$1" | |
curl -s -X POST \ | |
https://api.telegram.org/bot$botToken/sendMessage \ | |
-d chat_id=$chatId \ | |
-d text="$message" \ | |
-d parse_mode="Markdown" | |
} | |
send_telegram_message "Crave Project: ${{ github.event.inputs.BASE_PROJECT }}%0ACrave ProjectID: $PROJECTID %0ABuild for ${{ github.event.inputs.DEVICE_NAME }} has status: $jobStatus %0ACheck Progress at: %0Ahttps://github.com/$(echo "${{ github.repository }}" | sed 's@_@\\_@g')/actions/runs/${{ github.run_id }}" | |
# Todo: error.log link | |
env: | |
chatId: ${{ secrets.TELEGRAM_TO }} | |
botToken: ${{ secrets.TELEGRAM_TOKEN }} | |
jobStatus: ${{ job.status }} | |
# Only reach this wheter the user killed the workflow. | |
- name: Execute if the job is cancelled | |
if: ${{ cancelled() }} | |
run: | | |
cd $PROJECTFOLDER | |
crave stop --all | |
# Upload '.zip's and '.img's directly from 'crave' ssh. | |
- name: Upload build artifact(s) | |
run: | | |
cd $PROJECTFOLDER | |
echo "${{ secrets.GITHUB_TOKEN }}" > token.txt | |
crave push token.txt -d $(crave ssh -- pwd | grep -v Select | sed -s 's/\r//g')/ | |
crave ssh -- "bash /opt/crave/github-actions/upload.sh '${{ github.run_id }}' '${{ github.event.inputs.DEVICE_NAME }}' '${{ github.repository }}' '${{ github.event.inputs.PRODUCT_NAME }}-${{ github.run_id }}' '${{ secrets.EXTRA_FILES }}'" | |
# Pull Errors and Display them | |
- name: Display error.log | |
if: ${{ failure() }} | |
id: errorlog | |
run: | | |
cd $PROJECTFOLDER | |
crave ssh -- sleep 1 | |
crave pull out/error.log | |
echo "Displaying out/error.log" | |
cat out/error.log | |
#if [[ -z "${{ secrets.CUSTOM_YAML }}" ]]; then |