Time for an update of the post Building GitHub projects with Jenkins, Maven and SonarQube 4.1.1 on OpenShift, because SonarQube 5.2 is out: It’s the first version since 4.1.1 that can be run on OpenShift. That is, it’s the first version of SonarQube 5 and the first one that contains Elasticsearch and many other features that are now available on OpenShift!
Interested? Then let’s see how to set up SonarQube on OpenShift.
- If you’re starting from scratch, just skip to this section.
- If you got a running instance of SonarQube
- make sure to back up you instance before you continue:
rhc snapshot save -a --filepath
or
ssh @-.rhcloud.com 'snapshot' > sonar.tar.gz
- Then pull the git repository just like in step 2,
- wait until the app has started and visit
https://sonar-.rhcloud.com/setup
SonarQube will update it’s database during the process.
- If you followed this post to set up your SonarQube instance and therefore use an SSH tunnel to access the SonarQube database, note that you can now get rid of this workaround. From SonarQube 5.2 the analyses can be run without direct contact to the database.
That is, you can also remove the database connection from your the configuration of the SonarQube plugin in jenkins.
- make sure to back up you instance before you continue:
Install new SonarQube instance
To install SonarQube 5.2, execute the following steps on your machine:
-
rhc app create sonar diy-0.1 postgresql-9.2
Make sure to remember the login and passwords!
-
git rm -r diy .openshift misc README.md git remote add upstream -m master https://github.com/schnatterer/openshift-sonarqube.git git pull -s recursive -X theirs upstream master git push
- Login to your SonarQube instance at
http://sonar-.rhcloud.com/
Note that the initial setup may take some minutes. So be patient.
The default login and passwords areadmin
/admin
.
You might want to change the password right away!
Basic installation Jenkins
Basically, the following is an updated (and a lot simpler) version of my post about SonarQube 4.1.1.
- Create Jenkins app
rhc app create jenkins jenkins-1
- Install Plugins
Browse toUpdate Center
https://jenkins-.rhcloud.com/pluginManager/advanced
and hit
Check Now
(as described here).
Then go to theAvailable
tab and install- Sonar Plugin,
- GitHub plugin,
- embeddable-build-status (if you’d like to include those nifty build badges in you README.md).
Then hit
Install without restart
orDownload and install after restart
. If necessary, you can restart your app anytime like sorhc app restart -a jenkins
- Set up maven settings.xml to a writable location.
- SSH to Jenkins
mkdir $OPENSHIFT_DATA_DIR/.m2 echo -e "<settings><localRepository>$OPENSHIFT_DATA_DIR/.m2</localRepository></settings>" > $OPENSHIFT_DATA_DIR/.m2/settings.xml
- Browse to
Configure System
https://jenkins-.rhcloud.com/configure
Default settings provider: Settings file in file system
File path=$OPENSHIFT_DATA_DIR/.m2/settings.xml
- SSH to Jenkins
- Either see my post on how to introduce a dedicated slave node to this setup or
set up the Jenkins master to run its own builds as follows (not recommended on small gears, as you might run out of memory pretty fast during builds):
Go toConfigure System
https://jenkins-.rhcloud.com/configure
and set
# of executors: 1
- Setup sonar plugin (the following bases on SonarQube Plugin 2.3 for Jenkins)
On the Jenkins frontend, go toConfigure System
https://jenkins-.rhcloud.com/configure
Global properties
,
tickEnvironment variables
ClickAdd
name=SONAR_USER_HOME
value=$OPENSHIFT_DATA_DIR
See here for more information.- Setup the Runner:
Navigate toSonarQube Runner
ClickAdd SonarQube Runner
Name=
- Then set up the plugin itself
Navigate toSonarQube
tickEnable injection of SonarQube server configuration as build environment variables
and set the following
Name=
Server URL:
http://sonar-.rhcloud.com/
Sonar account login: admin
Sonar account password:
(
default:admin
) - Hit
Save
Configure build for a repository
Now lets set up our first build.
- Go to
https://jenkins-.rhcloud.com/view/All/newJob
Item name:
(Unfortunately, Maven projects do not work due to OpenShift’s restrictions.)
HitOK
- On the next Screen
GitHub project
:https://github.com///
Source Code Management
:https://github.com//.git
Branch Specifier (blank for 'any'): origin/master
Build Triggers
: TickBuild when a change is pushed to GitHub
Build Environment
: TickPrepare SonarQube Scanner environment
Build
|Execute Shell
cd $WORKSPACE # Start the actual build mvn clean package $SONAR_MAVEN_GOAL --settings $OPENSHIFT_DATA_DIR/.m2/settings.xml -Dsonar.host.url=$SONAR_HOST_URL
- I’d also recommend the following actions
Post-build Actions
|Add post-build action
|Publish JUnit test result report
Test report XMLs=target/surefire-reports/TEST-.xml*
Post-build Actions
|Add post-build action
|E-mail Notification
Recipients=
- Hit
Apply
. - Finally, press
Save
and start you first build. Check Jenkins console output for errors. If everything succeeds you should see the result of the project’s analysis on SonarQube’s dashboard.