<filename>docker/jenkins-master/Dockerfile
# jenkins/jenkins -> https://github.com/jenkinsci/docker/blob/master/Dockerfile
# openjdk:8-jdk-stretch -> https://github.com/docker-library/openjdk/blob/master/8/jre/Dockerfile
# buildpack-deps:stretch-scm -> https://github.com/docker-library/buildpack-deps/blob/f84f6184d79f2cb7ab94c365ac4f47915e7ca2a8/debian/stretch/scm/Dockerfile
# buildpack-deps:stretch-curl -> https://github.com/docker-library/buildpack-deps/blob/f84f6184d79f2cb7ab94c365ac4f47915e7ca2a8/debian/stretch/curl/Dockerfile
# debian-stretch

FROM debian:stretch

#---- buildpack-deps:stretch-curl  &  buildpack-deps:stretch-scm
RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates \
		curl \
		netbase \
		wget \
        bzr \
		git \
		mercurial \
		openssh-client \
		subversion \
        procps \
        gnupg \
		dirmngr \
		bzip2 \
		unzip \
		xz-utils \
	&& rm -rf /var/lib/apt/lists/*


#---- openjdk:8-jdk-stretch

# https://packages.debian.org/stretch/openjdk-8-jdk
ENV JAVA_DEB_VERSION 8u275-b01-1~deb9u1
# https://packages.debian.org/stretch/ca-certificates-java
ENV CA_CERTIFICATES_JAVA_VERSION 20170929~deb9u3
RUN apt-get update && apt-get install -y --no-install-recommends \
		openjdk-8-jdk="$JAVA_DEB_VERSION" \
    	ca-certificates-java="$CA_CERTIFICATES_JAVA_VERSION" \
    && rm -rf /var/lib/apt/lists/* \
	\
# basic smoke test
	javac -version; \
	java -version

#---- Jenkins/Jenkins

# Install git lfs (Large File Storage) on Debian stretch per https://github.com/git-lfs/git-lfs/wiki/Installation#debian-and-ubuntu
# Avoid JENKINS-59569 - git LFS 2.7.1 fails clone with reference repository
# RUN apt-get update && apt-get upgrade -y \
#     && apt-get install -y git curl \
#     && curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash \
#     && apt-get install -y git-lfs \
#     && git lfs install \
#     && rm -rf /var/lib/apt/lists/*

ARG user=jenkins
ARG group=jenkins
ARG uid=1000
ARG gid=1000
ARG http_port=8080
ARG agent_port=50000
# To find available versions and SHA’s for Jenkins WAR files you can go here: http://mirrors.jenkins.io/war-stable/
ARG JENKINS_VERSION=2.263.1
ARG JENKINS_SHA=0eedeb2b11a32726acb57db26e262b1923cf408e84708baf471e3b53462ed6f1
ARG TINI_VERSION=v0.16.1
ARG JENKINS_HOME=/var/jenkins_home
ARG REF=/usr/share/jenkins/ref

# Can be used to customize where jenkins.war get downloaded from
ARG JENKINS_URL=https://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war

ENV JENKINS_VERSION ${JENKINS_VERSION}
ENV JENKINS_HOME $JENKINS_HOME
ENV JENKINS_SLAVE_AGENT_PORT ${agent_port}
ENV JENKINS_UC https://updates.jenkins.io
ENV JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental
ENV JENKINS_INCREMENTALS_REPO_MIRROR=https://repo.jenkins-ci.org/incrementals
ENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log
ENV REF $REF
## https://www.jenkins.io/doc/book/managing/system-properties/
ENV JAVA_OPTS="-Djava.util.logging.config.file=/var/jenkins_home/log.properties \
-Xmx2048m"
ENV JENKINS_OPTS=" --handlerCountMax=100 \
--logfile=/var/log/jenkins/jenkins.log \
--webroot=/var/cache/jenkins/war"

# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN mkdir -p $JENKINS_HOME \
  && chown ${uid}:${gid} $JENKINS_HOME \
  && groupadd -g ${gid} ${group} \
  && useradd -d "$JENKINS_HOME" -u ${uid} -g ${gid} -m -s /bin/bash ${user}

# Jenkins home directory is a volume, so configuration and build history
# can be persisted and survive image upgrades
VOLUME $JENKINS_HOME

# $REF (defaults to `/usr/share/jenkins/ref/`) contains all reference configuration we want
# to set on a fresh new installation. Use it to bundle additional plugins
# or config file with your custom jenkins Docker image.
RUN mkdir -p ${REF}/init.groovy.d

# Use tini as subreaper in Docker container to adopt zombie processes
COPY utility-files/tini_pub.gpg ${JENKINS_HOME}/tini_pub.gpg
RUN curl -fsSL https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-$(dpkg --print-architecture) -o /sbin/tini \
  && curl -fsSL https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-$(dpkg --print-architecture).asc -o /sbin/tini.asc \
  && gpg --no-tty --import ${JENKINS_HOME}/tini_pub.gpg \
  && gpg --verify /sbin/tini.asc \
  && rm -rf /sbin/tini.asc /root/.gnupg \
  && chmod +x /sbin/tini

# Install Jenkins
RUN curl -fsSL ${JENKINS_URL} -o /usr/share/jenkins/jenkins.war \
  && echo "${JENKINS_SHA}  /usr/share/jenkins/jenkins.war" | sha256sum -c -

# Prep Jenkins Directories
RUN chown -R ${user} "$JENKINS_HOME" "$REF"
RUN mkdir /var/log/jenkins
RUN mkdir /var/cache/jenkins
RUN chown -R ${user}:${group} /var/log/jenkins
RUN chown -R ${user}:${group} /var/cache/jenkins

# Expose Ports for web and slave agents
EXPOSE ${http_port}  
EXPOSE ${agent_port}

# Copy in local config files
COPY utility-files/init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy
COPY utility-files/jenkins-support /usr/local/bin/jenkins-support
COPY utility-files/jenkins.sh /usr/local/bin/jenkins.sh
COPY utility-files/plugins.sh /usr/local/bin/plugins.sh
COPY utility-files/install-plugins.sh /usr/local/bin/install-plugins.sh
RUN chmod +x /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy \
	&& chmod +x /usr/local/bin/jenkins-support \
	&& chmod +x /usr/local/bin/plugins.sh \
	&& chmod +x /usr/local/bin/jenkins.sh \
	&& chmod +x /usr/local/bin/install-plugins.sh

# Master executors config
COPY config/executors.groovy /usr/share/jenkins/ref/init.groovy.d/executors.groovy

# Install this list of plugins
COPY config/plugins.txt /tmp/plugins.txt
RUN /usr/local/bin/install-plugins.sh < /tmp/plugins.txt

# Config plugins
RUN mkdir -p "$JENKINS_HOME/userContent/ui"
COPY config/plugin_simple-theme-plugin/theme-cyan.css "$JENKINS_HOME/userContent/ui/theme-cyan.css"
COPY config/plugin_simple-theme-plugin/org.codefirst.SimpleThemeDecorator.xml "$JENKINS_HOME/org.codefirst.SimpleThemeDecorator.xml"

# Skip install wizard, but still configure a secure Jenkins by creating the admin user
# ENV JAVA_OPTS="${JAVA_OPTS} -Djenkins.install.runSetupWizard=false "
# COPY config/secureAdminUser.groovy /usr/share/jenkins/ref/init.groovy.d/security.groovy

USER ${user}

ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/jenkins.sh"]

