Jenkins : Automated Upgrade

*nix/Mac Auto-Upgrade With Container

If you run Jenkins with java -jar jenkins.war, Jenkins will be able to update itself from the Manage Jenkins page. This is the simplest way to do automatic upgrades.

If you've installed via the OS X installer, the 'jenkins' running the process does not own the .war file. To get auto-upgrade working, just fix the permissions so the jenkins user can write the WAR (in /Applications/Jenkins).

If you run Jenkins in other servlet containers, here is a simple set of steps to create a Jenkins job to semi-automate Jenkins Updates written by Rolf. The local paths need to be changed to reflect the individual setup configuration.

cd /tmp
rm -f jenkins.war.backup
cp /usr/local/jboss/server/default/deploy/jenkins.war /tmp/jenkins.war.backup
rm -f jenkins.war
wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war
nohup /usr/local/bin/copywar.sh > /tmp/copywar.out 2>&1 &

copywar.sh contains this:

#!/bin/bash
sleep 20
cp /tmp/jenkins.war /usr/local/jboss/server/default/deploy

The reason this in in a separate script with a nohup is so that the job can complete properly before Jenkins deployment begins. In Vista it needs to be set to "Run with highest Authority," but should have no trouble running in Windows XP.

Debian Package Upgrade

Using the Upgrade-Button from within Jenkins (available since 1.318) will not work if Jenkins was installed from a Debian package (results in permission denied errors when trying to download the new WAR file)!

This update procedure works, if you installed Jenkins from a Debian package (see http://weblogs.java.net/blog/kohsuke/archive/2008/06/debian_packages.html for more information):

aptitude update
aptitude install jenkins

Windows Auto-Upgrade

If you install Jenkins as a Windows service, Jenkins will be able to update itself from the Manage Jenkins page. This is the simplest way to do it.

Alternatively, here is a similar script to the one above for Windows users.  It is a batch file and can be setup as a scheduled task to update Jenkins on a regular schedule. This particular script keeps a backup of the most recent copy of jenkins.war in the same directory as the auto updater does. Because this script starts and stops the Jenkins service for you, it does require that it be run as an Administrator on Windows 2008 and above. It does delete the complete exploded war file from the deployment location, so be careful if you save any configuration files to that directory.

@rem --[ This code block detects if the script is being running with admin PRIVILEGES If it isn't it pauses and then quits]-----
@echo OFF
net session > NUL 2> NUL
IF %ERRORLEVEL% EQU 0 (
    ECHO Administrator PRIVILEGES Detected!
) ELSE (
   echo.
   echo ####### ERROR: ADMINISTRATOR PRIVILEGES REQUIRED #########
   echo This script must be run as administrator to work properly!  
   echo If you're seeing this after clicking on a start menu icon,
   echo then right click on the shortcut and select "Run As Administrator".
   echo ##########################################################
   echo.
   PAUSE
   EXIT /B 1
)

@echo off
REM === Some modifications need to be made for your setup options ===
set deployLoc="C:\Program Files\Jenkins"
set jenkinsHome="C:\Program Files\Jenkins"
set jenkinsURL="http://mirrors.jenkins-ci.org/war/latest/jenkins.war"
set WGET="C:\Program Files\GnuWin32\bin\wget.exe"

Echo === get new files ===
%WGET% -O %deployLoc%\jenkins.war.latest --no-check-certificate %jenkinsURL%
IF %ERRORLEVEL% EQU 0 (
   echo Download successful
) ELSE (
   echo ####### ERROR: DOWNLOAD FAILED #########
   PAUSE
   EXIT /B 1
)

Echo === Stopping Current Jenkins Service ===
sc stop Jenkins

Echo === Sleeping to wait for file cleanup ===
ping -n 4 127.0.0.1 > NUL

Echo === clean files ===
copy /Y %deployLoc%\jenkins.war %deployLoc%\jenkins.war.bak"
del %deployLoc%\jenkins.war

Echo === make room to explode new war file ===
RD /s /q %jenkinsHome%\war

Echo === rename new war file ===
move /Y %deployLoc%\jenkins.war.latest %deployLoc%\jenkins.war

Echo *** Starting new upgraded Jenkins
sc start Jenkins

Echo *** Sleeping to wait for service startup
ping -n 4 127.0.0.1 > NUL