Skip to content

Commit

Permalink
Introduce slim version of container image
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsihk authored Feb 13, 2024
1 parent 5d558f1 commit c1c2429
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 79 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ jobs:
VERSION=${GITHUB_REF#refs/*/}
TAGS="${{ env.IMAGE_NAME }}:${VERSION}"
TAGS_SLIM="${{ env.IMAGE_NAME }}:${VERSION}-slim"
if [[ $GITHUB_REF == refs/tags/* ]]; then
TAGS="$TAGS,${{ env.IMAGE_NAME }}:latest"
TAGS_SLIM="$TAGS_SLIM,${{ env.IMAGE_NAME }}:latest-slim"
fi
for TAG in $(echo $TAGS | sed 's/,/ /g'); do
TAGS="$TAGS,quay.io/$TAG"
TAGS_SLIM="$TAGS_SLIM,quay.io/$TAG-slim"
done
echo "platforms=${DOCKER_PLATFORMS}" >> $GITHUB_OUTPUT
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
echo "tags_slim=${TAGS_SLIM}" >> $GITHUB_OUTPUT
- name: Set up QEMU
id: qemu
Expand Down Expand Up @@ -121,3 +125,15 @@ jobs:
tags: ${{ steps.prepare.outputs.tags }}
platforms: ${{ steps.prepare.outputs.platforms }}
provenance: false

- name: Build and push (slim version)
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prepare.outputs.tags_slim }}
platforms: ${{ steps.prepare.outputs.platforms }}
provenance: false
build-args: |
ARG_ENABLE_GIT_CLONE='false'
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ ARG BASH_VERSION="=5.2.21-r0"
ARG ARG_WEB_PATH='/var/www/html'
ENV WEB_PATH=${ARG_WEB_PATH}

# controls whether the remaining steps should use git clone or simply download from git repo
ARG ARG_ENABLE_GIT_CLONE='true'
ENV ENABLE_GIT_CLONE=${ARG_ENABLE_GIT_CLONE}

USER root
COPY --chown=nobody rootfs/ /

Expand All @@ -24,8 +28,8 @@ COPY --chown=nobody rootfs/ /
RUN apk add --no-cache \
dcron${DCRON_VERSION} \
libcap${LIBCAP_VERSION} \
git${GIT_VERSION} \
bash${BASH_VERSION} \
&& if [ "${ENABLE_GIT_CLONE}" = 'true' ]; then apk add --no-cache git${GIT_VERSION}; fi \
&& chown nobody:nobody /usr/sbin/crond \
&& setcap cap_setgid=ep /usr/sbin/crond \
# Clean up unused files from base image
Expand Down Expand Up @@ -62,7 +66,7 @@ ENV MOODLE_GIT_URL=${ARG_MOODLE_GIT_URL} \
SMTP_HOST=smtp.gmail.com \
SMTP_PORT=587 \
[email protected] \
SMTP_PASSWORD=your_password  \
SMTP_PASSWORD=your_password \
SMTP_PROTOCOL=tls \
MOODLE_MAIL_NOREPLY_ADDRESS=noreply@localhost \
MOODLE_MAIL_PREFIX=[moodle] \
Expand Down
101 changes: 55 additions & 46 deletions README.md

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions rootfs/docker-entrypoint-init.d/02-configure-moodle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ if [ -z "$AUTO_UPDATE_MOODLE" ] || [ "$AUTO_UPDATE_MOODLE" = true ]; then
fi
echo "Upgrading moodle..."
php -d max_input_vars=10000 "${WEB_PATH}"/admin/cli/maintenance.php --enable
if [ -z "$UPDATE_MOODLE_CODE" ] || [ "$UPDATE_MOODLE_CODE" = true ]; then
echo "Checking moodle code version..."
git -C "${WEB_PATH}" fetch origin "$MODOLE_GIT_BRANCH" --depth=1 && git -C "${WEB_PATH}" checkout FETCH_HEAD -B "$MODOLE_GIT_BRANCH"
if [ "${ENABLE_GIT_CLONE}" = 'true' ]; then
if [ -z "$UPDATE_MOODLE_CODE" ] || [ "$UPDATE_MOODLE_CODE" = true ]; then
echo "Checking moodle code version..."
git -C "${WEB_PATH}" fetch origin "$MODOLE_GIT_BRANCH" --depth=1 && git -C "${WEB_PATH}" checkout FETCH_HEAD -B "$MODOLE_GIT_BRANCH"
fi
fi
php -d max_input_vars=10000 "${WEB_PATH}"/admin/cli/upgrade.php --non-interactive --allow-unstable
if [ $START_IN_MAINT_MODE = false ]; then
Expand Down
26 changes: 16 additions & 10 deletions rootfs/usr/libexec/check-git-commit
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ if [ -n "$1" ] || [ -n "$2" ]
then
if [ -d "$1" ]
then
CODE_PATH="$1"
CURRENT_COMMIT=$(git -C "$CODE_PATH" rev-parse HEAD)
TARGET_COMMIT="$2"

echo "Downloaded Git commit at $CODE_PATH: $CURRENT_COMMIT"

if [ "$CURRENT_COMMIT" != "$TARGET_COMMIT" ]
if [ "${ENABLE_GIT_CLONE}" = 'true' ]
then
echo "Expecting Git commit: $TARGET_COMMIT, mismatch"
exit 1
CODE_PATH="$1"
CURRENT_COMMIT=$(git -C "$CODE_PATH" rev-parse HEAD)
TARGET_COMMIT="$2"

echo "Downloaded Git commit at $CODE_PATH: $CURRENT_COMMIT"

if [ "$CURRENT_COMMIT" != "$TARGET_COMMIT" ]
then
echo "Expecting Git commit: $TARGET_COMMIT, mismatch"
exit 1
else
echo "Git commit matched"
exit 0
fi
else
echo "Git commit matched"
echo "Skipped checking git commit, git clone is not enabled"
exit 0
fi
else
Expand Down
47 changes: 30 additions & 17 deletions rootfs/usr/libexec/clone-from-git
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Clone Git Repository with Minimal Commit History
# Description: The script will take the inputted Git repo to clone the repo with least commit history
# Parameters: $1: Git repo URL, e.g. https://github.com/catalyst/moodle-cachestore_redissentinel.gi
# Parameters: $1: Git repo URL, e.g. https://github.com/catalyst/moodle-cachestore_redissentinel.git
# $2: Git branch, e.g. master
# $3: Git commit hash, e.g. b495e8f36a81fd1a2a414e34a978da879c473f31
# $4: Moodle plugin absolute path, e.g. /var/www/html/cache/stores/redissentinel/
Expand All @@ -14,24 +14,37 @@ then
INPUT_GIT_BRANCH="$2"
INPUT_GIT_COMMIT="$3"
DOWNLOAD_PATH="$4"
echo "Clone from git repo: ${INPUT_GIT_URL}..."
git clone "${INPUT_GIT_URL}" --branch "${INPUT_GIT_BRANCH}" --single-branch --depth 1 "${DOWNLOAD_PATH}"/
# Make sure the current HEAD is the defined commit
for FETCH_DEPTH in 1 5 10 30 50 100 200
do
if [ $FETCH_DEPTH -gt 1 ]

if [ "${ENABLE_GIT_CLONE}" = 'true' ]
then
echo "Clone from git repo: ${INPUT_GIT_URL} to ${DOWNLOAD_PATH}..."
git clone "${INPUT_GIT_URL}" --branch "${INPUT_GIT_BRANCH}" --single-branch --depth 1 "${DOWNLOAD_PATH}"/
# Make sure the current HEAD is the defined commit
for FETCH_DEPTH in 1 5 10 30 50 100 200
do
if [ $FETCH_DEPTH -gt 1 ]
then
echo "Attempting to fetch last $FETCH_DEPTH commits..."
git -C "${DOWNLOAD_PATH}" fetch origin "${INPUT_GIT_BRANCH}" --depth "${FETCH_DEPTH}"
fi
git -C "${DOWNLOAD_PATH}" reset --hard "${INPUT_GIT_COMMIT}"
RESULT=$?
if [ $RESULT -eq 0 ]
then
break;
fi
done
git -C "${DOWNLOAD_PATH}" clean -dfx
else
if [ ! -d "${DOWNLOAD_PATH}" ]
then
echo "Attempting to fetch last $FETCH_DEPTH commits..."
git -C "${DOWNLOAD_PATH}" fetch origin "${INPUT_GIT_BRANCH}" --depth "${FETCH_DEPTH}"
mkdir -p "${DOWNLOAD_PATH}"
fi
git -C "${DOWNLOAD_PATH}" reset --hard "${INPUT_GIT_COMMIT}"
RESULT=$?
if [ $RESULT -eq 0 ]
then
break;
fi
done
git -C "${DOWNLOAD_PATH}" clean -dfx
# e.g. https://github.com/catalyst/moodle-cachestore_redissentinel/archive/b495e8f36a81fd1a2a414e34a978da879c473f31.tar.gz
DOWNLOAD_URL=$(echo "${INPUT_GIT_URL}" | sed 's/\.git$//')'/archive/'"${INPUT_GIT_COMMIT}"'.tar.gz'
echo "Download from git repo: ${DOWNLOAD_URL} to ${DOWNLOAD_PATH}..."
curl --location "${DOWNLOAD_URL}" | tar xz --strip-components=1 -C "${DOWNLOAD_PATH}"/
fi
else
echo "Missing argument: <repo> <branch> <commit_hash> <local_path>"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion rootfs/usr/libexec/moodle/install-git-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Install Moodle plugin from Git
# Description: The script will take the inputted Git repo to install
# to Moodle core
# Parameters: $1: Git repo URL, e.g. https://github.com/catalyst/moodle-cachestore_redissentinel.gi
# Parameters: $1: Git repo URL, e.g. https://github.com/catalyst/moodle-cachestore_redissentinel.git
# $2: Git branch, e.g. master
# $3: Git commit hash, e.g. b495e8f36a81fd1a2a414e34a978da879c473f31
# $4: Moodle plugin absolute path, e.g. /var/www/html/cache/stores/redissentinel/
Expand Down

0 comments on commit c1c2429

Please sign in to comment.