% #!/bin/bash % # % # Full fledged script that can be used with parameters % # % # THIS IS A GENERATED SCRIPT. See the usage section below. % # % usage() { cat << USAGE Generate Gitblit Dockerfiles, either for release or snapshots Usage: generate_dockerfile.sh [flags] Options: -h, --help show help screen -v, --version the Gitblit version for which the Dockerfile is generated Required for releases -o, --output-file write to specified file (default: stdout) --sha the SHA-256 hash of the Gitblit release tarball Required for release versions --tarball the Gitblit snapshot tarball file Default is ${GITBLIT_FILE} --release generate a Dockerfile for a Gitblit release Default is to generate a Dockerfile for a snapshot version --ubuntu generate a Dockerfile for only a Ubuntu based image Default is to generate a combined Dockerfile which can be used to build Ubuntu and Alpine images --alpine generate a Dockerfile for only an Alpine based image Default is to generate a combined Dockerfile which can be used to build Ubuntu and Alpine images Environment variables: GITBLIT_VERSION GITBLIT_DOWNLOAD_SHA Example: Generate a Dockerfile to build snapshot images $ GITBLIT_VERSION=SNAPSHOT ./generate_dockerfile.sh > Dockerfile Generate the Dockerfile for the Alpine image, stored in this repository $ ./generate_dockerfile.sh -v 1.10.0 --sha 123...abc --release --alpine -o ../Dockerfile.alpine This script is generated from the Dockerfile.template with 'bash-tpl' Regenerate with: $ bash-tpl -- Dockerfile.template > generate_dockerfile.sh See: https://github.com/TekWizely/bash-tpl USAGE } parse_args() { while (($#)); do case "$1" in -h | --help) usage exit 0 ;; -o | --output-file) if [ -n "${2}" ]; then OUPUT_FILE="${2}" else echo "Error: Invalid or missing value for --output-file: '${2}'" >&2 exit 1 fi shift 2 ;; -v | --version) if [ -n "${2}" ]; then GITBLIT_VERSION="${2}" else echo "Error: Invalid or missing value for --version: '${2}'" >&2 exit 1 fi shift 2 ;; --sha) if [ -n "${2}" ]; then GITBLIT_DOWNLOAD_SHA="${2}" else echo "Error: Invalid or missing value for --sha: '${2}'" >&2 exit 1 fi shift 2 ;; --tarball) if [ -n "${2}" ]; then GITBLIT_FILE="${2}" else echo "Error: Invalid or missing value for --tarball: '${2}'" >&2 exit 1 fi shift 2 ;; --release) IMAGE_TYPE="release" shift ;; --alpine) DOCKERFILE_TYPE="alpine" shift ;; --ubuntu) DOCKERFILE_TYPE="ubuntu" shift ;; --* | -*) # unsupported flags echo "Error: unknown flag: '$1'; use -h for help" >&2 exit 1 ;; *) # unsupported positional arguments echo "Error: unknown argument: '$1'; use -h for help" >&2 exit 1 ;; esac done } % % # Some global defines % # % GITBLIT_VAR=/var/opt/gitblit : ${GITBLIT_FILE:=gitblit-*-SNAPSHOT.tar.gz} % % # % # Parse parameters and check for missing values % parse_args "$@" if [[ -z "$GITBLIT_VERSION" ]] ; then echo "ERROR: You need to define GITBLIT_VERSION" >&2 exit 1 fi if [[ -z "$GITBLIT_DOWNLOAD_SHA" && "$IMAGE_TYPE" == release ]] ; then echo "ERROR: You need to define GITBLIT_DOWNLOAD_SHA for a release Dockerfile" >&2 exit 1 fi % % # % # If an outfile was defined, reroute to file % if [[ -n "${OUPUT_FILE}" ]]; then exec > "${OUPUT_FILE}" fi % % # % # Here begins the generation of the actual Dockerfile % # % if [[ "$IMAGE_TYPE" == release && "$DOCKERFILE_TYPE" == alpine ]] ; then FROM openjdk:8-jre-alpine # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever packages get added RUN addgroup -S -g 8117 gitblit && adduser -S -H -G gitblit -u 8117 -h /opt/gitblit gitblit % elif [[ "$IMAGE_TYPE" == release && "$DOCKERFILE_TYPE" == ubuntu ]] ; then FROM openjdk:8-jre-slim # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever packages get added RUN groupadd -r -g 8117 gitblit && useradd -r -M -g gitblit -u 8117 -d /opt/gitblit gitblit % else FROM openjdk:8-jre-slim AS base # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever packages get added RUN groupadd -r -g 8117 gitblit && useradd -r -M -g gitblit -u 8117 -d /opt/gitblit gitblit % fi ENV GITBLIT_VERSION <% ${GITBLIT_VERSION} %> % if [[ "$IMAGE_TYPE" == release ]] ; then ENV GITBLIT_DOWNLOAD_SHA <% ${GITBLIT_DOWNLOAD_SHA} %> ENV GITBLIT_DOWNLOAD_URL https://github.com/gitblit-org/gitblit/releases/download/v${GITBLIT_VERSION}/gitblit-${GITBLIT_VERSION}.tar.gz % if [[ "$DOCKERFILE_TYPE" == alpine ]] ; then # Install su-exec to step down from root RUN set -eux; \ apk add --no-cache \ 'su-exec>=0.2' \ ; \ \ \ # Download and install Gitblit wget -nv -O gitblit.tar.gz ${GITBLIT_DOWNLOAD_URL} ; \ echo "${GITBLIT_DOWNLOAD_SHA} *gitblit.tar.gz" | sha256sum -c - ; \ mkdir -p /opt/gitblit ; \ tar xzf gitblit.tar.gz -C /opt/gitblit --strip-components 1 ; \ rm -f gitblit.tar.gz ; \ # Remove unneeded scripts. rm -f /opt/gitblit/install-service-*.sh ; \ rm -r /opt/gitblit/service-*.sh ; \ \ # Change shell to 'sh' for Alpine for file in /opt/gitblit/*.sh ; do \ sed -i -e 's;bin/bash;bin/sh;' $file ; \ done % else # Install fetch dependencies, and gsou to step down from root RUN set -eux ; \ apt-get update && apt-get install -y --no-install-recommends \ wget \ gosu \ ; \ rm -rf /var/lib/apt/lists/* ; \ # Download and install Gitblit wget --progress=bar:force:noscroll -O gitblit.tar.gz ${GITBLIT_DOWNLOAD_URL} ; \ echo "${GITBLIT_DOWNLOAD_SHA} *gitblit.tar.gz" | sha256sum -c - ; \ mkdir -p /opt/gitblit ; \ tar xzf gitblit.tar.gz -C /opt/gitblit --strip-components 1 ; \ rm -f gitblit.tar.gz ; \ # Remove unneeded scripts. rm -f /opt/gitblit/install-service-*.sh ; \ rm -r /opt/gitblit/service-*.sh ; \ \ # It is getting annoying not to have 'll' and colors when opening a bash in the container echo "export LS_OPTIONS='--color=auto'" >> /root/.bashrc ; \ echo 'eval `dircolors -b`' >> /root/.bashrc ; \ echo "alias ls='"'ls $LS_OPTIONS'"'" >> /root/.bashrc ; \ echo "alias ll='"'ls $LS_OPTIONS -l'"'" >> /root/.bashrc ; % fi % else ADD <% ${GITBLIT_FILE} %> /opt/ # Install gosu to step down from root RUN set -eux ; \ apt-get update && apt-get install -y --no-install-recommends \ gosu \ ; \ rm -rf /var/lib/apt/lists/* ; \ # Adjust folder name of gitblit unpacked by ADD command mv /opt/gitblit-* /opt/gitblit ; \ # Remove unneeded scripts. rm -f /opt/gitblit/install-service-*.sh ; \ rm -r /opt/gitblit/service-*.sh ; \ \ # It is getting annoying not to have 'll' and colors when opening a bash in the container echo "export LS_OPTIONS='--color=auto'" >> /root/.bashrc ; \ echo 'eval `dircolors -b`' >> /root/.bashrc ; \ echo "alias ls='"'ls $LS_OPTIONS'"'" >> /root/.bashrc ; \ echo "alias ll='"'ls $LS_OPTIONS -l'"'" >> /root/.bashrc ; % fi LABEL maintainer="James Moger , Florian Zschocke " \ org.label-schema.schema-version="1.0" \ org.label-schema.name="gitblit" \ org.label-schema.description="Gitblit is an open-source, pure Java stack for managing, viewing, and serving Git repositories." \ org.label-schema.url="http://gitblit.com" \ org.label-schema.version="${GITBLIT_VERSION}" \ org.opencontainers.image.title="gitblit" \ org.opencontainers.image.description="Gitblit is an open-source, pure Java stack for managing, viewing, and serving Git repositories." \ org.opencontainers.image.url="http://gitblit.com" \ org.opencontainers.image.source="https://github.com/gitblit-org/gitblit-docker" \ org.opencontainers.image.documentation="https://github.com/gitblit-org/gitblit-docker/blob/master/hub-readme.md" \ org.opencontainers.image.version="${GITBLIT_VERSION}" ENV GITBLIT_VAR <% ${GITBLIT_VAR} %> # Move the data files to a separate directory and set some defaults RUN set -eux ; \ mkdir -p -m 0775 $GITBLIT_VAR ; \ gbetc=$GITBLIT_VAR/etc ; \ gbsrv=$GITBLIT_VAR/srv ; \ mkdir -p -m 0775 $gbsrv ; \ mv /opt/gitblit/data/git $gbsrv ; \ ln -s $gbsrv/git /opt/gitblit/data/git ; \ mv /opt/gitblit/data $gbetc ; \ ln -s $gbetc /opt/gitblit/data ; \ \ # Make sure that the most current default properties file is available # unedited to Gitblit. mkdir -p /opt/gitblit/etc/ ; \ mv $gbetc/defaults.properties /opt/gitblit/etc ; \ printf "\ 6 c\\\n\ \\\n\ \\\n\ ""#\\\n\ ""# DO NOT EDIT THIS FILE. IT CAN BE OVERWRITTEN BY UPDATES.\\\n\ ""# FOR YOUR OWN CUSTOM SETTINGS USE THE FILE ${gbetc}/gitblit.properties\\\n\ ""# THIS FILE IS ONLY FOR REFERENCE.\\\n\ ""#\\\n\ \\\n\ \\\n\ \n\ /^# Base folder for repositories/,/^git.repositoriesFolder/d\n\ /^# The location to save the filestore blobs/,/^filestore.storageFolder/d\n\ /^# Specify the location of the Lucene Ticket index/,/^tickets.indexFolder/d\n\ /^# The destination folder for cached federation proposals/,/^federation.proposalsFolder/d\n\ /^# The temporary folder to decompress/,/^server.tempFolder/d\n\ s/^server.httpPort.*/#server.httpPort = 8080/\n\ s/^server.httpsPort.*/#server.httpsPort = 8443/\n\ s/^server.redirectToHttpsPort.*/#server.redirectToHttpsPort = true/\n\ " > /tmp/defaults.sed ; \ sed -f /tmp/defaults.sed /opt/gitblit/etc/defaults.properties > $gbetc/defaults.properties ; \ rm -f /tmp/defaults.sed ; \ # Check that removal worked grep "^git.repositoriesFolder" $gbetc/defaults.properties && false ; \ grep "^filestore.storageFolder" $gbetc/defaults.properties && false ; \ grep "^tickets.indexFolder" $gbetc/defaults.properties && false ; \ grep "^federation.proposalsFolder" $gbetc/defaults.properties && false ; \ grep "^server.tempFolder" $gbetc/defaults.properties && false ; \ \ # Create a system.properties file that sets the defaults for this docker setup. # This is not available outside and should not be changed. echo "git.repositoriesFolder = ${gbsrv}/git" > /opt/gitblit/etc/system.properties ; \ echo "filestore.storageFolder = ${gbsrv}/lfs" >> /opt/gitblit/etc/system.properties ; \ echo "tickets.indexFolder = ${gbsrv}/tickets/lucene" >> /opt/gitblit/etc/system.properties ; \ echo "federation.proposalsFolder = ${gbsrv}/fedproposals" >> /opt/gitblit/etc/system.properties ; \ echo "server.tempFolder = ${GITBLIT_VAR}/temp/gitblit" >> /opt/gitblit/etc/system.properties ; \ echo "server.httpPort = 8080" >> /opt/gitblit/etc/system.properties ; \ echo "server.httpsPort = 8443" >> /opt/gitblit/etc/system.properties ; \ echo "server.redirectToHttpsPort = true" >> /opt/gitblit/etc/system.properties ; \ \ # Create a properties file for settings that can be set via environment variables from docker printf '\ ''#\n\ ''# GITBLIT-DOCKER.PROPERTIES\n\ ''#\n\ ''# This file is used by the docker image to store settings that are defined\n\ ''# via environment variables. The settings in this file are automatically changed,\n\ ''# added or deleted.\n\ ''#\n\ ''# Do not define your custom settings in this file. Your overrides or\n\ ''# custom settings should be defined in the "gitblit.properties" file.\n\ ''#\n\ ''# Do NOT change this include line. It makes sure that settings for this docker image are set.\n\ ''#\n\ include = /opt/gitblit/etc/defaults.properties,/opt/gitblit/etc/system.properties\n\ \n' > $gbetc/gitblit-docker.properties ; \ \ # Comment out settings in defaults that we support to override in gitblit-docker.properties sed -i -e 's/^\(web.enableRpcServlet.*\)/#\1/' \ -e 's/^\(web.enableRpcManagement.*\)/#\1/' \ -e 's/^\(web.enableRpcAdministration.*\)/#\1/' \ $gbetc/defaults.properties ; \ \ # Create the gitblit.properties file that the user can use for customization. printf '\ ''#\n\ ''# GITBLIT.PROPERTIES\n\ ''#\n\ ''# Define your custom settings in this file and/or include settings defined in\n\ ''# other properties files.\n\ ''#\n\ \n\ ''# NOTE: Gitblit will not automatically reload "included" properties. Gitblit\n\ ''# only watches the "gitblit.properties" file for modifications.\n\ ''#\n\ ''# Paths may be relative to the ${baseFolder} or they may be absolute.\n\ ''#\n\ ''# ONLY append your custom settings files at the END of the "include" line.\n\ ''# The present files define the default settings for the docker container. If you\n\ ''# remove them or change the order, things may break.\n\ ''#\n\ include = gitblit-docker.properties\n\ \n\ ''#\n\ ''# Define your overrides or custom settings below\n\ ''#\n\ \n' > $gbetc/gitblit.properties ; \ \ \ # Change ownership to gitblit user for all files that the process needs to write chown -R gitblit:gitblit $GITBLIT_VAR ; \ # Set file permissions so that gitblit can read all and others cannot mess up # or read private data chmod ug+rwxs $gbsrv $gbsrv/git ; \ chmod ug+rwxs $gbetc $gbetc/certs ; \ chmod go=r $gbetc/defaults.properties ; \ chmod 0664 $gbetc/gitblit-docker.properties ; \ chmod 0664 $gbetc/gitblit.properties ; \ \ # Now we make a backup of the etc files, so that we can copy them to mount bound # volumes to make sure all needed files are present in them. cp -a $gbetc /opt/gitblit/vog-etc ; \ cp -a $gbsrv/git/project.mkd /opt/gitblit/srv-project.mkd ; %# %# The following block is generated for a single Dockerfile which can build Ubuntu and Alpine %# base images from one file, using the '--target' parameter. % if [[ "$DOCKERFILE_TYPE" != alpine && "$DOCKERFILE_TYPE" != ubuntu ]] ; then # # Create the alpine based image, using the gitblit installation prepared in the base image # FROM openjdk:8-jre-alpine AS alpine # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever packages get added RUN addgroup -S -g 8117 gitblit && adduser -S -H -G gitblit -u 8117 -h /opt/gitblit gitblit ENV GITBLIT_VERSION <% ${GITBLIT_VERSION} %> ENV GITBLIT_VAR <% ${GITBLIT_VAR} %> COPY --from=base /opt/gitblit /opt/gitblit COPY --from=base ${GITBLIT_VAR} ${GITBLIT_VAR} # Install su-exec to step down from root RUN set -eux; \ apk add --no-cache \ 'su-exec>=0.2' \ ; \ \ \ # Change shell to 'sh' for Alpine for file in /opt/gitblit/*.sh ; do \ sed -i -e 's;bin/bash;bin/sh;' $file ; \ done LABEL maintainer="James Moger , Florian Zschocke " \ org.label-schema.schema-version="1.0" \ org.label-schema.name="gitblit" \ org.label-schema.description="Gitblit is an open-source, pure Java stack for managing, viewing, and serving Git repositories." \ org.label-schema.url="http://gitblit.com" \ org.label-schema.version="${GITBLIT_VERSION}" # Provide script and data to migrate from earlier images to the new layout. COPY migrate/migrate-data /usr/local/bin/ COPY migrate/non-etc-files migrate/defaults.* /usr/local/share/gitblit/ # Setup the Docker container environment ARG GITBLIT_RPC ENV GITBLIT_RPC ${GITBLIT_RPC:-on} ENV PATH /opt/gitblit:$PATH WORKDIR /opt/gitblit VOLUME $GITBLIT_VAR COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] # 8080: HTTP front-end and transport # 8443: HTTPS front-end and transport # 9418: Git protocol transport # 29418: SSH transport EXPOSE 8080 8443 9418 29418 CMD ["gitblit"] # # Create the Ubuntu based image, continuing the base image # FROM base AS ubuntu % fi # Provide script and data to migrate from earlier images to the new layout. COPY migrate/migrate-data /usr/local/bin/ COPY migrate/non-etc-files migrate/defaults.* /usr/local/share/gitblit/ # Setup the Docker container environment ARG GITBLIT_RPC ENV GITBLIT_RPC ${GITBLIT_RPC:-on} ENV PATH /opt/gitblit:$PATH WORKDIR /opt/gitblit VOLUME $GITBLIT_VAR COPY docker-entrypoint.sh /usr/local/bin/ ENTRYPOINT ["docker-entrypoint.sh"] # 8080: HTTP front-end and transport # 8443: HTTPS front-end and transport # 9418: Git protocol transport # 29418: SSH transport EXPOSE 8080 8443 9418 29418 CMD ["gitblit"]