-
-
Notifications
You must be signed in to change notification settings - Fork 1
172 lines (166 loc) · 6.42 KB
/
build.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
# Most of this file is copied from https://github.com/TECH7Fox/asterisk-hass-addons/blob/main/.github/workflows/ci.yaml
name: Build and push to Docker Hub
'on':
push:
branches:
- 'main'
tags:
- 'v*'
pull_request:
branches:
- 'main'
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
prepare:
runs-on: ubuntu-latest
steps:
- name: Get lowercase GitHub username
id: repository_owner
uses: ASzc/change-string-case-action@v6
with:
string: ${{ github.repository_owner }}
- name: Set outputs
id: set-outputs
run: |
echo 'image=${{ steps.repository_owner.outputs.lowercase }}/localui' >> "${GITHUB_OUTPUT}"
# Only enable push on push events or pull requests coming from the same repository, except from dependabot
echo 'push=${{ github.event_name == 'push' || github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' }}' >> "${GITHUB_OUTPUT}"
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ steps.set-outputs.outputs.image }}
tags: |
type=raw,value=nightly
type=ref,event=pr
type=semver,pattern={{version}}
outputs:
image: ${{ steps.set-outputs.outputs.image }}
push: ${{ steps.set-outputs.outputs.push }}
meta-version: ${{ steps.meta.outputs.version }}
meta-labels: ${{ steps.meta.outputs.labels }}
meta-json: ${{ steps.meta.outputs.json }}
build:
needs:
- prepare
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64/v8
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Needed to calculate branch for tag
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
if: needs.prepare.outputs.push == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set cache flags
id: cache-flags
run: |
# Set the cache-to output
echo 'cache-to=type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }}' >> "${GITHUB_OUTPUT}"
# Set the cache-from output
if [[ '${{ github.event_name }}' == 'push' ]]; then
if [[ '${{ github.ref }}' == 'refs/tags/v'* ]]; then
# Use cache from the branch when building a tag
branch="$(git branch -r --contains '${{ github.ref }}')"
branch="${branch##*/}"
echo "cache-from=type=gha,scope=${branch}-${{ matrix.platform }}" >> "${GITHUB_OUTPUT}"
else
# Use cache from the same branch when not building a tag
echo 'cache-from=type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }}' >> "${GITHUB_OUTPUT}"
fi
else
# Use cache from target branch too when building a pull request
# In this case, it has to be a multiline string
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "cache-from<<${EOF}" >> "${GITHUB_OUTPUT}"
printf '%s\n' \
"type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }}" \
"type=gha,scope=${{ github.base_ref }}-${{ matrix.platform }}" \
>> "${GITHUB_OUTPUT}"
echo "${EOF}" >> "${GITHUB_OUTPUT}"
fi
- name: Build and push by digest
id: build
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ needs.prepare.outputs.meta-labels }}
outputs: |
type=image,name=${{ needs.prepare.outputs.image }},push-by-digest=true,name-canonical=true,push=${{ needs.prepare.outputs.push }}
cache-from: |
${{ steps.cache-flags.outputs.cache-from }}
cache-to: |
${{ steps.cache-flags.outputs.cache-to }}
- 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@v3
with:
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
push:
needs:
- prepare
- build
runs-on: ubuntu-latest
steps:
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
if: needs.prepare.outputs.push == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create manifest list and push
if: needs.prepare.outputs.push == 'true'
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -r '"-t " + (.tags | join(" -t "))' <<< '${{ needs.prepare.outputs.meta-json }}') \
$(printf '${{ needs.prepare.outputs.image }}@sha256:%s ' *)
- name: Inspect image
if: needs.prepare.outputs.push == 'true'
run: |
docker buildx imagetools inspect '${{ needs.prepare.outputs.image }}:${{ needs.prepare.outputs.meta-version }}'
- name: Get repository README
uses: actions/checkout@v4
with:
sparse-checkout: README.md
sparse-checkout-cone-mode: false
- name: Update Docker Hub description
uses: peter-evans/dockerhub-description@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: ${{ needs.prepare.outputs.image }}
short-description: ${{ github.event.repository.description }}
enable-url-completion: true