generated from home-assistant/addons-example
-
Notifications
You must be signed in to change notification settings - Fork 0
184 lines (159 loc) · 6.14 KB
/
build-addons.yaml
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
173
174
175
176
177
178
179
180
181
182
183
184
name: Build Addons
env:
MONITORED_FILES: "apparmor.txt build.yaml config.yaml Dockerfile rootfs"
on:
push:
branches:
- main
tags:
- "*_v*"
pull_request:
permissions:
contents: read
jobs:
addons_to_build:
runs-on: ubuntu-latest
name: Find add-ons to build
outputs:
addons_to_build: ${{ steps.addons_to_build.outputs.addons_to_build }}
steps:
- name: Check out the repository
uses: actions/checkout@v4.0.0
- name: Find add-on directories
id: addons
uses: home-assistant/actions/helpers/find-addons@master
- name: Get changed files
id: changed_files
# If pushing a tag only, this step will fail.
if: "!startsWith(github.ref, 'refs/tags/')"
uses: jitterbit/get-changed-files@v1
- name: Get tagged add-on
id: tagged_addon
if: startsWith(github.ref, 'refs/tags/')
shell: bash
run: |
split=($(echo "${GITHUB_REF_NAME}" | tr '_' ' '))
if [[ "${#split[@]}" -ne 2 ]]; then
echo "Unparsable tag name: ${GITHUB_REF_NAME}"
exit 1
fi
name="${split[0]}"
version="${split[1]}"
echo "Parsed add-on name: '${name}'"
echo "Parsed add-on version: '${version}'"
for addon in ${{ steps.addons.outputs.addons }}; do
if [[ "${name}" == "${addon}" ]]; then
echo "Parsed add-on name valid."
echo "name=${name}" >> "${GITHUB_OUTPUT}"
echo "version=${version}" >> "${GITHUB_OUTPUT}"
exit 0
fi
done
echo "Parsed add-on name not valid. Will not build."
echo "name=" >> "${GITHUB_OUTPUT}"
echo "version=" >> "${GITHUB_OUTPUT}"
- name: Get tagged add-on information
id: tagged_addon_info
if: steps.tagged_addon.outputs.name != ''
uses: home-assistant/actions/helpers/info@master
with:
path: "./${{ steps.tagged_addon.outputs.name }}"
- name: Check tagged add-on has image name
if: steps.tagged_addon.outputs.name != ''
shell: bash
run: |
if [[ "${{ steps.tagged_addon_info.outputs.image }}" == "null" ]]; then
echo "Tagged add-on does not have an image name. Cannot build."
exit 1
fi
- name: Get add-ons to build
id: addons_to_build
shell: bash
run: |
addons_to_build=()
for addon in ${{ steps.addons.outputs.addons }}; do
if [[ "${addon}" == "${{ steps.tagged_addon.outputs.name }}" ]]; then
addons_to_build+=("{\"name\":\"${addon}\",\"deploy\":true},")
continue
fi
if [[ "${{ steps.changed_files.outputs.all }}" =~ "${addon}" ]]; then
for file in ${{ env.MONITORED_FILES }}; do
if [[ "${{ steps.changed_files.outputs.all }}" =~ "${addon}/${file}" ]]; then
if [[ ! "${addons_to_build[@]}" =~ "${addon}" ]]; then
addons_to_build+=("{\"name\":\"${addon}\",\"deploy\":false},")
fi
fi
done
fi
done
if [[ "${#addons_to_build[@]}" -gt 0 ]]; then
output="[$(echo ${addons_to_build[@]} | rev | cut -c 2- | rev)]"
echo "Changed add-ons: $output"
echo "addons_to_build=${output}" >> "${GITHUB_OUTPUT}"
else
echo "No add-on to build."
echo "Monitored files: (${{ env.MONITORED_FILES }})"
echo "addons_to_build=[]" >> "${GITHUB_OUTPUT}"
fi
build:
needs: addons_to_build
runs-on: ubuntu-latest
name: Build ${{ matrix.arch }} ${{ matrix.addon.name }} add-on
# https://github.com/orgs/community/discussions/27125#discussioncomment-3254720
if: fromJson(needs.addons_to_build.outputs.addons_to_build)[0] != null
permissions:
contents: read
packages: write
strategy:
matrix:
addon: ${{ fromJson(needs.addons_to_build.outputs.addons_to_build) }}
arch: ["aarch64", "amd64", "armhf", "armv7", "i386"]
steps:
- name: Check out repository
uses: actions/checkout@v4.0.0
- name: Get information
id: info
uses: home-assistant/actions/helpers/info@master
with:
path: "./${{ matrix.addon.name }}"
- name: Check if add-on should be built
id: check
run: |
if [[ "${{ steps.info.outputs.architectures }}" =~ "${{ matrix.arch }}" ]]; then
echo "build_arch=true" >> "${GITHUB_OUTPUT}"
else
echo "${{ matrix.arch }} is not a valid arch for ${{ matrix.addon.name }}, skipping build";
echo "build_arch=false" >> "${GITHUB_OUTPUT}"
fi
- name: Compute extra args
id: compute_extra_args
shell: bash
run: |
extra_args=()
if [[ "${{ fromJson(steps.info.outputs.image) }}" == "null" ]]; then
extra_args+=(
--docker-hub "ghcr.io/${{ github.repository_owner }}"
--image "{arch}-addon-${{ matrix.addon.name }}"
)
fi
if [[ "${{ matrix.addon.deploy }}" == "false" ]]; then
extra_args+=(--test)
fi
echo "Extra args: ${extra_args[@]@Q}"
echo "extra_args=${extra_args[@]@Q}" >> "${GITHUB_OUTPUT}"
- name: Login to GitHub Container Registry
if: matrix.addon.deploy
uses: docker/login-action@v3.0.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build ${{ matrix.addon.name }} add-on
if: steps.check.outputs.build_arch == 'true'
uses: home-assistant/builder@2023.09.0
with:
args: |
${{ steps.compute_extra_args.outputs.extra_args }} \
--${{ matrix.arch }} \
--target /data/${{ matrix.addon.name }} \
--addon