From 3075a62a7931c799d0e7dfdbb36ffb4a2728e15e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rene=20Hamp=C3=B6lz?= <hampoelz@student.tugraz.at>
Date: Wed, 13 Nov 2024 00:24:59 +0100
Subject: [PATCH] Add flag for build container
---
build.Dockerfile | 11 ++--------
build.sh | 55 ++++++++++++++++++++++++++++++++----------------
2 files changed, 39 insertions(+), 27 deletions(-)
diff --git a/build.Dockerfile b/build.Dockerfile
index 0d7d8e6..39f8fb4 100644
--- a/build.Dockerfile
+++ b/build.Dockerfile
@@ -1,13 +1,6 @@
-# Build the containers with:
-# docker run --privileged --rm -v "$(pwd)":/workspace -v /var/run/docker.sock:/var/run/docker.sock -w /workspace container_build_container ./build.sh <some_tag> --latest --push --login --no-cache
-
-FROM docker:24.0
+FROM docker:27
RUN apk update && \
- apk add --no-cache jq curl git bash nodejs npm
-
-RUN docker buildx version
+ apk add --no-cache bash curl jq nodejs npm
WORKDIR /workspace
-
-CMD ["/bin/bash"]
\ No newline at end of file
diff --git a/build.sh b/build.sh
index 41e36dd..6a259fc 100755
--- a/build.sh
+++ b/build.sh
@@ -9,6 +9,8 @@ REGISTRY=registry.gitlab.tugraz.at/oop1ws/container
NO_CACHE=false
LOGIN=false
+BUILD_WITH_CONTAINER=false
+
print_usage() {
echo "
$SCRIPT v$VERSION
@@ -26,18 +28,20 @@ print_usage() {
yourself with your GitLab username and a personal access token
using the 'docker login registry.gitlab.tugraz.at' command.
- -h, --help This help text
- --latest Tag the containers additionally with the 'latest' tag
- --push Push the build result to the registry
- --registry Use a custom container registry
- --no-cache Do not use cache when building the images
- --login Use the environment variables 'DOCKER_USERNAME'
- and 'DOCKER_PASSWORD' to automatically login to the
- registry
+ -h, --help This help text
+ --latest Tag the containers additionally with the 'latest' tag
+ --push Push the build result to the registry
+ --registry Use a custom container registry
+ --no-cache Do not use cache when building the images
+ --login Use the environment variables 'DOCKER_USERNAME' and
+ 'DOCKER_PASSWORD' to automatically login to the registry
+ --use-container Perform the building steps within a dedicated container
+ that already has all required build dependencies installed
"
}
ARGS=()
+ALL_ARGS="$@"
while [[ $# -gt 0 ]]; do
case $1 in
@@ -61,6 +65,9 @@ while [[ $# -gt 0 ]]; do
--login)
LOGIN=true
;;
+ --use-container)
+ BUILD_WITH_CONTAINER=true
+ ;;
-*|--*)
echo "ERROR: Unknown option $1"
exit 1
@@ -92,26 +99,38 @@ check_command() {
}
check_command "docker" "Docker"
-check_command "npx" "Node.js"
-check_command "jq" "jq"
-docker buildx version &>/dev/null
-if [ $? -ne 0 ]; then
- echo "ERROR: The Docker CLI plugin 'buildx' is not installed"
- exit 1
+if $BUILD_WITH_CONTAINER; then
+ WORKSPACE=$(dirname "$0")
+ BUILD_IMAGE="$REGISTRY/builder"
+ BUILD_ARGS=${ALL_ARGS//"--use-container "/}
+
+ docker build --tag "$BUILD_IMAGE" -f build.Dockerfile .
+ docker run --rm -e DOCKER_USERNAME -e DOCKER_PASSWORD -v /var/run/docker.sock:/var/run/docker.sock:ro -v "$WORKSPACE":/workspace "$REGISTRY/builder" ./build.sh $BUILD_ARGS
+ exit $?
+else
+ check_command "npx" "Node.js"
+ check_command "jq" "jq"
+
+ docker buildx version &>/dev/null
+ if [ $? -ne 0 ]; then
+ echo "ERROR: The Docker CLI plugin 'buildx' is not installed"
+ exit 1
+ fi
fi
+
# =============================
# prepare environment
# =============================
if $LOGIN; then
- if [ -v $DOCKER_PASSWORD ]; then
- echo "ERROR: Variable 'DOCKER_PASSWORD' not set"
- exit 1
- elif [ -v $DOCKER_USERNAME ]; then
+ if [ -z $DOCKER_USERNAME ]; then
echo "ERROR: Variable 'DOCKER_USERNAME' not set"
exit 1
+ elif [ -z $DOCKER_PASSWORD ]; then
+ echo "ERROR: Variable 'DOCKER_PASSWORD' not set"
+ exit 1
fi
echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin $REGISTRY
--
GitLab