Skip to content

Commit

Permalink
build: Add Dockerfile and wrapper script for dev environment
Browse files Browse the repository at this point in the history
- Add Dockerfile
- Add runDevEnvironment.sh for building Docker image and executing build in the container
- Allow to provide --no-build and --no-run options to the wrapper script
- Enable caching of gradle dependencies in the host
- Auto cleanup dangling images

Resolves #4, #5, #9
  • Loading branch information
turboBasic committed Dec 30, 2023
1 parent 523a981 commit b96feee
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# syntax=docker/dockerfile:1

# hadolint global ignore=DL3006
ARG IMAGE_VERSION=python:3.12.1-alpine3.19
FROM ${IMAGE_VERSION}

ARG GRADLE_VERSION=8.5
RUN apk add --no-cache \
gradle=~$GRADLE_VERSION \
openjdk11-jdk \
shadow

# Add user with the same UID/GID as the user who invokes the build
ARG USER_NAME=appuser
ARG UID=1000
ARG GID=1000
RUN groupadd --non-unique --gid $GID $USER_NAME && \
useradd --non-unique --gid $GID --uid $UID --create-home --shell /bin/sh $USER_NAME
USER $USER_NAME

WORKDIR /app

CMD ["./gradlew", "--info", "--no-daemon", "clean", "test"]

# Metadata
ARG BUILD_DATE
ARG IMAGE_NAME
ARG IMAGE_PATCH_VER=0
ARG VCS_REF=none
LABEL \
org.label-schema.build-date="$BUILD_DATE" \
org.label-schema.docker.cmd="docker run --rm -it -v \"\$PWD\":/app -v \$HOME/.gradle:/home/$USER_NAME/.gradle turboBasic/jenkins-library" \
org.label-schema.description="Build image, Gradle $GRADLE_VERSION" \
org.label-schema.name="$IMAGE_NAME" \
org.label-schema.schema-version="1.0" \
org.label-schema.url="https://github.com/turboBasic/JenkinsLibrary/blob/main/README.md" \
org.label-schema.vcs-ref="$VCS_REF" \
org.label-schema.vcs-url="https://github.com/turboBasic/JenkinsLibrary" \
org.label-schema.vendor="turboBasic" \
org.label-schema.version="$IMAGE_PATCH_VER"
64 changes: 64 additions & 0 deletions runDevEnvironment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/sh

main() {
cd "$(dirname "$0")" || exit
setDefaults

END_OF_OPTIONS=
while [ -z "$END_OF_OPTIONS" ] && [ -n "$1" ]; do
case "$1" in
--no-build)
NO_BUILD=true
shift;;
--no-run)
NO_RUN=true
shift;;
*)
END_OF_OPTIONS=true
esac
done

if [ -z "$NO_BUILD" ]; then
docker build --rm --tag "$IMAGE_NAME":latest \
--build-arg UID="$(id -u)" --build-arg GID="$(id -g)" \
--build-arg VCS_REF="$VCS_REF" \
--build-arg IMAGE_NAME="$IMAGE_NAME" \
--build-arg BUILD_DATE="$(date -u "+%Y-%m-%d %H:%M")" \
.
docker image prune --force --filter "label=org.label-schema.name=$IMAGE_NAME"
fi

if [ -z "$NO_RUN" ]; then
# shellcheck disable=SC2086
docker run --rm $DOCKER_OPTIONS \
--volume "$PWD:/app" \
--env GRADLE_USER_HOME=/app/.gradle \
"$IMAGE_NAME" "$@"
if [ "$CI" != true ] && [ $# = 0 ]; then
echo "Tests execution report: file://$PWD/build/reports/tests/test/index.html"
fi
fi
}

setDefaults() {
IMAGE_NAME=turbobasic/jenkins-library
NO_BUILD=
NO_RUN=
DOCKER_OPTIONS=

VCS_REF="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)/$(git rev-parse --short HEAD 2>/dev/null)"
if [ "$VCS_REF" = "/" ]; then
VCS_REF="not-in-version-control"
fi
if [ "$CI" != true ]; then
DOCKER_OPTIONS="--interactive --tty"
fi
true
}


# Start body of script
set -o errexit
[ "$CI" = "true" ] && set -o xtrace

main "$@"

0 comments on commit b96feee

Please sign in to comment.