-
Notifications
You must be signed in to change notification settings - Fork 0
249 lines (210 loc) · 8.22 KB
/
BuildDockerPush.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
name: Build and push docker images
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]
workflow_dispatch:
schedule:
# Weekly 2am Monday morning
- cron: '0 2 * * MON'
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# Get version information
version:
name: Version
runs-on: ubuntu-latest
outputs:
# Build version number
SemVer2: ${{ steps.nbgv.outputs.SemVer2 }}
# ESPHome version number
ESPHomeVersion: ${{ steps.plv.outputs.version }}
# Push / release if not a pull request
# Test for strings not variables, e.g. needs.version.outputs.PushAction == 'true'
PushAction: ${{ github.event_name != 'pull_request' }}
# Tag as latest or develop
TagName: ${{ endsWith(github.ref, 'refs/heads/main') && 'latest' || 'develop' }}
# Tag list for docker build
TagList: ${{ steps.tags.outputs.tags }}
# Docker image
ImageName: docker.io/ptr727/esphome-nonroot
steps:
# Checkout code
# https://github.com/marketplace/actions/checkout
- name: Checkout
uses: actions/checkout@v4
with:
# Get all history for version calculation
fetch-depth: 0
# Run Nerdbank.GitVersioning
# https://github.com/marketplace/actions/nerdbank-gitversioning
- name: Run Nerdbank.GitVersioning tool
id: nbgv
uses: dotnet/nbgv@master
# curl --silent https://api.github.com/repos/esphome/esphome/releases/latest | jq -r .tag_name
# curl --silent https://pypi.org/pypi/esphome/json | jq -r '.releases | keys | .[]' | sort -V | tail -n 1
# curl --silent https://pypi.org/pypi/esphome/json | jq -r .info.version
# TODO: There is a slim chance that the github release is out of sync with pypi release
- name: Get ESPHome Package Latest Version
id: plv
run: |
echo "version=$(curl --silent https://pypi.org/pypi/esphome/json | jq -r .info.version)" >> $GITHUB_OUTPUT
# Create tags as comma separated list
- name: Get tag list
id: tags
run: |
if [[ "${{ endsWith(github.ref, 'refs/heads/main') }}" == "true" ]]; then
echo "tags=docker.io/ptr727/esphome-nonroot:latest,docker.io/ptr727/esphome-nonroot:${{ steps.plv.outputs.version }}" >> $GITHUB_OUTPUT
else
echo "tags=docker.io/ptr727/esphome-nonroot:develop" >> $GITHUB_OUTPUT
fi
# Build and push docker images
buildpush:
name: Build and push
runs-on: ubuntu-latest
needs: version
steps:
# Checkout code
# https://github.com/marketplace/actions/checkout
- name: Checkout
uses: actions/checkout@v4
# Setup QEMU for multi architecture builds
# https://github.com/marketplace/actions/docker-setup-qemu
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64,linux/arm64
# Setup docker build
# https://github.com/marketplace/actions/docker-setup-buildx
- name: Setup Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64
# Login to Docker Hub
# https://github.com/marketplace/actions/docker-login
- name: Login to Docker Hub
uses: docker/login-action@v3
if: ${{ needs.version.outputs.PushAction == 'true' }}
with:
registry: docker.io
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
# Docker build and push
# https://github.com/marketplace/actions/build-and-push-docker-images
- name: Docker build and push
uses: docker/build-push-action@v6
with:
context: ./Docker
push: ${{ needs.version.outputs.PushAction == 'true' }}
tags: ${{ needs.version.outputs.TagList }}
platforms: linux/amd64,linux/arm64
build-args: |
LABEL_VERSION=${{ needs.version.outputs.SemVer2 }}
ESPHOME_VERSION=${{ needs.version.outputs.ESPHomeVersion }}
# Get tool versions from image
toolversions:
name: Tool versions
runs-on: ubuntu-latest
needs: [buildpush, version]
if: ${{ needs.version.outputs.PushAction == 'true' }}
steps:
# Get image size
- name: Get image size
run: |
touch ${{ runner.temp }}/version.info
echo Image: ${{ needs.version.outputs.ImageName }}:${{ needs.version.outputs.TagName }} >> ${{ runner.temp }}/version.info
echo Size: $(docker manifest inspect -v ${{ needs.version.outputs.ImageName }}:${{ needs.version.outputs.TagName }} | jq '.[] | select(.Descriptor.platform.architecture=="amd64") | [.OCIManifest.layers[].size] | add' | numfmt --to=iec) >> ${{ runner.temp }}/version.info
# Get tool versions in container
# https://github.com/marketplace/actions/docker-run-action
- name: Write tool versions to file
uses: addnab/docker-run-action@v3
with:
image: "${{ needs.version.outputs.ImageName }}:${{ needs.version.outputs.TagName }}"
options: --volume ${{ runner.temp }}/version.info:/version.info
run: |
echo OS: $(. /etc/os-release; echo $PRETTY_NAME) >> /version.info
echo Python: $(python --version) >> /version.info
echo ESPHome: $(esphome version) >> /version.info
echo PlatformIO: $(pio --version) >> /version.info
# Print version file contents
- name: Print versions
run: cat ${{ runner.temp }}/version.info
# https://github.com/marketplace/actions/upload-a-build-artifact
- name: Upload version artifacts
uses: actions/upload-artifact@v4
with:
name: versions
path: ${{ runner.temp }}/version.info
# Update Docker README.md
updatereadme:
name: Create Docker README.md
runs-on: ubuntu-latest
needs: [toolversions, version]
if: ${{ needs.version.outputs.PushAction == 'true' }}
steps:
# https://github.com/marketplace/actions/checkout
- name: Checkout
uses: actions/checkout@v4
# https://github.com/marketplace/actions/download-a-build-artifact
- name: Download version artifacts
uses: actions/download-artifact@v4
with:
name: versions
path: ${{ runner.temp }}/versions
- name: Create README.md from README.m4
run: m4 --include=${{ runner.temp }}/versions ./Docker/README.m4 > ${{ runner.temp }}/README.md
# https://github.com/marketplace/actions/docker-hub-description
- name: Update Docker Hub README.md
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
repository: ptr727/esphome-nonroot
short-description: ${{ github.event.repository.description }}
readme-filepath: ${{ runner.temp }}/README.md
# Release
release:
name: Release
runs-on: ubuntu-latest
needs: [ buildpush, version ]
if: ${{ needs.version.outputs.PushAction == 'true' }}
permissions:
contents: write
steps:
# https://github.com/marketplace/actions/checkout
- name: Checkout Code
uses: actions/checkout@v4
# Create GitHub release
# https://github.com/marketplace/actions/gh-release
- name: Create GitHub release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
tag_name: ${{ needs.version.outputs.SemVer2 }}
prerelease: ${{ !endsWith(github.ref, 'refs/heads/main') }}
# Create a custom badge to report the build date
datebadge:
name: Date badge
runs-on: ubuntu-latest
needs: [release, version]
if: ${{ needs.version.outputs.PushAction == 'true' }}
permissions:
contents: write
steps:
# Get date from environment as a variable
- id: date
run: |
echo "date=$(date)" >> $GITHUB_OUTPUT
# Create badge
# https://github.com/marketplace/actions/bring-your-own-badge
- name: Build date badge
uses: RubbaBoy/BYOB@v1
with:
name: lastbuild
label: "Last Build"
icon: "github"
status: ${{ steps.date.outputs.date }}
color: "blue"
github_token: ${{ secrets.GITHUB_TOKEN }}