Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added templating #302

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*/**/Dockerfile linguist-generated
/*/**/docker-entrypoint.sh linguist-generated
/Dockerfile*.template linguist-language=Dockerfile
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 5.0/alpine/Dockerfile → 5.0/alpine3.18/Dockerfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
6 changes: 6 additions & 0 deletions 5.0/Dockerfile → 5.0/bookworm/Dockerfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 ruby:%%RUBY_VERSION%%-alpine3.18
FROM ruby:{{ .ruby.version }}-{{ env.variant }}

# explicitly set uid/gid to guarantee that it won't change in the future
# the values 999:999 are identical to the current user/group id assigned
Expand Down Expand Up @@ -40,9 +40,9 @@ RUN set -eux; \
chown redmine:redmine "$HOME"; \
chmod 1777 "$HOME"

ENV REDMINE_VERSION %%REDMINE_VERSION%%
ENV REDMINE_DOWNLOAD_URL %%REDMINE_DOWNLOAD_URL%%
ENV REDMINE_DOWNLOAD_SHA256 %%REDMINE_DOWNLOAD_SHA256%%
ENV REDMINE_VERSION {{ .version }}
ENV REDMINE_DOWNLOAD_URL {{ .downloadUrl }}
ENV REDMINE_DOWNLOAD_SHA256 {{ .sha256 }}

RUN set -eux; \
wget -O redmine.tar.gz "$REDMINE_DOWNLOAD_URL"; \
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile-debian.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ruby:%%RUBY_VERSION%%-slim-bookworm
FROM ruby:{{ .ruby.version }}-slim-{{ env.variant }}

# explicitly set uid/gid to guarantee that it won't change in the future
# the values 999:999 are identical to the current user/group id assigned
Expand Down Expand Up @@ -43,9 +43,9 @@ RUN set -eux; \
chown redmine:redmine "$HOME"; \
chmod 1777 "$HOME"

ENV REDMINE_VERSION %%REDMINE_VERSION%%
ENV REDMINE_DOWNLOAD_URL %%REDMINE_DOWNLOAD_URL%%
ENV REDMINE_DOWNLOAD_SHA256 %%REDMINE_DOWNLOAD_SHA256%%
ENV REDMINE_VERSION {{ .version }}
ENV REDMINE_DOWNLOAD_URL {{ .downloadUrl }}
ENV REDMINE_DOWNLOAD_SHA256 {{ .sha256 }}

RUN set -eux; \
# if we use wget here, we get certificate issues (https://github.com/docker-library/redmine/pull/249#issuecomment-984176479)
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-debian.template'
;;
esac

{
generated_warning
gawk -f "$jqt" "$template"
} > "$dir/Dockerfile"
done
done
78 changes: 34 additions & 44 deletions generate-stackbrew-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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,61 +70,49 @@ join() {
echo "${out#$sep}"
}

for version in "${versions[@]}"; do
# normally this would be down in the other loop, but "passenger" doesn't have it, so this is the simplest option (we just can't ever have "alpine" be out of sync, so we should remove it instead if it ever needs to be out of sync)
commit="$(dirCommit "$version")"
fullVersion="$(git show "$commit":"$version/Dockerfile" | awk '$1 == "ENV" && $2 == "REDMINE_VERSION" { print $3; exit }')"
for version; do
export version

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

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

fullVersion="$(jq -r '.[env.version].version' versions.json)"

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

for variant in '' passenger alpine; do
dir="$version${variant:+/$variant}"
[ -f "$dir/Dockerfile" ] || continue

for variant in "${variants[@]}"; do
dir="$version/$variant"
commit="$(dirCommit "$dir")"

if [ -n "$variant" ]; then
variantAliases=( "${versionAliases[@]/%/-$variant}" )
variantAliases=( "${variantAliases[@]//latest-/}" )
else
variantAliases=( "${versionAliases[@]}" )
fi

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

suite="${variantParent#*:}" # "ruby:2.7-slim-bullseye", "2.7-alpine3.15"
suite="${suite##*-}" # "bullseye", "alpine3.15"
suite="${suite#alpine}" # "bullseye", "3.15"
variantAliases=( "${versionAliases[@]/%/-$variant}" )
variantAliases=( "${variantAliases[@]//latest-/}" )

case "$variant" in
alpine)
suite="alpine$suite" # "alpine3.8"
suiteAliases=( "${versionAliases[@]/%/-$suite}" )
"$debian")
variantAliases=(
"${versionAliases[@]}"
"${variantAliases[@]}"
)
;;
passenger)
# the "passenger" variant doesn't get any extra aliases (sorry)
suiteAliases=()
;;
*)
suiteAliases=( "${variantAliases[@]/%/-$suite}" )
alpine"$alpine")
variantAliases+=( "${versionAliases[@]/%/-alpine}" )
variantAliases=( "${variantAliases[@]//latest-/}" )
;;
esac
suiteAliases=( "${suiteAliases[@]//latest-/}" )
variantAliases+=( "${suiteAliases[@]}" )

case "$variant" in
passenger) variantArches='amd64' ;; # https://github.com/docker-library/redmine/pull/87#issuecomment-323877678
*) variantArches="${parentRepoToArches[$variantParent]}" ;;
esac

if [ "$variant" != 'alpine' ]; then
# the "gosu" Debian package isn't available on mips64le
variantArches="$(sed <<<" $variantArches " -e 's/ mips64le / /g')"
fi

echo
cat <<-EOE
Expand Down
68 changes: 2 additions & 66 deletions update.sh
Original file line number Diff line number Diff line change
@@ -1,71 +1,7 @@
#!/usr/bin/env bash
set -Eeuo pipefail

# see https://www.redmine.org/projects/redmine/wiki/redmineinstall
defaultRubyVersion='3.1'
declare -A rubyVersions=(
)

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

versions=( "$@" )
if [ ${#versions[@]} -eq 0 ]; then
versions=( */ )
fi
versions=( "${versions[@]%/}" )

# https://github.com/docker-library/redmine/issues/256
downloadsPage="$(curl -fsSL 'https://redmine.org/projects/redmine/wiki/Download')"

releasesUrl='https://www.redmine.org/releases'
versionsPage="$(curl -fsSL "$releasesUrl")"

allVersions="$(
sed <<<"$versionsPage"$'\n'"$downloadsPage" \
-rne 's/.*redmine-([0-9.]+)[.]tar[.]gz.*/\1/p' \
| sort -ruV
)"

for version in "${versions[@]}"; do
ourVersions="$(grep -E "^$version[.]" <<<"$allVersions")"
fullVersion=
for tryVersion in $ourVersions; do
url="$releasesUrl/redmine-$tryVersion.tar.gz"
if sha256="$(curl -fsSL "$url.sha256" 2>/dev/null)" && sha256="$(cut -d' ' -f1 <<<"$sha256")" && [ -n "$sha256" ]; then
fullVersion="$tryVersion"
break
fi
if urlLine="$(grep -oEm1 'href="https?://[^"]+/'"redmine-$tryVersion.tar.gz"'".*sha256:.*' <<<"$downloadsPage")" && url="$(cut -d'"' -f2 <<<"$urlLine")" && [ -n "$url" ] && sha256="$(grep -oEm1 'sha256:[[:space:]]*[a-f0-9]{64}' <<<"$urlLine")" && [ -n "$sha256" ] && sha256="${sha256: -64}"; then
fullVersion="$tryVersion"
break
fi
done
if [ -z "$fullVersion" ]; then
echo >&2 "error: failed to find full version for '$version'"
exit 1
fi

rubyVersion="${rubyVersions[$version]:-$defaultRubyVersion}"

text="ruby $rubyVersion"

echo "$version: $fullVersion ($text)"

commonSedArgs=(
-r
-e 's/%%REDMINE_VERSION%%/'"$fullVersion"'/'
-e 's/%%RUBY_VERSION%%/'"$rubyVersion"'/'
-e 's!%%REDMINE_DOWNLOAD_URL%%!'"$url"'!'
-e 's/%%REDMINE_DOWNLOAD_SHA256%%/'"$sha256"'/'
-e 's/%%REDMINE%%/redmine:'"$version"'/'
)

mkdir -p "$version"
cp docker-entrypoint.sh "$version/"
sed "${commonSedArgs[@]}" Dockerfile-debian.template > "$version/Dockerfile"

mkdir -p "$version/alpine"
cp docker-entrypoint.sh "$version/alpine/"
sed -i -e 's/gosu/su-exec/g' "$version/alpine/docker-entrypoint.sh"
sed "${commonSedArgs[@]}" Dockerfile-alpine.template > "$version/alpine/Dockerfile"
done
./versions.sh "$@"
./apply-templates.sh "$@"
16 changes: 16 additions & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"5.0": {
"alpine": "3.18",
"debian": "bookworm",
"downloadUrl": "https://www.redmine.org/releases/redmine-5.0.6.tar.gz",
"ruby": {
"version": "3.1"
},
"sha256": "488fe08f37a8eb1011415922a8ea743b7f38d8a7a5f8822950a34a375dcf08ee",
"variants": [
"bookworm",
"alpine3.18"
],
"version": "5.0.6"
}
}
Loading