Skip to content

Commit

Permalink
Implement jq templating
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGoderre committed Oct 19, 2023
1 parent f00ff05 commit c01ba51
Show file tree
Hide file tree
Showing 25 changed files with 380 additions and 117 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/verify-templating.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Verify Templating

on:
pull_request:
push:

defaults:
run:
shell: 'bash -Eeuo pipefail -x {0}'

jobs:
apply-templates:
name: Check For Uncomitted Changes
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Apply Templates
run: ./apply-templates.sh
- name: Check Git Status
run: |
status="$(git status --short)"
[ -z "$status" ]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.jq-template.awk
6 changes: 6 additions & 0 deletions 6.0/alpine/Dockerfile → 6.0/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM alpine:3.18

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 6.0/Dockerfile → 6.0/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM debian:bookworm-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 6.2/alpine/Dockerfile → 6.2/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM alpine:3.18

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 6.2/Dockerfile → 6.2/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM debian:bookworm-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.0/alpine/Dockerfile → 7.0/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM alpine:3.18

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.0/Dockerfile → 7.0/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM debian:bookworm-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.2/alpine/Dockerfile → 7.2/alpine3.18/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM alpine:3.18

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions 7.2/Dockerfile → 7.2/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM debian:bookworm-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions Dockerfile-alpine.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.18
FROM alpine:{{ env.variant | ltrimstr("alpine") }}

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

ENV REDIS_VERSION placeholder
ENV REDIS_DOWNLOAD_URL placeholder
ENV REDIS_DOWNLOAD_SHA placeholder
ENV REDIS_VERSION {{ .version }}
ENV REDIS_DOWNLOAD_URL {{ .downloadUrl }}
ENV REDIS_DOWNLOAD_SHA {{ .sha256 }}

RUN set -eux; \
\
Expand Down
10 changes: 5 additions & 5 deletions Dockerfile.template
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM debian:bookworm-slim
FROM debian:{{ env.variant }}-slim

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

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

ENV REDIS_VERSION placeholder
ENV REDIS_DOWNLOAD_URL placeholder
ENV REDIS_DOWNLOAD_SHA placeholder
ENV REDIS_VERSION {{ .version }}
ENV REDIS_DOWNLOAD_URL {{ .downloadUrl }}
ENV REDIS_DOWNLOAD_SHA {{ .sha256 }}

RUN set -eux; \
\
Expand Down
73 changes: 73 additions & 0 deletions apply-templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash
set -Eeuo pipefail

[ -f versions.json ] # run "versions.sh" first

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

jqt='.jq-template.awk'
if [ -n "${BASHBREW_SCRIPTS:-}" ]; then
jqt="$BASHBREW_SCRIPTS/jq-template.awk"
elif [ "$BASH_SOURCE" -nt "$jqt" ]; then
# https://github.com/docker-library/bashbrew/blob/master/scripts/jq-template.awk
wget -qO "$jqt" 'https://github.com/docker-library/bashbrew/raw/9f6a35772ac863a0241f147c820354e4008edf38/scripts/jq-template.awk'
fi

if [ "$#" -eq 0 ]; then
versions="$(jq -r 'keys | map(@sh) | join(" ")' versions.json)"
eval "set -- $versions"
fi

generated_warning() {
cat <<-EOH
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
EOH
}

for version; do
export version

if [ -d "$version" ]; then
rm -rf "$version"
fi

if jq -e '.[env.version] | not' versions.json > /dev/null; then
echo "skipping $version ..."
continue
fi

variants="$(jq -r '.[env.version].variants | map(@sh) | join(" ")' versions.json)"
eval "variants=( $variants )"

for variant in "${variants[@]}"; do
export variant

echo "processing $version/$variant ..."

dir="$version${variant:+/$variant}"

mkdir -p "$dir"

cp -f docker-entrypoint.sh "$dir/"

case "$variant" in
alpine*)
template='Dockerfile-alpine.template'
sed -i -e 's/gosu/su-exec/g' "$dir/docker-entrypoint.sh"
;;
*)
template='Dockerfile.template'
;;
esac

{
generated_warning
gawk -f "$jqt" "$template"
} > "$dir/Dockerfile"
done
done
85 changes: 43 additions & 42 deletions generate-stackbrew-library.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
set -eu

declare -A aliases=(
Expand All @@ -9,11 +9,13 @@ declare -A aliases=(
self="$(basename "$BASH_SOURCE")"
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"

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

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

# get the most recent commit which modified any of "$@"
fileCommit() {
Expand Down Expand Up @@ -68,51 +70,50 @@ join() {
echo "${out#$sep}"
}

for version in "${versions[@]}"; do
for v in \
'' alpine \
; do
dir="$version${v:+/$v}"
variant="$(basename "$v")"
for version; do
export version

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

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

fullVersion="$(git show "$commit":"$dir/Dockerfile" | awk '$1 == "ENV" && $2 == "REDIS_VERSION" { print $3; exit }')"

versionAliases=()
while [ "$fullVersion" != "$version" -a "${fullVersion%[.]*}" != "$fullVersion" ]; do
versionAliases+=( $fullVersion )
fullVersion="${fullVersion%[.]*}"
done
versionAliases+=(
$version
${aliases[$version]:-}
)

if [ -n "$variant" ]; then
variantAliases=( "${versionAliases[@]/%/-$variant}" )
variantAliases=( "${variantAliases[@]//latest-/}" )
else
variantAliases=( "${versionAliases[@]}" )
fi
fullVersion="$(jq -r '.[env.version].version' versions.json)"

variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")"
versionAliases=()
while [ "$fullVersion" != "$version" -a "${fullVersion%[.]*}" != "$fullVersion" ]; do
versionAliases+=( $fullVersion )
fullVersion="${fullVersion%[.]*}"
done
versionAliases+=(
$version
${aliases[$version]:-}
)

suite="${variantParent#*:}" # "jessie-slim", "stretch"
suite="${suite%-slim}" # "jessie", "stretch"

if [ "$v" = 'alpine' ]; then
suite="alpine$suite" # "alpine3.8"
suiteAliases=( "${versionAliases[@]/%/-$suite}" )
else
suiteAliases=( "${variantAliases[@]/%/-$suite}" )
fi
suiteAliases=( "${suiteAliases[@]//latest-/}" )
variantAliases+=( "${suiteAliases[@]}" )
for variant in "${variants[@]}"; do
dir="$version/$variant"
commit="$(dirCommit "$dir")"

variantParent="$(awk 'toupper($1) == "FROM" { print $2 }' "$dir/Dockerfile")"
variantArches="${parentRepoToArches[$variantParent]}"

variantAliases=( "${versionAliases[@]/%/-$variant}" )
variantAliases=( "${variantAliases[@]//latest-/}" )

case "$variant" in
"$debian")
variantAliases=(
"${versionAliases[@]}"
"${variantAliases[@]}"
)
;;
alpine"$alpine")
variantAliases+=( "${versionAliases[@]/%/-alpine}" )
variantAliases=( "${variantAliases[@]//latest-/}" )
;;
esac

echo
cat <<-EOE
Tags: $(join ', ' "${variantAliases[@]}")
Expand Down
Loading

0 comments on commit c01ba51

Please sign in to comment.