Skip to content

Commit 12e802f

Browse files
authored
Merge pull request #379 from LaurentGoderre/templating
Implement jq templating
2 parents f00ff05 + c01ba51 commit 12e802f

25 files changed

+380
-117
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Verify Templating
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
defaults:
8+
run:
9+
shell: 'bash -Eeuo pipefail -x {0}'
10+
11+
jobs:
12+
apply-templates:
13+
name: Check For Uncomitted Changes
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v3
17+
- name: Apply Templates
18+
run: ./apply-templates.sh
19+
- name: Check Git Status
20+
run: |
21+
status="$(git status --short)"
22+
[ -z "$status" ]

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.jq-template.awk

6.0/alpine/Dockerfile renamed to 6.0/alpine3.18/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM alpine:3.18
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

6.0/Dockerfile renamed to 6.0/bookworm/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM debian:bookworm-slim
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

6.2/alpine/Dockerfile renamed to 6.2/alpine3.18/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM alpine:3.18
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

6.2/Dockerfile renamed to 6.2/bookworm/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM debian:bookworm-slim
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

7.0/alpine/Dockerfile renamed to 7.0/alpine3.18/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM alpine:3.18
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

7.0/Dockerfile renamed to 7.0/bookworm/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM debian:bookworm-slim
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

7.2/alpine/Dockerfile renamed to 7.2/alpine3.18/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM alpine:3.18
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

7.2/Dockerfile renamed to 7.2/bookworm/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
3+
#
4+
# PLEASE DO NOT EDIT IT DIRECTLY.
5+
#
6+
17
FROM debian:bookworm-slim
28

39
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
File renamed without changes.

Dockerfile-alpine.template

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.18
1+
FROM alpine:{{ env.variant | ltrimstr("alpine") }}
22

33
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
44
RUN addgroup -S -g 1000 redis && adduser -S -G redis -u 999 redis
@@ -10,9 +10,9 @@ RUN apk add --no-cache \
1010
# add tzdata for https://github.com/docker-library/redis/issues/138
1111
tzdata
1212

13-
ENV REDIS_VERSION placeholder
14-
ENV REDIS_DOWNLOAD_URL placeholder
15-
ENV REDIS_DOWNLOAD_SHA placeholder
13+
ENV REDIS_VERSION {{ .version }}
14+
ENV REDIS_DOWNLOAD_URL {{ .downloadUrl }}
15+
ENV REDIS_DOWNLOAD_SHA {{ .sha256 }}
1616

1717
RUN set -eux; \
1818
\

Dockerfile.template

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
FROM debian:bookworm-slim
1+
FROM debian:{{ env.variant }}-slim
22

33
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
44
RUN groupadd -r -g 999 redis && useradd -r -g redis -u 999 redis
55

66
# grab gosu for easy step-down from root
77
# https://github.com/tianon/gosu/releases
8-
ENV GOSU_VERSION 1.16
8+
ENV GOSU_VERSION {{ .gosu.version }}
99
RUN set -eux; \
1010
savedAptMark="$(apt-mark showmanual)"; \
1111
apt-get update; \
@@ -26,9 +26,9 @@ RUN set -eux; \
2626
gosu --version; \
2727
gosu nobody true
2828

29-
ENV REDIS_VERSION placeholder
30-
ENV REDIS_DOWNLOAD_URL placeholder
31-
ENV REDIS_DOWNLOAD_SHA placeholder
29+
ENV REDIS_VERSION {{ .version }}
30+
ENV REDIS_DOWNLOAD_URL {{ .downloadUrl }}
31+
ENV REDIS_DOWNLOAD_SHA {{ .sha256 }}
3232

3333
RUN set -eux; \
3434
\

apply-templates.sh

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
[ -f versions.json ] # run "versions.sh" first
5+
6+
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
7+
8+
jqt='.jq-template.awk'
9+
if [ -n "${BASHBREW_SCRIPTS:-}" ]; then
10+
jqt="$BASHBREW_SCRIPTS/jq-template.awk"
11+
elif [ "$BASH_SOURCE" -nt "$jqt" ]; then
12+
# https://github.com/docker-library/bashbrew/blob/master/scripts/jq-template.awk
13+
wget -qO "$jqt" 'https://github.com/docker-library/bashbrew/raw/9f6a35772ac863a0241f147c820354e4008edf38/scripts/jq-template.awk'
14+
fi
15+
16+
if [ "$#" -eq 0 ]; then
17+
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
18+
eval "set -- $versions"
19+
fi
20+
21+
generated_warning() {
22+
cat <<-EOH
23+
#
24+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
25+
#
26+
# PLEASE DO NOT EDIT IT DIRECTLY.
27+
#
28+
29+
EOH
30+
}
31+
32+
for version; do
33+
export version
34+
35+
if [ -d "$version" ]; then
36+
rm -rf "$version"
37+
fi
38+
39+
if jq -e '.[env.version] | not' versions.json > /dev/null; then
40+
echo "skipping $version ..."
41+
continue
42+
fi
43+
44+
variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)"
45+
eval "variants=( $variants )"
46+
47+
for variant in "${variants[@]}"; do
48+
export variant
49+
50+
echo "processing $version/$variant ..."
51+
52+
dir="$version${variant:+/$variant}"
53+
54+
mkdir -p "$dir"
55+
56+
cp -f docker-entrypoint.sh "$dir/"
57+
58+
case "$variant" in
59+
alpine*)
60+
template='Dockerfile-alpine.template'
61+
sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh"
62+
;;
63+
*)
64+
template='Dockerfile.template'
65+
;;
66+
esac
67+
68+
{
69+
generated_warning
70+
gawk -f "$jqt" "$template"
71+
} > "$dir/Dockerfile"
72+
done
73+
done

generate-stackbrew-library.sh

+43-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -eu
33

44
declare -A aliases=(
@@ -9,11 +9,13 @@ declare -A aliases=(
99
self="$(basename "$BASH_SOURCE")"
1010
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
1111

12-
versions=( */ )
13-
versions=( "${versions[@]%/}" )
12+
if [ "$#" -eq 0 ]; then
13+
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
14+
eval "set -- $versions"
15+
fi
1416

1517
# sort version numbers with highest first
16-
IFS=$'\n'; versions=( $(echo "${versions[*]}" | sort -rV) ); unset IFS
18+
IFS=$'\n'; set -- $(sort -rV <<<"$*"); unset IFS
1719

1820
# get the most recent commit which modified any of "$@"
1921
fileCommit() {
@@ -68,51 +70,50 @@ join() {
6870
echo "${out#$sep}"
6971
}
7072

71-
for version in "${versions[@]}"; do
72-
for v in \
73-
'' alpine \
74-
; do
75-
dir="$version${v:+/$v}"
76-
variant="$(basename "$v")"
73+
for version; do
74+
export version
7775

78-
[ -f "$dir/Dockerfile" ] || continue
76+
variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)"
77+
eval "variants=( $variants )"
7978

80-
commit="$(dirCommit "$dir")"
79+
alpine="$(jq -r '.[env.version].alpine' versions.json)"
80+
debian="$(jq -r '.[env.version].debian' versions.json)"
8181

82-
fullVersion="$(git show "$commit":"$dir/Dockerfile" | awk '$1 == "ENV" && $2 == "REDIS_VERSION" { print $3; exit }')"
83-
84-
versionAliases=()
85-
while [ "$fullVersion" != "$version" -a "${fullVersion%[.]*}" != "$fullVersion" ]; do
86-
versionAliases+=( $fullVersion )
87-
fullVersion="${fullVersion%[.]*}"
88-
done
89-
versionAliases+=(
90-
$version
91-
${aliases[$version]:-}
92-
)
93-
94-
if [ -n "$variant" ]; then
95-
variantAliases=( "${versionAliases[@]/%/-$variant}" )
96-
variantAliases=( "${variantAliases[@]//latest-/}" )
97-
else
98-
variantAliases=( "${versionAliases[@]}" )
99-
fi
82+
fullVersion="$(jq -r '.[env.version].version' versions.json)"
10083

101-
variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")"
84+
versionAliases=()
85+
while [ "$fullVersion" != "$version" -a "${fullVersion%[.]*}" != "$fullVersion" ]; do
86+
versionAliases+=( $fullVersion )
87+
fullVersion="${fullVersion%[.]*}"
88+
done
89+
versionAliases+=(
90+
$version
91+
${aliases[$version]:-}
92+
)
10293

103-
suite="${variantParent#*:}" # "jessie-slim", "stretch"
104-
suite="${suite%-slim}" # "jessie", "stretch"
105-
106-
if [ "$v" = 'alpine' ]; then
107-
suite="alpine$suite" # "alpine3.8"
108-
suiteAliases=( "${versionAliases[@]/%/-$suite}" )
109-
else
110-
suiteAliases=( "${variantAliases[@]/%/-$suite}" )
111-
fi
112-
suiteAliases=( "${suiteAliases[@]//latest-/}" )
113-
variantAliases+=( "${suiteAliases[@]}" )
94+
for variant in "${variants[@]}"; do
95+
dir="$version/$variant"
96+
commit="$(dirCommit "$dir")"
97+
98+
variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")"
11499
variantArches="${parentRepoToArches[$variantParent]}"
115100

101+
variantAliases=( "${versionAliases[@]/%/-$variant}" )
102+
variantAliases=( "${variantAliases[@]//latest-/}" )
103+
104+
case "$variant" in
105+
"$debian")
106+
variantAliases=(
107+
"${versionAliases[@]}"
108+
"${variantAliases[@]}"
109+
)
110+
;;
111+
alpine"$alpine")
112+
variantAliases+=( "${versionAliases[@]/%/-alpine}" )
113+
variantAliases=( "${variantAliases[@]//latest-/}" )
114+
;;
115+
esac
116+
116117
echo
117118
cat <<-EOE
118119
Tags: $(join ', ' "${variantAliases[@]}")

0 commit comments

Comments
 (0)