Skip to content

Update Central Firmware Repository #38

Update Central Firmware Repository

Update Central Firmware Repository #38

name: Update Central Firmware Repository
on:
release:
types: [published]
workflow_dispatch:
jobs:
update-central-repo:
if: ${{ github.event.release.prerelease == false || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Git
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
- name: Clone Central Repository
run: |
git clone https://all-solutions:${{ secrets.CENTRAL_REPO_TOKEN }}@github.com/all-solutions/Flash2MQTT.git Flash2MQTT
- name: Download Firmware Assets
id: fetch-release
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const owner = context.repo.owner;
const repo = context.repo.repo;
let release;
let releaseTag;
if (context.eventName === 'release') {
releaseTag = context.payload.release.tag_name;
console.log(`Using release tag from event: ${releaseTag}`);
release = await github.rest.repos.getReleaseByTag({ owner, repo, tag: releaseTag });
} else {
release = await github.rest.repos.getLatestRelease({ owner, repo });
releaseTag = release.data.tag_name;
console.log(`Using latest release tag: ${releaseTag}`);
}
const assets = release.data.assets.filter(asset => asset.name.endsWith('.bin'));
if (assets.length === 0) {
core.setFailed('No .bin assets found in the release.');
return;
}
for (const asset of assets) {
const download = await github.rest.repos.getReleaseAsset({
owner,
repo,
asset_id: asset.id,
headers: {
Accept: 'application/octet-stream',
},
});
fs.writeFileSync(asset.name, Buffer.from(download.data));
console.log(`Downloaded ${asset.name}`);
}
core.setOutput('tag', releaseTag);
- name: List Downloaded Files
run: ls -la
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Sync, Update and Push Central Repository
env:
FIRMWARE_NAME: ${{ github.event.repository.name }}
RELEASE_VERSION: ${{ steps.fetch-release.outputs.tag }}
run: |
version="${RELEASE_VERSION#v}"
version="${version#V}"
version_pattern="V?${version}"
echo "Firmware Name: $FIRMWARE_NAME"
echo "Release Version: $version"
update_repo_contents() {
cd Flash2MQTT
git fetch origin main
git checkout -B main origin/main
mkdir -p "firmware/${FIRMWARE_NAME}"
rm -f "firmware/${FIRMWARE_NAME}"/*.bin
cp ../*.bin "firmware/${FIRMWARE_NAME}/"
printf 'Updated at %s\n' "$(date -u +'%Y-%m-%dT%H:%M:%SZ')" > "firmware/${FIRMWARE_NAME}/last_update.txt"
cd "firmware/${FIRMWARE_NAME}"
ls *.bin > bin_files.txt
total=0
count=0
while read file; do
if [[ "$file" =~ _${version_pattern}\.bin$ ]]; then
variant_part=$(echo "$file" | sed -E 's/^'"$FIRMWARE_NAME"'_//; s/_V?'"${version}"'\.bin$//')
variant_name="${variant_part}"
if [[ "$variant_name" == "esp32c3_supermini" || "$variant_name" == "esp32s3_supermini" || "$variant_name" == "waveshare_esp32_s3_eth" || "$variant_name" == "wemos_d1_mini32" ]]; then
total=$((total + 1))
fi
fi
done < bin_files.txt
echo '[' > variants.json
while read file; do
if [[ "$file" =~ _${version_pattern}\.bin$ ]]; then
variant_part=$(echo "$file" | sed -E 's/^'"$FIRMWARE_NAME"'_//; s/_V?'"${version}"'\.bin$//')
variant_name="${variant_part}"
case "$variant_name" in
"esp32c3_supermini") display_name="ESP32-C3 Super Mini" ;;
"esp32s3_supermini") display_name="ESP32-S3 Super Mini" ;;
"waveshare_esp32_s3_eth") display_name="Waveshare ESP32-S3 ETH" ;;
"wemos_d1_mini32") display_name="Wemos D1 Mini32" ;;
*) continue ;;
esac
count=$((count + 1))
echo ' {' >> variants.json
echo ' "displayName": "'"$display_name"'",' >> variants.json
echo ' "file": "https://all-solutions.github.io/Flash2MQTT/firmware/'"$FIRMWARE_NAME"'/'"$file"'"' >> variants.json
if [ $count -lt $total ]; then
echo ' },' >> variants.json
else
echo ' }' >> variants.json
fi
fi
done < bin_files.txt
echo ']' >> variants.json
rm bin_files.txt
cd ..
if [ ! -f firmware_list.json ]; then
echo '[]' > firmware_list.json
fi
tmpfile=$(mktemp)
jq --arg name "$FIRMWARE_NAME" --arg version "$version" \
'if any(.[]; .name == $name) then map(if .name == $name then .version = $version else . end) else . + [{"name": $name, "version": $version}] end' \
firmware_list.json > "$tmpfile" && mv "$tmpfile" firmware_list.json
cd ../..
}
for attempt in 1 2 3; do
echo "Push attempt ${attempt}/3"
update_repo_contents
cd Flash2MQTT
git add "firmware/${FIRMWARE_NAME}" "firmware/firmware_list.json"
git commit -m "Update firmware for ${FIRMWARE_NAME} to version ${RELEASE_VERSION}" || true
if git push https://all-solutions:${{ secrets.CENTRAL_REPO_TOKEN }}@github.com/all-solutions/Flash2MQTT.git HEAD:main; then
exit 0
fi
cd ..
echo "Push failed because the remote changed. Retrying with a fresh sync..."
sleep 2
done
echo "Failed to push firmware update after 3 attempts."
exit 1