-
Notifications
You must be signed in to change notification settings - Fork 104
283 lines (244 loc) · 9.93 KB
/
deploy-multi-mp.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
name: Deploy hass-addon-sunsynk-multi to ghcr.io
on:
release:
types:
- published
workflow_run:
workflows: ["CI"]
branches: [main]
types:
- completed
workflow_dispatch: {}
env:
REGISTRY_IMAGE: ghcr.io/${{ github.repository_owner }}/hass-addon-sunsynk-multi
jobs:
ci-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo 'CI failed'
information:
name: Gather add-on information
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'release' }}
outputs:
architectures: ${{ steps.information.outputs.architectures }}
build: ${{ steps.information.outputs.build }}
description: ${{ steps.information.outputs.description }}
environment: ${{ steps.release.outputs.environment }}
name: ${{ steps.information.outputs.name }}
slug: ${{ steps.information.outputs.slug }}
target: ${{ steps.information.outputs.target }}
version: ${{ steps.release.outputs.version }}
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v4
- name: 🚀 Run add-on information
id: information
uses: frenck/action-addon-information@v1.4
with:
path: ./hass-addon-sunsynk-multi
- name: ℹ️ Gather version and environment
id: release
run: |
sha="${{ github.sha }}"
# Default values
environment="edge"
version="${sha:0:7}"
# Check if it's a release event
if [[ "${{ github.event_name }}" == "release" ]]; then
version="${{ github.event.release.tag_name }}" # Lowercase the tag name
version="${version#v}" # Remove 'v' prefix if present
environment="stable"
# Set environment to beta if it's a prerelease
if [[ "${{ github.event.release.prerelease }}" == "true" ]]; then
environment="beta"
fi
fi
# Output the environment and version
echo "environment=${environment}" >> "$GITHUB_OUTPUT"
echo "version=${version}" >> "$GITHUB_OUTPUT"
# Log the outputs
cat "$GITHUB_OUTPUT" >> $GITHUB_STEP_SUMMARY
build:
name: 👷 Build & Deploy ${{ matrix.platform }} ${{ needs.information.outputs.environment }}/${{ needs.information.outputs.version }}
runs-on: ubuntu-latest
needs: information
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm/v6
- linux/arm/v7
- linux/arm64
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v4
- name: Prepare Files for Build
run: |
mkdir -p hass-addon-sunsynk-multi/sunsynk
cp -r src hass-addon-sunsynk-multi/sunsynk/
cp setup.* README.md hass-addon-sunsynk-multi/sunsynk/
- name: Set up the correct base image based on platform
run: |
case "${{ matrix.platform }}" in
linux/amd64) echo "BUILD_FROM=ghcr.io/home-assistant/amd64-base-python:3.12-alpine3.21" >> $GITHUB_ENV ;;
linux/arm64) echo "BUILD_FROM=ghcr.io/home-assistant/aarch64-base-python:3.12-alpine3.21" >> $GITHUB_ENV ;;
linux/arm/v6) echo "BUILD_FROM=ghcr.io/home-assistant/armhf-base-python:3.12-alpine3.21" >> $GITHUB_ENV ;;
linux/arm/v7) echo "BUILD_FROM=ghcr.io/home-assistant/armv7-base-python:3.12-alpine3.21" >> $GITHUB_ENV ;;
linux/i386) echo "BUILD_FROM=ghcr.io/home-assistant/i386-base-python:3.12-alpine3.21" >> $GITHUB_ENV ;;
esac
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache # Cache path
key: ${{ runner.os }}-buildx-${{ github.sha }} # Unique key for cache
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare sanitized platform name
id: sanitize-platform
run: |
SANITIZED_PLATFORM=$(echo ${{ matrix.platform }} | sed 's/^linux\///; s/\///g')
echo "SANITIZED_PLATFORM=${SANITIZED_PLATFORM}" >> $GITHUB_ENV
echo "Sanitized platform name: ${SANITIZED_PLATFORM}"
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: ./hass-addon-sunsynk-multi # Specify the directory containing your Dockerfile
file: ./hass-addon-sunsynk-multi/Dockerfile # Specify the Dockerfile path within the context
platforms: ${{ matrix.platform }}
cache-from: type=local,src=/tmp/.buildx-cache # Use cache for build
cache-to: type=local,dest=/tmp/.buildx-cache,mode=max # Save cache
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
build-args: |
BUILD_FROM=${{ env.BUILD_FROM }}
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.SANITIZED_PLATFORM }} # Use the sanitized platform name
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
- name: Save platform to file
id: save-platform
run: |
mkdir -p outputs
echo ${{ env.SANITIZED_PLATFORM }} >> outputs/${{ env.SANITIZED_PLATFORM }}.txt
- name: Artifact platform file for later
uses: actions/upload-artifact@v3
with:
name: outputs
path: outputs/*.txt
merge:
runs-on: ubuntu-latest
needs:
- information
- build
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Load outputs
uses: actions/download-artifact@v3
with:
name: outputs
path: outputs
- name: Load outputs to variable
id: platforms_raw
run: |
ls outputs
# Replace newlines with commas, then remove the trailing comma
echo "value=$(cat outputs/*.txt | tr '\n' ',' | sed 's/,$//')" >> "$GITHUB_OUTPUT"
- name: Generate tags for multi-platform image
run: |
# Define base tags
TAGS=(
"-t ${{ env.REGISTRY_IMAGE }}:${{ needs.information.outputs.version }}"
"-t ${{ env.REGISTRY_IMAGE }}:${{ needs.information.outputs.environment }}"
)
# Get the list of platforms from the build job output
platforms="${{ steps.platforms_raw.outputs.value }}"
# Split the platforms and add platform-specific tags
IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms"
for PLATFORM in "${PLATFORM_ARRAY[@]}"; do
TAGS+=("-t ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.version }}")
TAGS+=("-t ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.environment }}")
done
# Join tags into a string
TAGS_STRING=$(printf " %s" "${TAGS[@]}")
# Export the tags string to the environment for later use
echo "TAGS_STRING=${TAGS_STRING}" >> $GITHUB_ENV
# Log the generated tags for debugging
echo "Generated tags: ${TAGS_STRING}"
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $TAGS_STRING \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
# Inspect the image using the first tag from TAGS_STRING (without the leading '-t ' part)
INSPECT_TAG=$(echo "${TAGS_STRING}" | awk '{print $2}')
# Log the tag being inspected
echo "Inspecting image with tag: $INSPECT_TAG"
# Run the inspection
docker buildx imagetools inspect $INSPECT_TAG
publish_addon:
name: 🆕 Update addon version to ${{ needs.information.outputs.version }}
needs: [information, build, merge]
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Point edge to the new ref
run: |
sed -i 's/version:.*/version: "${{ needs.information.outputs.version }}"/g' hass-addon-sunsynk-edge/config.yaml
- name: Point multi to the new ref
if: "${{ needs.information.outputs.environment == 'stable'}}"
run: |
sed -i 's/version:.*/version: "${{ needs.information.outputs.version }}"/g' hass-addon-sunsynk-multi/config.yaml
- uses: stefanzweifel/git-auto-commit-action@v5.0.1
with:
branch: main
commit_user_email: kellerza@gmail.com
commit_message: Update addon version to ${{ needs.information.outputs.version }} [no ci]