Repository navigation
157 lines (140 loc) · 6.48 KB
/
Copy pathdocker-image.yml
File metadata and controls
157 lines (140 loc) · 6.48 KB
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
# This file is part of the Ansel project.
# Copyright (C) 2026 Aurélien PIERRE.
# Copyright (C) 2026 Guillaume Stutin.
#
# Ansel is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansel is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansel. If not, see <http://www.gnu.org/licenses/>.
name: Build and Push Docker Image
on:
workflow_dispatch:
schedule:
- cron: "0 5 * * *"
permissions:
contents: write # the image is also attached to the monthly nightly release
jobs:
# The cron fires whether master moved or not. Before this job, every night on an unchanged
# head rebuilt the image, pushed it to Docker Hub again and replaced the archive the release
# already held for that commit. The release is asked first; tools/nightly_published.sh matches the commit hash in
# the asset names. A manual dispatch builds anyway, since rebuilding on purpose is what
# someone dispatching wants.
#
# The archive is what is asked about. The Docker Hub push needs no question of its own: it
# comes earlier in the same job, so an archive on the release means the image went out too.
#
# The build checks out the commit judged here, not the branch again, so a push landing in
# between cannot have one commit checked and another built.
published:
name: Is this commit already published
runs-on: ubuntu-latest
outputs:
sha: ${{ steps.check.outputs.sha }}
build: ${{ steps.check.outputs.build }}
steps:
- uses: actions/checkout@v6
with:
ref: ${{ github.ref_name }}
fetch-depth: 1
submodules: false
- name: Ask the release
id: check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
EVENT: ${{ github.event_name }}
run: |
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
if [ "${EVENT}" != "workflow_dispatch" ] && tools/nightly_published.sh \
'Ansel-@VERSION@-docker.tar.zst'; then
echo "already published for this commit, not rebuilt"
echo "build=false" >> "$GITHUB_OUTPUT"
else
echo "build=true" >> "$GITHUB_OUTPUT"
fi
docker:
needs: published
if: ${{ needs.published.outputs.build == 'true' }}
runs-on: ubuntu-latest
steps:
- name: Checkout ansel source
uses: actions/checkout@v6
with:
ref: ${{ needs.published.outputs.sha }}
# Full history: get_git_version_string.sh must reach the last version tag, or
# the version degrades to a bare hash. (An earlier edit put this only in a
# comment and left the key at 1; the first master run showed VERSION "b4597470".)
fetch-depth: 0
fetch-tags: true
submodules: true
# The image is built from a context without .git (.dockerignore), so the version
# string and commit hash are computed here, where git is, and passed in.
- name: Compute version
run: |
version=$(sh tools/get_git_version_string.sh)
echo "ANSEL_VERSION=${version}" >> "$GITHUB_ENV"
echo "ANSEL_COMMIT_HASH=$(git rev-parse HEAD)" >> "$GITHUB_ENV"
# The git string is 0.0.0+4824~gc9a3f02e8b. Every other package writes it into
# its filename with "." for "~" (0.0.0+4824.gc9a3f02e8b), and that is what
# tools/nightly_manifest.py parses; the archive name follows suit. A Docker
# tag additionally forbids "+" (and "~" -- the first run died on it), so the
# tag is the filename form with "+" as "-". The manifest undoes exactly that.
file_version=${version//\~/.}
echo "ANSEL_FILE_VERSION=${file_version}" >> "$GITHUB_ENV"
echo "ANSEL_DOCKER_TAG=${file_version//+/-}" >> "$GITHUB_ENV"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: .ci/Dockerfile
push: true
build-args: |
ANSEL_VERSION=${{ env.ANSEL_VERSION }}
ANSEL_COMMIT_HASH=${{ env.ANSEL_COMMIT_HASH }}
ANSEL_BUILD_CHANNEL=nightly
tags: |
aurelienpierre/ansel:current
aurelienpierre/ansel:${{ env.ANSEL_DOCKER_TAG }}
# The same image, as a file on the monthly release next to the other formats, so
# the nightly manifest can link to it and a user without Docker Hub access (or
# who wants the exact build from a given night, months later) can
# `zstd -d < Ansel-*-docker.tar.zst | docker load`.
#
# A release asset must be under 2 GiB. The guard fails the step outright rather
# than let tip upload a file GitHub would reject after the transfer.
- name: Save the image for the release
run: |
command -v zstd >/dev/null || { sudo apt-get update -q && sudo apt-get install -y -q zstd; }
docker pull -q "aurelienpierre/ansel:${ANSEL_DOCKER_TAG}"
file="Ansel-${ANSEL_FILE_VERSION}-docker.tar.zst"
docker save "aurelienpierre/ansel:${ANSEL_DOCKER_TAG}" | zstd -T0 -19 -q -o "${file}"
size=$(stat -c %s "${file}")
echo "ANSEL_DOCKER_FILE=${file}" >> "$GITHUB_ENV"
echo "Image archive: ${file}, $((size / 1048576)) MiB" >> "$GITHUB_STEP_SUMMARY"
if (( size >= 2147483648 )); then
echo "::error::${file} is $((size / 1048576)) MiB, over GitHub's 2 GiB asset limit -- slim the image (multi-stage Dockerfile) before it can ship on the release"
exit 1
fi
- name: Compute the release tag
run: echo "TAG=nightly-$(date -u +%Y-%m)" >> "$GITHUB_ENV"
- name: Attach to the nightly release
uses: aurelienpierreeng/tip@master
with:
tag: ${{ env.TAG }}
rm: false
token: ${{ secrets.GITHUB_TOKEN }}
files: ${{ env.ANSEL_DOCKER_FILE }}