Search code examples
jenkinsvirtualboxautostart

How can Jenkins autostart a VirtualBox slave?


I have a VirtualBox Jenkins slave. I start it manually every time it goes down (e.g. after doing reboot to the PC).

Is there a way to make Jenkins start it automatically?


Solution

  • I have a Windows host that should run an Ubuntu 14.04 virtual machine Jenkins slave.

    Pre-requirements: you should have a VM slave defined in Jenkins (http://YOUR-JENKINS-URL/computer/)

    This is how I solved it:

    1) Setup Ubuntu VM to automatically start a service on boot that runs a script to start Jenkins slave:

    • Use upstart to run the script: create a the file /etc/init/jenkins-slave.conf

      description "A job to start a Jenkins slave"
      author "Your Name"
      start on runlevel [2345]
      exec echo Starting Jenkins slave at `date` >> /var/log/jenkins-slave-job.log
      exec /jenkins/run.sh
      
    • The /jenkins/run.sh:

      cd $(dirname $0)
      java -jar slave.jar -jnlpUrl         
      http://YOUR-JENKINS-URL/computer/NAME-OF-NODE/slave-agent.jnlp -secret 51d080f68b3d2552c977840aa8a01bb371a1b3e8b3326f36fadb497e597185ce
      
    • The /Jenkins folder should contain slave.jar file downloaded from Jenkins node page.

    • The 2 last lines in the run.sh file should be taken from the Jenkins node page.

    2) Setup Windows to start the VM on startup:

    • Create a Windows shourtcut with the following value in the "Target:"

      "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm "UbuntuVBox"
      
    • Where UbuntuVBox should be replaced with the name of your VM

    • Put the shortcut under "Startup" folder in the Windows start menu

    To conclude:

    After doing the above steps - if you reboot the Windows host, it should run the shortcut, that will run the VM. The VM will run the service "Jenkins-slave" and your Jenkins slave should be up and running.