Skip to content

Update Central Firmware Repository #2

Update Central Firmware Repository

Update Central Firmware Repository #2

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
run: |
cd Flash2MQTT/firmware/${{ github.event.repository.name }}
ls *.bin > bin_files.txt
echo '[' > variants.json
count=0
total=$(grep -vc '^$' bin_files.txt)
while read file; do
if [[ "$file" != *"OTA"* ]]; then
variant_name=$(echo $file | sed -e 's/.*_\([^_]*\)\.bin/\1/')
display_name=$(echo $variant_name | tr '_' ' ' | sed -e 's/\b\(.\)/\u\1/g')
echo ' {' >> variants.json
echo ' "displayName": "'"$display_name"'",' >> variants.json
echo ' "file": "https://all-solutions.github.io/Flash2MQTT/firmware/'"${{ github.event.repository.name }}"'/'"$file"'"' >> variants.json
echo -n ' }' >> variants.json
count=$((count + 1))
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
- name: Commit and Push Changes
run: |
cd Flash2MQTT
git add firmware/${{ github.event.repository.name }}
git commit -m "Update firmware for ${{ github.event.repository.name }} to version ${{ github.event.release.tag_name }}"
git push https://all-solutions:${{ secrets.CENTRAL_REPO_TOKEN }}@github.com/all-solutions/Flash2MQTT.git HEAD:master
- name: Update firmware_list.json
run: |
cd Flash2MQTT/firmware
# Install jq if not already installed
if ! command -v jq &> /dev/null; then
sudo apt-get update && sudo apt-get install -y jq
fi
# Update the version in firmware_list.json
if [ ! -f firmware_list.json ]; then
echo '[]' > firmware_list.json
fi
tmpfile=$(mktemp)
jq --arg name "${{ github.event.repository.name }}" --arg version "${{ github.event.release.tag_name }}" \
'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 ..
git add firmware/firmware_list.json
git commit -m "Update firmware_list.json with new version of ${{ github.event.repository.name }}"
git push https://all-solutions:${{ secrets.CENTRAL_REPO_TOKEN }}@github.com/all-solutions/Flash2MQTT.git HEAD:master