Update Marauder Firmware Service #8404
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
name: Update Marauder Firmware Service | |
on: | |
schedule: | |
- cron: '0 * * * *' | |
workflow_dispatch: | |
jobs: | |
update_release: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v2 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '14' | |
- name: Get Current Version from Remote index.html | |
id: current_version | |
run: | | |
VERSION=$(grep -oP 'Latest Marauder v\d+\.\d+\.\d+' /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/index.html | grep -oP '\d+\.\d+\.\d+' | sort -V | tail -1) | |
if [ -z "$VERSION" ]; then | |
echo "No version found in index.html" | |
echo "current_version=0.0.0" >> $GITHUB_ENV | |
else | |
echo "Current version: $VERSION" | |
echo "current_version=$VERSION" >> $GITHUB_ENV | |
fi | |
if [ "$current_version" -eq "0.0.0" ]; then | |
echo "STOP=1" >> $GITHUB_ENV | |
else | |
echo "STOP=0" >> $GITHUB_ENV | |
fi | |
- name: Install Axios | |
run: npm install axios | |
- name: Check for New Release | |
id: check_release | |
uses: actions/github-script@v5 | |
with: | |
script: | | |
const axios = require('axios'); | |
const response = await axios.get('https://api.github.com/repos/justcallmekoko/ESP32Marauder/releases/latest'); | |
const latestRelease = response.data.tag_name; | |
return latestRelease; | |
- name: Set up Git | |
run: | | |
git config --global user.name 'Github' | |
git config --global user.email 'Github Actions' | |
- name: Remove Node.js & axios | |
run: | | |
rm -rf node_modules | |
- name: Compare Versions and Download New Release | |
run: | | |
STOP=${{ env.STOP }} | |
if [ "$STOP" -eq 1 ]; then | |
echo "NOUPDATE=1" >> $GITHUB_ENV | |
exit | |
fi | |
CURRENT_VERSION=${{ env.current_version }} | |
LATEST_VERSION=${{ steps.check_release.outputs.result }} | |
version_to_int() { | |
# Remove 'v' prefix if present | |
local version=${1#v} | |
IFS='.' read -ra ADDR <<< "$version" | |
printf -v int '%03d%03d%03d' "${ADDR[0]}" "${ADDR[1]}" "${ADDR[2]}" | |
echo $int | |
} | |
CURRENT_VERSION_INT=$(version_to_int "$CURRENT_VERSION") | |
LATEST_VERSION_INT=$(version_to_int "$LATEST_VERSION") | |
if [ "$CURRENT_VERSION_INT" -lt "$LATEST_VERSION_INT" ]; then | |
echo "New release found: $LATEST_VERSION. Downloading..." | |
rm -rf /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/PREVIOUS | |
cp -rf /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/CURRENT /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/PREVIOUS | |
rm -rf /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/CURRENT | |
mkdir -p /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/CURRENT | |
GITHUB_USER="justcallmekoko" | |
GITHUB_REPO="ESP32Marauder" | |
RELEASE_URL="https://github.com/${GITHUB_USER}/${GITHUB_REPO}/releases/expanded_assets/${LATEST_VERSION}" | |
DOWNLOAD_DIR="/home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/CURRENT" | |
mkdir -p "$DOWNLOAD_DIR" | |
FILE_URLS=$(curl -s "$RELEASE_URL" | grep -oP 'href="\K[^"]+\.bin(?=")') | |
if [ -z "$FILE_URLS" ]; then | |
echo "No .bin files found on the release page." | |
exit 1 | |
fi | |
for FILE_URL in $FILE_URLS; do | |
FULL_URL="https://github.com${FILE_URL}" | |
echo "Downloading $FULL_URL" | |
wget -P "$DOWNLOAD_DIR" "$FULL_URL" | |
done | |
echo "NOUPDATE=0" >> $GITHUB_ENV | |
else | |
echo "No new release found. Current version is up to date." | |
echo "NOUPDATE=1" >> $GITHUB_ENV | |
exit 0 | |
fi | |
- name: Update Variables.js to link the Correct firmware | |
run: | | |
NOUPDATE=${{ env.NOUPDATE }} | |
if [ "$NOUPDATE" -eq 1 ]; then | |
exit | |
else | |
cd $HOME | |
cd /home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/js | |
update() { | |
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
cd "$dir" | |
if [ $# -ne 4 ]; then | |
exit 1 | |
fi | |
search_text="$1" | |
suffix="$2" | |
folder_type="$3" | |
js_file="$4" | |
if [ "$folder_type" == "CURRENT" ]; then | |
folder="/home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/CURRENT" | |
elif [ "$folder_type" == "PREVIOUS" ]; then | |
folder="/home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/resources/PREVIOUS" | |
fi | |
if [ ! -d "$folder" ]; then | |
echo "Error: Folder $folder does not exist." | |
exit 1 | |
fi | |
line_number=$(grep -n "$search_text" "$js_file" | cut -d':' -f1) | |
if [ -z "$line_number" ]; then | |
echo "Search text '$search_text' not found in $js_file." | |
exit 1 | |
fi | |
file=$(find "$folder" -type f -name "*$suffix" -print -quit) | |
if [ -z "$file" ]; then | |
echo "No files found with suffix '$suffix' in $folder." | |
exit 0 | |
fi | |
relative_path="${file#/home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/}" | |
sed -i "${line_number}!b; :a; N; /'firmware': '.*'/!ba; s|'firmware': '.*'|'firmware': '$relative_path'|" "$js_file" | |
echo "The first 'firmware' entry after '$search_text' in $js_file updated to match the file with suffix '$suffix' in folder type '$folder_type'." | |
} | |
JS_FOLDER="/home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/js" | |
update "Mpreviouss2Files" "flipper.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "Mlatests2Files" "flipper.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "Mpreviouss2SDFiles" "flipper.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "Mlatests2SDFiles" "flipper.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MpreviouswroomFiles" "old_hardware.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MlatestwroomFiles" "old_hardware.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MV6previousFiles" "v6.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MV6latestFiles" "v6.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MV6_1previousFiles" "v6_1.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MV6_1latestFiles" "v6_1.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MKitpreviousFiles" "kit.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MKitlatestFiles" "kit.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MS3previousFiles" "multiboardS3.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MS3latestFiles" "multiboardS3.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MV6MinipreviousFiles" "_mini.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MV6MinilatestFiles" "_mini.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MD1MinipreviousFiles" "_lddb.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MD1MinilatestFiles" "_lddb.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
update "MDevPropreviousFiles" "_dev_board_pro.bin" "PREVIOUS" "$JS_FOLDER/variables.js" | |
update "MDevProlatestFiles" "_dev_board_pro.bin" "CURRENT" "$JS_FOLDER/variables.js" | |
fi | |
- name: Update index.html to Correct firmware | |
run: | | |
NOUPDATE="${{ env.NOUPDATE }}" | |
if [ "$NOUPDATE" -eq 1 ]; then | |
exit | |
fi | |
file="/home/runner/work/fzeeflasher.github.io/fzeeflasher.github.io/index.html" | |
CURRENT_VERSION="${{ env.current_version }}" | |
LATEST_VERSION="${{ steps.check_release.outputs.result }}" | |
LATEST_FIX=${LATEST_VERSION#v} | |
sed -i "s/<option value=\"latest\">Latest [^<]\+<\/option>/<option value=\"latest\">Latest $LATEST_FIX<\/option>/g" "$file" | |
sed -i "s/<option value=\"previous\">Previous [^<]\+<\/option>/<option value=\"previous\">Previous $CURRENT_VERSION<\/option>/g" "$file" | |
sed -i "s/\(<b>Latest Marauder \)v[0-9]\+\.[0-9]\+\.[0-9]\+\(<\/b>\)/\1v$LATEST_FIX\2/" "$file" | |
- name: Commit and Push Changes | |
if: steps.check_release.outputs.result != 'false' # Corrected condition | |
run: | | |
NOUPDATE=${{ env.NOUPDATE }} | |
if [ "$NOUPDATE" -eq 1 ]; then | |
exit | |
fi | |
LATEST_VERSION=${{ steps.check_release.outputs.result }} | |
git add . | |
git commit -m "Updated Firmware version to $LATEST_VERSION of Marauder." | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} | |
- name: Fetch latest commit | |
if: env.NOUPDATE != '1' | |
run: | | |
LATEST_COMMIT=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/fzeeflasher/fzeeflasher.github.io/commits?per_page=1" | \ | |
jq -r '.[0].commit.message + "|" + .[0].html_url') | |
echo "COMMIT_INFO=$LATEST_COMMIT" >> $GITHUB_ENV | |
- name: Notify Discord | |
if: env.NOUPDATE != '1' | |
run: | | |
TITLE=$(echo "$COMMIT_INFO" | cut -d '|' -f1) | |
COMMIT_URL=$(echo "$COMMIT_INFO" | cut -d '|' -f2) | |
if [ ${#TITLE} -gt 256 ]; then | |
TITLE="${TITLE:0:253}..." | |
fi | |
curl -X POST -H "Content-Type: application/json" -d @- "${{ secrets.DISCORD_WEBHOOK }}" <<EOF | |
{ | |
"username": "Flashee", | |
"avatar_url": "https://raw.githubusercontent.com/FZEEFlasher/fzeeflasher.github.io/main/assets/fzeelogo.webp", | |
"embeds": [{ | |
"title": "$TITLE", | |
"url": "$COMMIT_URL", | |
"color": 16291105, | |
"footer": { | |
"text": "GitHub Actions" | |
}, | |
"timestamp": "$(date --utc +%FT%TZ)", | |
"thumbnail": { | |
"url": "https://raw.githubusercontent.com/FZEEFlasher/fzeeflasher.github.io/main/assets/fzeeflasher_bot_banner.webp" | |
} | |
}] | |
} | |
EOF | |
env: | |
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} |