Skip to content

Update Central Firmware Repository #8

Update Central Firmware Repository

Update Central Firmware Repository #8

name: Update Central Firmware Repository
on:
release:
types: [published]
jobs:
update-central-repo:
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 '[email protected]'
- 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
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;
const releaseTag = context.payload.release.tag_name;
// Get the release by tag name
const release = await github.rest.repos.getReleaseByTag({
owner,
repo,
tag: releaseTag,
});
// Filter assets that end with .bin
const assets = release.data.assets.filter(asset => asset.name.endsWith('.bin'));
if (assets.length === 0) {
core.setFailed('No .bin assets found in the release.');
}
for (const asset of assets) {
const download = await github.rest.repos.getReleaseAsset({
owner,
repo,
asset_id: asset.id,
headers: {
Accept: 'application/octet-stream',
},
});
// Write the asset to a file
fs.writeFileSync(asset.name, Buffer.from(download.data));
console.log(`Downloaded ${asset.name}`);
}
- name: List Downloaded Files
run: ls -la
- name: Copy Firmware Files
run: |
mkdir -p Flash2MQTT/firmware/${{ github.event.repository.name }}
cp *.bin Flash2MQTT/firmware/${{ github.event.repository.name }}/
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Update variants.json and firmware_list.json
env:
FIRMWARE_NAME: ${{ github.event.repository.name }}
RELEASE_VERSION: ${{ github.event.release.tag_name }}
run: |
cd Flash2MQTT/firmware/${FIRMWARE_NAME}
ls *.bin > bin_files.txt
# Initialize variables
total=0
count=0
# Remove 'v' or 'V' prefix from version if present
version="${RELEASE_VERSION#v}"
version="${version#V}"
echo "Firmware Name: $FIRMWARE_NAME"
echo "Release Version: $version"
# Determine total number of desired variants
echo "Determining total number of desired variants..."
while read file; do
echo "Processing file: $file"
if [[ "$file" == *"_${version}.bin" ]]; then
echo "File matches current release version."
variant_part=$(echo "$file" | sed -E 's/^'"$FIRMWARE_NAME"'_//; s/_'"${version}"'\.bin$//')
variant_name="${variant_part%%_*}" # Extract the base variant name
echo "Extracted variant_name: $variant_name"
if [[ "$variant_name" == "d1_mini" || "$variant_name" == "esp01" ]]; then
echo "Variant $variant_name is desired. Incrementing total."
total=$((total + 1))
else
echo "Variant $variant_name is not desired."
fi
else
echo "File does not match current release version."
fi
done < bin_files.txt
echo "Total desired variants: $total"
# Start building variants.json
echo '[' > variants.json
# Process files and create variants.json
echo "Building variants.json..."
while read file; do
echo "Processing file: $file"
if [[ "$file" == *"_${version}.bin" ]]; then
echo "File matches current release version."
variant_part=$(echo "$file" | sed -E 's/^'"$FIRMWARE_NAME"'_//; s/_'"${version}"'\.bin$//')
variant_name="${variant_part%%_*}" # Extract the base variant name
echo "Extracted variant_name: $variant_name"
case "$variant_name" in
"d1_mini")
display_name="D1 Mini"
;;
"esp01")
display_name="ESP-01"
;;
*)
echo "Variant $variant_name is not desired. Skipping."
continue
;;
esac
count=$((count + 1))
echo "Adding variant $display_name to variants.json."
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
else
echo "File does not match current release version."
fi
done < bin_files.txt
echo ']' >> variants.json
rm bin_files.txt
# Update firmware_list.json
cd ..
# Install jq if not already installed
if ! command -v jq &> /dev/null; then
sudo apt-get update && sudo apt-get install -y jq
fi
# Initialize firmware_list.json if it doesn't exist
if [ ! -f firmware_list.json ]; then
echo '[]' > firmware_list.json
fi
# Update firmware_list.json
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
- name: Commit and Push Changes
run: |
cd Flash2MQTT
git add firmware/${{ github.event.repository.name }}
git add firmware/firmware_list.json
git commit -m "Update firmware for ${{ github.event.repository.name }} to version $version"
git pull --rebase origin main
git push https://all-solutions:${{ secrets.CENTRAL_REPO_TOKEN }}@github.com/all-solutions/Flash2MQTT.git HEAD:main