Skip to content

Instantly share code, notes, and snippets.

@ggrandes
Last active November 26, 2019 19:47
Show Gist options
  • Save ggrandes/52d025431115596b344a to your computer and use it in GitHub Desktop.
Save ggrandes/52d025431115596b344a to your computer and use it in GitHub Desktop.
Jenkins LTS autodownload of latest version
#!/bin/bash
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Orinal source:
# https://gist.github.com/ggrandes/52d025431115596b344a
#
# Automatic download Jenkins LTS war file
#
# 2015.04.14, v1.0, ggrandes
#
# https://jenkins-ci.org/changelog-stable
# http://mirrors.jenkins-ci.org/war-stable/latest/jenkins.war
#
DIST_HOME="/opt/jenkins/dist/"
BASE_URL="http://mirrors.jenkins-ci.org/war-stable/"
BASE_FILE="jenkins.war"
VERSION_FILE="current_version"
TMP_FILE="/tmp/jenkins-ci.$$.tmp"
#
download () {
local urlget="$1" outfile="$2"
local curl_params=""
#
[ -s "${outfile}" ] &&
curl_params="--time-cond ${outfile}"
curl -L -q -f -sS -m 180 --retry 3 \
-o "${outfile}" ${curl_params} \
"${urlget}"
}
#
cd $DIST_HOME
echo $(date +'%Y-%m-%d %H:%M:%S') Getting version
download "${BASE_URL}" "${TMP_FILE}"
LATEST_STABLE="$(sed -r -e 's/^.*href=\"([0-9.]+)\/\".+/JENKINS_VERSION:\1/g' < $TMP_FILE | \
grep '^JENKINS_VERSION:' | sort -k2 -V | tail -1 | cut -d: -f2)"
rm -f "${TMP_FILE}" 1>/dev/null 2>&1
[ -r "$VERSION_FILE" ] && read CURRENT_VERSION < ${VERSION_FILE}
echo $(date +'%Y-%m-%d %H:%M:%S') Current version: ${CURRENT_VERSION:-NONE}
echo $(date +'%Y-%m-%d %H:%M:%S') Lastest version: ${LATEST_STABLE}
if [ "${CURRENT_VERSION}" != "${LATEST_STABLE}" ]; then
BASE_FILE_VER="jenkins-${LATEST_STABLE}.war"
BASE_URL_VER="${BASE_URL}${LATEST_STABLE}/jenkins.war"
echo $(date +'%Y-%m-%d %H:%M:%S') Downloading Jenkins ${LATEST_STABLE}
rm -f "${BASE_FILE_VER}" 1>/dev/null 2>&1
download "${BASE_URL_VER}" "${BASE_FILE_VER}" "${BASE_FILE}"
[ -r "${BASE_FILE_VER}" ] &&
echo $(date +'%Y-%m-%d %H:%M:%S') Checking downloaded file &&
unzip -t "${BASE_FILE_VER}" &&
chmod a+r "${BASE_FILE_VER}" &&
echo $(date +'%Y-%m-%d %H:%M:%S') Linking to latest version &&
ln -fs "${BASE_FILE_VER}" "${BASE_FILE}" &&
touch "${BASE_FILE}" &&
echo "${LATEST_STABLE}" > ${VERSION_FILE}
fi
echo $(date +'%Y-%m-%d %H:%M:%S') END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment