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

build: refactor image regeneration #2483

Merged
merged 6 commits into from
Jan 29, 2025
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
23 changes: 3 additions & 20 deletions .github/Dockerfile.regenerate
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
ARG BASE=ghcr.io/sbb-design-systems/sbb-angular/showcase:latest
FROM $BASE as base

# For version 11 + 12
FROM nginxinc/nginx-unprivileged:stable as initless

# Copy config from base image
COPY --from=base /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
# Copy showcase from base image
COPY --from=base /usr/share/nginx/html /usr/share/nginx/html

# For versions 13+
FROM nginxinc/nginx-unprivileged:stable as init
FROM nginxinc/nginx-unprivileged:stable

# Copy config from base image
COPY --from=base /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf
# Copy showcase from base image
COPY --from=base /usr/share/nginx/html /usr/share/nginx/html
# Copy ngssc from base image if it exists
COPY --from=base /usr/sbin/ngssc /usr/sbin/ngssc
# Copy ngssc.sh from base image if it exists
COPY --from=base /docker-entrypoint.d/ngssc.sh /docker-entrypoint.d/ngssc.sh
COPY default.conf /etc/nginx/conf.d/default.conf
COPY html /usr/share/nginx/html
11 changes: 11 additions & 0 deletions .github/Dockerfile.regenerate.ngssc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# For versions 13+
FROM nginxinc/nginx-unprivileged:stable

COPY default.conf /etc/nginx/conf.d/default.conf
COPY html /usr/share/nginx/html
USER root
COPY ngssc /usr/sbin/ngssc
RUN chmod +x /usr/sbin/ngssc
COPY ngssc.sh /docker-entrypoint.d/ngssc.sh
RUN chmod +x /docker-entrypoint.d/ngssc.sh
USER $UID
6 changes: 2 additions & 4 deletions .github/actions/setup-mint/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ runs:
steps:
- name: Setup mint
run: |
mkdir -p /tmp/mint
cd /tmp/mint
mkdir -p "$HOME/.local/bin"
cd "$HOME/.local/bin"

# Get the current released tag_name
VER=$(curl -sL https://api.github.com/repos/mintoolkit/mint/releases \
| grep tag_name | head -n1 | cut -d'"' -f4)
curl -L -o mint.tar.gz "https://github.com/mintoolkit/mint/releases/download/${VER}/dist_linux.tar.gz"
tar -xvf mint.tar.gz --strip-components 1

echo "/tmp/mint" >> $GITHUB_PATH

echo "Installed mint:${VER}"
shell: bash
59 changes: 23 additions & 36 deletions .github/workflows/regenerate-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,39 @@ on:
permissions:
packages: write

env:
IMAGE_REPO: ghcr.io/${{ github.repository }}/showcase


jobs:
preview-image:
tag-resolver:
runs-on: ubuntu-latest
env:
START_VERSION: 11
IMAGE_REPO: ghcr.io/${{ github.repository }}/showcase
outputs:
versions: ${{ steps.versions.outputs.versions }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 0
fetch-tags: true
- uses: ./.github/actions/setup-mint
- name: Regenerate latest container images to keep them up to date
- name: Find latest tags from major versions
id: versions
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
majorVersion=$START_VERSION
echo "Starting image regeneration with version $majorVersion"
./scripts/find-major-versions.sh >> "$GITHUB_OUTPUT"

while : ; do
version=$(git tag --sort=v:refname -l "$majorVersion.*.*" | tail -1)
[[ "$version" != "" ]] || break

image="$IMAGE_REPO:$version"
echo ""
echo "Regenerating $image"
echo ""

(( $majorVersion < 13 )) && target=initless || target=init
docker build \
--tag tmp-fat \
--build-arg BASE="$image" \
--file .github/Dockerfile.regenerate \
--target $target \
.
mint slim \
--target tmp-fat \
--tag tmp \
--preserve-path /usr/share/nginx/html
docker tag tmp "$image"
docker push "$image"

echo ""
echo "Finished regenerating $image"
echo ""

majorVersion=$((majorVersion+1))
done
regenerate-image:
runs-on: ubuntu-latest
needs: tag-resolver
strategy:
matrix:
version: ${{ fromJSON(needs.tag-resolver.outputs.versions) }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- uses: ./.github/actions/setup-mint
- run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin
./scripts/regenerate-image.sh
env:
DOCKER_BUILDKIT: 1
VERSION: ${{ matrix.version }}
19 changes: 19 additions & 0 deletions scripts/find-major-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set -e

if [ -z "${START_VERSION}" ]; then
echo "Missing START_VERSION variable"
exit 1
fi

majorVersion=$START_VERSION
versions=()

while : ; do
version=$(git tag --sort=v:refname -l "$majorVersion.*.*" | tail -1)
[[ "$version" != "" ]] || break
versions+=("$version")
majorVersion=$((majorVersion+1))
done

printf -v joined '"%s", ' "${versions[@]}"
echo "versions=[${joined::-2}]"
47 changes: 47 additions & 0 deletions scripts/regenerate-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
set -e

if [ -z "${IMAGE_REPO}" ]; then
echo "Missing IMAGE_REPO variable"
exit 1
elif [ -z "${VERSION}" ]; then
echo "Missing VERSION variable"
exit 1
fi

image="$IMAGE_REPO:$VERSION"
majorVersion="${VERSION%.*.*}"
target=""
echo ""
echo "Regenerating $image"
echo ""

mkdir -p "$PWD/dist"

docker create --name dummy $image
if [[ $majorVersion > 12 ]]; then
target=".ngssc"
docker cp dummy:/usr/sbin/ngssc "$PWD/dist/ngssc"
docker cp dummy:/docker-entrypoint.d/ngssc.sh "$PWD/dist/ngssc.sh"
fi

docker cp dummy:/etc/nginx/conf.d/default.conf "$PWD/dist/default.conf"
docker cp dummy:/usr/share/nginx/html "$PWD/dist/html/"

docker rm -f dummy

docker build \
--tag tmp-fat \
--file ".github/Dockerfile.regenerate$target" \
"$PWD/dist"

mint slim \
--target tmp-fat \
--tag tmp \
--preserve-path /usr/share/nginx/html

docker tag tmp "$image"
docker push "$image"

echo ""
echo "Finished regenerating $image"
echo ""
Loading