Having fun with the Jenkins DSL
For this post i will cover a specific plugin called Jenkins Job DSL. This plugin gives you a Groovy DSL to code your Jobs, yes Code not XML. :-) This is good for lots of reasons. First of All in a DevOps context this allow you to automate the job creation which is tedious and manual. Moreover the DSL is great because it allow you to create a massive number of job, so in a microservices context lets say you have 50 microservices and all run in they own jetty and they code is on GitHub and build is done using maven. You can automate all that and create a JOB using the DSL this job will create the other jobs. Even if you have one job only for me it makes sense because you have it automated.
We will create a simple DSL and will invoke a bash script with download an builds Apache web server. The server version will be a build parameter so the user will pass this on the job when the build happens.
Groovy Job DSL for Jenkins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hudson.model.* | |
freeStyleJob("JenkinsDSLJob") { | |
properties { | |
label("master") | |
parameters { | |
stringParam("APACHE_VERSION", "2.4.18", "Apache Version") | |
} | |
def item = hudson.model.Hudson.instance.getItem('JenkinsDSLJob') | |
def value = item.lastBuild.getEnvironment(null).get('APACHE_VERSION') | |
steps { | |
shell("sudo chmod 777 build_install_apache.sh && ./build_install_apache.sh " + value) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
apacheVersion="$1" | |
echo "Building apache [ $apacheVersion ]... " | |
sudo rm -rf httpd* | |
sudo apt-get install -y libapr1-dev libaprutil1-dev | |
sudo wget http://www.carfab.com/apachesoftware//httpd/httpd-$apacheVersion.tar.gz | |
sudo tar xvf httpd-$apacheVersion.tar.gz | |
cd httpd-$apacheVersion | |
sudo ./configure | |
sudo make | |
sudo make install |
Cheers,
Diego Pacheco