Skip to content

Instantly share code, notes, and snippets.

@mkoertgen
Last active July 12, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkoertgen/c9dcca7a6dbc09e8f80b to your computer and use it in GitHub Desktop.
Save mkoertgen/c9dcca7a6dbc09e8f80b to your computer and use it in GitHub Desktop.
Install a jenkins build server for .NET projects with boxstarter
Scripts for jenkins build server automation on windows
slave.jar
jenkins.war
defaults.bat
slave-agent.jnlp
@echo off
setlocal
set DIR=%~dp0
set command=%1
set role=%2
rem https://gist.github.com/mkoertgen/c9dcca7a6dbc09e8f80b
rem What you need
rem 1. jenkins.war
rem 2. slave.jar
rem 3. credentials:
rem - user,pwd (logon for scheduled task)
rem - for slave: jnlpUrl, jnlpCredentials=user:apitoken
set jenkinsDir=%3
set taskUser=%4
set taskPwd=%5
set jnlpUrl=%6
set jnlpCredentials=%7
set taskName=%~n0
:set_defaults
if exist "%DIR%defaults.bat" call "%DIR%defaults.bat"
if "%jenkinsDir%"=="" set jenkinsDir=%DIR%
if "%jenkinsPort%"=="" set jenkinsPort=80
if "%command%"=="" set command=usage
goto %command%
:usage
echo Run '%taskName%' as scheduled task
echo.
echo Usage: %~n0 [command]
echo.
echo Available commands:
echo.
echo usage Shows this help
echo run Run '%taskName%'
echo install Install '%taskName%'
echo start Start '%taskName%'
echo stop Stop '%taskName%'
echo status Show status of '%taskName%'
echo restart Restart '%taskName%'
echo uninstall Uninstall '%taskName%'
goto end
:install
echo Installing '%taskName%' (%role%) ...
echo.
xcopy /D /H /R /E /I /Y /Q "%DIR%*.*" "%jenkinsDir%\"
call :check_error %errorlevel% "Could not copy installation files." || exit /B 1
schtasks /Create /TN %taskName% /RU %taskUser% /RP %taskPwd% /SC onstart /RL HIGHEST /DELAY 0000:30 /TR "%jenkinsDir%\%~n0 run %role%"
call :check_error %errorlevel% "Could not install '%taskName%'." || exit /B 1
echo.
call :start || exit /B 2
echo.
echo Installed '%taskName%'.
goto end
:uninstall
echo Uninstalling '%taskName%' ...
echo.
call :stop || exit /B 2
schtasks /Delete /TN %taskName% /f
call :check_error %errorlevel% "Could not uninstall '%taskName%'." || exit /B 1
rem wait 5 seconds to relieve access problems to logfile
ping 127.0.0.1 -n 5 > nul
rem rmdir /S /Q "%jenkinsDir%"
echo.
echo Uninstalled '%taskName%'.
goto end
:start
echo Starting '%taskName%' ...
schtasks /Run /I /TN %taskName%
call :check_error %errorlevel% "Could not start '%taskName%'." || exit /B 1
echo Started '%taskName%'.
goto end
:stop
schtasks /End /TN %taskName%
call :check_error %errorlevel% "Could not stop '%taskName%'." || exit /B 1
taskkill /im java.exe /f /FI "WINDOWTITLE eq Jenkins" >nul 2>&1
echo Stopped '%taskName%'.
goto end
:status
echo Status of '%taskName%' is ...
schtasks /Query /TN %taskName%
goto end
:restart
echo Restarting '%taskName%' ...
echo.
call :stop || exit /B 1
echo.
call :start || exit /B 2
echo.
echo Restarted '%taskName%'.
goto end
:run
echo Starting 'Jenkins' (%role%) ...
echo Please keep this window open (minimized is OK).
cd /D "%jenkinsDir%"
if "%JENKINS_HOME%"=="" set JENKINS_HOME=%jenkinsDir%\home
if not exist "%JENKINS_HOME%" mkdir "%JENKINS_HOME%" 2>nul
if "%role%"=="master" (
set jargs=-Xrs -Xmx512m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar jenkins.war --httpPort=%jenkinsPort%
) else (
set jargs=-jar slave.jar -jnlpUrl %jnlpUrl% -jnlpCredentials %jnlpCredentials%
)
start "Jenkins" /B /high /wait java %jargs% > %JENKINS_HOME%\jenkins.log 2>&1
rem exit code 1 is Ok (Ctrl+C)
call :check_error %errorlevel% "Could not run 'Jenkins'." || exit /B 1
goto end
:end
endlocal
exit /B 0
:check_error
set /a exitCode = %1
set /a okCode = 0
if not "%3"=="" set /a okCode = %3
if %exitCode% equ %okCode% exit /B 0
echo ERROR: %2 (code: %exitCode%)
if "%4"=="log" echo ERROR: %2 (code: %exitCode%) >>%logfile%
exit /B %exitCode%
Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowProtectedOSFiles -EnableShowFileExtensions -EnableShowFullPathInTitleBar
Enable-RemoteDesktop
Disable-InternetExplorerESC
# for machine
#& setx /m HTTP_PROXY http://[proxy]
#& setx /m HTTPS_PROXY http://[proxy]
#& setx /m NO_PROXY .dns.suffix
# for this process
#& set HTTP_PROXY=http://[proxy]
#& set HTTPS_PROXY=http://[proxy]
#& set NO_PROXY=.dns.suffix
# jenkins
cinst git -params '"/GitAndUnixToolsOnPath /NoAutoCrlf"' -y
# & setx /m PATH "%PATH%;C:\Program Files\Git\bin"
cinst jdk8 -y
# for a Jenkins slave we are done, for a master we need jenkins
#cinst jenkins -y
# TODO
# msbuild14, fxcop14
# cf.: https://gist.github.com/mkoertgen/417e8d3dfbe6ae8bc2f7
# "path\install_fxcop14.bat"
cinst netfx-4.5.2-devpack -y
# nuget
cinst nuget.commandline -y
# gitversion
cinst gitversion.portable -y
# mkdocs
cinst plantuml -y
cinst python -y
pip install mkdocs
# cleaver
cinst nodejs -y
cinst phantomjs -y
npm install -g cleaver
# sonar
# ignore dependency jre8, we already have jdk8
cinst sonar-runner -y -i
# powercli
# PS4 & management framework (prerequisite, usually needs reboot)
cinst powershell -y
# boxstarter for deployments targeting Windows machines
cinst boxstarter -y
# TODO...
@mkoertgen
Copy link
Author

How to use this?

  1. Setup a Windows machine
  2. Run the boxstarter script jenkins.ps1 for this machine
  3. login to your machine, and run the script jenkins.bat install

Be sure to read the comments in the scripts.

After cloning specify your settings in the defaults.bat. Here is an example:

if "%jenkinsDir%"=="" set jenkinsDir=c:\tools\jenkins
if "%user%"=="" set user=[user for jenkins task]
if "%pwd%"=="" set pwd=[password]
rem if "%jnlpUrl%"=="" set jnlpUrl=http://[jenkins-master]/computer/[jenkins-slave]/slave-agent.jnlp

if "%role%"=="" set role=slave
if "%jnlpUrl%"=="" set jnlpUrl=file:///c:/tools/jenkins/slave-agent.jnlp
if "%jnlpCredentials%"=="" set jnlpCredentials=[jeknins-user]:[token]

rem disable proxy for slave auth, cf.: https://issues.jenkins-ci.org/browse/JENKINS-28086
if "%role%"=="slave" set http_proxy=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment