-
Notifications
You must be signed in to change notification settings - Fork 1
173 lines (155 loc) · 6.61 KB
/
update_Flash2MQTT.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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