Merge pull request #226 from yoanisgil/build-args

Make jenkins version configurable via build-arg
diff --git a/Dockerfile b/Dockerfile
index d91bc9b..2c4694a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -5,10 +5,16 @@
 ENV JENKINS_HOME /var/jenkins_home
 ENV JENKINS_SLAVE_AGENT_PORT 50000
 
+ARG user=jenkins
+ARG group=jenkins
+ARG uid=1000
+ARG gid=1000
+
 # 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 useradd -d "$JENKINS_HOME" -u 1000 -m -s /bin/bash jenkins
+RUN 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
@@ -39,7 +45,7 @@
   && echo "$JENKINS_SHA /usr/share/jenkins/jenkins.war" | sha1sum -c -
 
 ENV JENKINS_UC https://updates.jenkins-ci.org
-RUN chown -R jenkins "$JENKINS_HOME" /usr/share/jenkins/ref
+RUN chown -R ${user} "$JENKINS_HOME" /usr/share/jenkins/ref
 
 # for main web interface:
 EXPOSE 8080
@@ -49,7 +55,7 @@
 
 ENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log
 
-USER jenkins
+USER ${user}
 
 COPY jenkins.sh /usr/local/bin/jenkins.sh
 ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
diff --git a/jenkins.sh b/jenkins.sh
index 26768e9..2fa42cd 100755
--- a/jenkins.sh
+++ b/jenkins.sh
@@ -2,7 +2,7 @@
 
 set -e
 
-# Copy files from /usr/share/jenkins/ref into /var/jenkins_home
+# Copy files from /usr/share/jenkins/ref into $JENKINS_HOME
 # So the initial JENKINS-HOME is set with expected content.
 # Don't override, as this is just a reference setup, and use from UI
 # can then change this, upgrade plugins, etc.
@@ -13,15 +13,16 @@
 	rel="${b:23}"
 	dir=$(dirname "${b}")
 	echo " $f -> $rel" >> "$COPY_REFERENCE_FILE_LOG"
-	if [[ ! -e /var/jenkins_home/${rel} || $f = *.override ]]
+	if [[ ! -e $JENKINS_HOME/${rel} || $f = *.override ]]
 	then
 		echo "copy $rel to JENKINS_HOME" >> "$COPY_REFERENCE_FILE_LOG"
-		mkdir -p "/var/jenkins_home/${dir:23}"
-		cp -r "${f}" "/var/jenkins_home/${rel}";
+		mkdir -p "$JENKINS_HOME/${dir:23}"
+		cp -r "${f}" "$JENKINS_HOME/${rel}";
 		# pin plugins on initial copy
-		[[ ${rel} == plugins/*.jpi ]] && touch "/var/jenkins_home/${rel}.pinned"
+		[[ ${rel} == plugins/*.jpi ]] && touch "$JENKINS_HOME/${rel}.pinned"
 	fi;
 }
+: ${JENKINS_HOME:="/var/jenkins_home"}
 export -f copy_reference_file
 touch "${COPY_REFERENCE_FILE_LOG}" || (echo "Can not write to ${COPY_REFERENCE_FILE_LOG}. Wrong volume permissions?" && exit 1)
 echo "--- Copying files at $(date)" >> "$COPY_REFERENCE_FILE_LOG"