-
Notifications
You must be signed in to change notification settings - Fork 1
150 lines (133 loc) · 5.89 KB
/
release-hook.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
name: Firmware release hook
run-name: > # "MCH2022 firmware dev release v1.2.3"
${{ github.event.client_payload.device_name }} firmware
${{ github.event.client_payload.channel != 'release' && github.event.client_payload.channel || '' }}
release ${{ github.event.client_payload.tag }}
on:
repository_dispatch:
types: [firmware-release]
jobs:
pull-and-pr:
name: Get new ${{ github.event.client_payload.device_name }} firmware & create PR
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Check payload
id: input
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
payload=$(
cat << $EOF
${{ toJson(github.event.client_payload) }}
$EOF
);
valid_payload=$(jq -r 'select(
(.device_id | type == "string") and
(.device_name | type == "string") and
(.tag | type == "string") and
(.channel | type == "string") and
(.fw_main | type == "string")
)' <<< "$payload");
[ -n "$valid_payload" ] || exit 1
device_id="${{ github.event.client_payload.device_id }}"
badge_json=$(jq -r --arg id "$device_id" '.[] | select(.id == $id)' badges.json)
if [ -z "$badge_json" ]; then
echo "No entry for device '$device_id' in badges.json" >&2
exit 1
fi
# output checked payload fields
echo "tag=${{ github.event.client_payload.tag }}" >> $GITHUB_OUTPUT
echo "repo=$(jq -r '.firmware_repo' <<< "$badge_json")" >> $GITHUB_OUTPUT
echo "channel=${{ github.event.client_payload.channel }}" >> $GITHUB_OUTPUT
echo "fw_main=${{ github.event.client_payload.fw_main }}" >> $GITHUB_OUTPUT
echo "device_id=${{ github.event.client_payload.device_id }}" >> $GITHUB_OUTPUT
echo "device_name=${{ github.event.client_payload.device_name }}" >> $GITHUB_OUTPUT
- name: Get release info
id: release_info
run: |
release_info=$(
gh release \
--repo "${{ steps.input.outputs.repo }}" \
view "${{ steps.input.outputs.tag }}"
);
echo "Release info:";
echo "$release_info";
release_header=$(awk '/[a-z]+:/' <<< "$release_info")
release_assets=$(grep "^asset:" <<< "$release_header" | cut -f 2-)
release_description=$(awk '/--/,0' <<< "$release_info" | tail +2)
# output info fields
for label in 'title' 'tag' 'draft' 'prerelease' 'author' 'created' 'published' 'url'; do
echo "$label=$(grep "^${label}:" <<< "$release_header" | cut -f 2-)" >> $GITHUB_OUTPUT;
done
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "assets<<$EOF" >> $GITHUB_OUTPUT
echo "$release_assets" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
echo "description<<$EOF" >> $GITHUB_OUTPUT
echo "$release_description" >> $GITHUB_OUTPUT
echo "$EOF" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ github.token }}
- name: Fetch & Integrate ${{ steps.input.outputs.device_name }} release
run: |
version="${{ steps.input.outputs.tag }}"
channel="${{ steps.input.outputs.channel }}"
main_bin="${{ steps.input.outputs.fw_main }}"
device_id="${{ steps.input.outputs.device_id }}"
device_name="${{ steps.input.outputs.device_name }}"
release_assets="${{ steps.release_info.outputs.assets }}"
echo "Adopting $device_name firmware version $version to channel '$channel'";
if ! grep -oq "^${main_bin}" <<< "$release_assets"; then
echo "❌ no asset '$main_bin' on release";
exit 1;
fi
gh release --repo "${{ steps.input.outputs.repo }}" \
download "$version" \
-p $main_bin \
-O $([ $channel == 'release' ] && echo "${device_id}.bin" || echo "${device_id}_${channel}.bin") \
--clobber
main_elf=${main_bin/%.bin/.elf}
if ! grep -oq "^${main_elf}" <<< "$release_assets"; then
echo "⚠️ no asset '$main_elf' on release";
else
gh release --repo "${{ steps.input.outputs.repo }}" \
download "$version" \
-p "$main_elf" \
-O "${device_id}-${version}.elf" \
--clobber
fi
release_url="${{ steps.release_info.outputs.url }}"
# update badges.json
jq -r \
--arg id $device_id \
--arg channel $channel \
--arg version $version \
--arg date $(date -I) \
--arg url $release_url \
'map(
select(.id == $id) |= (
if $channel == "release" then .version else .["version_" + $channel] end = {
name: $version,
date: $date,
url: $url
}
)
)' badges.json | tee badges.json.new && mv badges.json.new badges.json
env:
GH_TOKEN: ${{ github.token }}
- id: create_pr
name: Create PR
uses: peter-evans/create-pull-request@v5
env:
message: >
${{ steps.input.outputs.device_name }} firmware
${{ steps.input.outputs.channel != 'release' && steps.input.outputs.channel || '' }}
release ${{ steps.input.outputs.tag }}
with:
branch: ${{ steps.input.outputs.device_id }}/${{ steps.input.outputs.channel }}
title: ${{ env.message }}
commit-message: ${{ env.message }}
assignees: ${{ steps.release_info.outputs.author }}
body: ${{ steps.release_info.outputs.description }}
delete-branch: true