This repository provides docker-compose.yml
sample file to run Jenkins and SonarQube in Docker.
It also provides instructions for connecting SonarQube to Jenkins
- You have Docker and Docker-Compose installed (Docker for Mac, Docker for Windows, get.docker.com and manual Compose installed for Linux).
- You want to use Docker for local development (i.e. never need to install php or npm on host) and have dev and prod Docker images be as close as possible.
- You don't want to lose fidelity in your dev workflow. You want a easy environment setup, using local editors, debug/inspect, local code repo, while web server runs in a container.
- You use docker-compose for local development only (docker-compose was never intended to be a production deployment tool anyway).
- The docker-compose.yml is not meant for docker stack deploy in Docker Swarm, it's meant for happy local development.
- The project must have the jacoco plugin.
- Make sure the image tags in the file are appropriate for you.
- Make sure you are using the correct volume mount path on line 8:
- {volume_mount_path/named_volume}:/var/jenkins_home
Note: Instead, you can use named volume that will not be linked to a directory on the local machine. But then you need to add a new named volume to the Volume section at the bottom of the file.
-
Running
docker-compose up -d
is all you need. -
Install Maven
- Open and login Jenkins (http://localhost:8080/). Note: At the first start, Jenkins will ask you for a password. This password is displayed in the log. However, you can have access to this password via the container.
- Go to Manage Jenkins > Global Tool Configuration > Maven
- Click "Maven installations"
- Write the name (For example:
Maven
) - Select Install automatically
- Select the vesion (For example:
3.6.3
)
-
Setup Java
- Go to Manage Jenkins > Global Tool Configuration > JDK
- Click "JDK installations"
- Write the name (For example:
JDK
) - Write the JAVA_HOME -
/opt/java/openjdk
-
Install SonarQube Scanner plugin
- Go to Manage Jenkins > Manage Plugins > Available
- Type "SonarQube Scanner for Jenkins" in the search bar
- Select found plugin
- Click "Download now and install after restart"
- Select "Restart Jenkins when installation is complete and no jobs are running"
- Wait for the plugin to download and install, then the server will restart
-
Setup SonarQube Scanner
- Open and login SonarQube (http://localhost:9000/)
- Go to Administration > Security > Users
- Click "Update Tokens"
- Enter token name
- Click "Generate"
- Copy the token
- Open Jenkins
- Go to Manage Jenkins > Manage Credentials
- Click "Jenkins" link
- Click "Global credentials (unrestricted)" link
- Click "Add Credentials"
- Select "Secret text"
- Paste Token to the Secret field
- Enter ID (For example:
SonarQube
) - Click "Create"
- Go to Manage Jenkins > Configure System > SonarQube Servers
- Select "Enable injection of SonarQube server configuration as build environment variables"
- Click add SonarQube
- Enter the name (For example:
SonarQube
) - Enter the Server URL -
http://sonarqube:9000
- Select Server authentication token what we created earlier
-
Create Job
- Click "New Item"
- Enter the name (For example:
first-project
) - Select "Freestyle project"
- Go to Source Code Management
- Select Git
- Enter Repository URL
- Enter Enter required branch to the Branches to build field
- Go to Build Environment
- Select Prepare SonarQube Scanner environment and Select Server authentication token
- Go to Build
- Click Add build step and select installed Maven
- Enter
clean test install $SONAR_MAVEN_GOAL
to the Goals field - Click Save
-
Go to the created job
-
Click Build Now
-
Wait for the build to finish
After all these steps, you will be able to see the results of the SonarQube analysis.
If you have any remarks or ideas for improvement, please email me (babanin.dima@gmail.com).