Skip to content

Commit a664b71

Browse files
Implement jq templating
1 parent f00ff05 commit a664b71

25 files changed

+374
-113
lines changed
Lines changed: 22 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 73 additions & 0 deletions
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

Lines changed: 37 additions & 38 deletions
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=(
@@ -69,50 +69,49 @@ join() {
6969
}
7070

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

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

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

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
80+
fullVersion="$(jq -r '.[env.version].version' versions.json)"
10081

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

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[@]}" )
92+
for variant in "${variants[@]}"; do
93+
dir="$version/$variant"
94+
commit="$(dirCommit "$dir")"
95+
96+
variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")"
11497
variantArches="${parentRepoToArches[$variantParent]}"
11598

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

update.sh

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,5 @@ set -Eeuo pipefail
33

44
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
55

6-
versions=( "$@" )
7-
if [ ${#versions[@]} -eq 0 ]; then
8-
versions=( */ )
9-
fi
10-
versions=( "${versions[@]%/}" )
11-
12-
packagesUrl='https://raw.githubusercontent.com/redis/redis-hashes/master/README'
13-
packages="$(echo "$packagesUrl" | sed -r 's/[^a-zA-Z.-]+/-/g')"
14-
trap "$(printf 'rm -f %q' "$packages")" EXIT
15-
curl -fsSL "$packagesUrl" -o "$packages"
16-
17-
for version in "${versions[@]}"; do
18-
rcVersion="${version%-rc}"
19-
20-
line="$(
21-
awk '
22-
{ gsub(/^redis-|[.]tar[.]gz$/, "", $2) }
23-
$1 == "hash" && $2 ~ /^'"$rcVersion"'([.]|$)/ { print }
24-
' "$packages" \
25-
| sort -rV \
26-
| head -1
27-
)"
28-
29-
if [ -n "$line" ]; then
30-
fullVersion="$(cut -d' ' -f2 <<<"$line")"
31-
downloadUrl="$(cut -d' ' -f5 <<<"$line")"
32-
shaHash="$(cut -d' ' -f4 <<<"$line")"
33-
shaType="$(cut -d' ' -f3 <<<"$line")"
34-
elif [ "$version" != "$rcVersion" ] && fullVersion="$(
35-
git ls-remote --tags https://github.com/redis/redis.git "refs/tags/$rcVersion*" \
36-
| cut -d/ -f3 \
37-
| cut -d^ -f1 \
38-
| sort -urV \
39-
| head -1
40-
)" && [ -n "$fullVersion" ]; then
41-
downloadUrl="https://github.com/redis/redis/archive/$fullVersion.tar.gz"
42-
shaType='sha256'
43-
shaHash="$(curl -fsSL "$downloadUrl" | "${shaType}sum" | cut -d' ' -f1)"
44-
else
45-
echo >&2 "error: full version for $version cannot be determined"
46-
exit 1
47-
fi
48-
[ "$shaType" = 'sha256' ] || [ "$shaType" = 'sha1' ]
49-
50-
echo "$version: $fullVersion"
51-
52-
for variant in \
53-
alpine '' \
54-
; do
55-
dir="$version${variant:+/$variant}"
56-
[ -d "$dir" ] || continue
57-
template="Dockerfile${variant:+-$variant}.template"
58-
59-
sed -r \
60-
-e 's/^(ENV REDIS_VERSION) .*/\1 '"$fullVersion"'/' \
61-
-e 's!^(ENV REDIS_DOWNLOAD_URL) .*!\1 '"$downloadUrl"'!' \
62-
-e 's/^(ENV REDIS_DOWNLOAD_SHA) .*/\1 '"$shaHash"'/' \
63-
-e 's!sha[0-9]+sum!'"$shaType"'sum!g' \
64-
"$template" > "$dir/Dockerfile"
65-
66-
cp -a docker-entrypoint.sh "$dir/"
67-
if [ "$variant" = 'alpine' ]; then
68-
sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh"
69-
fi
70-
done
71-
done
6+
./versions.sh "$@"
7+
./apply-templates.sh "$@"

0 commit comments

Comments
 (0)