Configure Gerrit
We are going to create two user accounts in Gerrit. One representing a development team lead and another representing a developer. 1. Connect to Gerrit via http://localhost:8082/login
- Select the New Account button under the Register heading
- Enter Team Lead for the full name and click the Save Changes button
- Enter teamlead for the username and click the Select Username button (Click ok to confirm)
- Paste the ssh public key that you use for Git (e.g., ~/.ssh/id_rsa.pub) into the Add SSH Public Key text box and click the Add button
- If you are using the container image, you can generate an ssh key pair by following these steps:
- Log in to your container
$ docker exec -it ciserver /bin/bash*
- Generate a key pair
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/opt/ci-server/.ssh/id_rsa): **(1)** Created directory '/opt/ci-server/.ssh'. Enter passphrase (empty for no passphrase): **(1)** Enter same passphrase again: **(1)** Your identification has been saved in /opt/ci-server/.ssh/id_rsa. Your public key has been saved in /opt/ci-server/.ssh/id_rsa.pub. The key fingerprint is: SHA256:ApTkVs+jY/N85pLY4WTKOJ1n2SOojs7B8Fi6F4/wsOg ci-server@2cdd3e3d74de The keys randomart image is: +---[RSA 2048]----+ | .o.. | | o.. | | + + | | . . . . | |. . * S | | | |+=++ + Xo=o | | | |oE+.oo o o.. | +----[SHA256]-----+*
1 Press enter when prompted
- Copy the public key that was created in /opt/ci-server/.ssh/id_rsa.pub
- Click the Continue link on the bottom of the page. Since this is the first user we have created in Gerrit, the user is automatically granted administrator privileges.
- Go back to http://localhost:8082/login and Select the New Account button under the Register heading again
- Enter Team Developer for the full name and click the Save Changes button
- Enter teamdeveloper for the username and click the Select Username button (Click ok to confirm)
- Click the Register New Email.. button, enter the email address that you have (or will have) in your .gitconfig. Click Register
- Paste the ssh public key that you use for Git (e.g., ~/.ssh/id_rsa.pub) into the Add SSH Public Key text box and click the Add button. Use the same public key as was used for the team lead user.
- Click the Continue link on the bottom of the page.
Configure Maven for SonarQube
Note You can skip this section if using the container image
Make a backup copy of /usr/share/maven/conf/settings.xml and use the following instead. If you are using the container image this file is already in place.
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers>
<server> (1)
<id>internal.repo</id>
<username>jenkins</username>
<password>jenkins123</password>
</server>
</servers>
<pluginGroups> (2)
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://localhost:8084 (3)
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
1 Credentials for pushing artifacts into Nexus. Note Maven also supports password encryption - http://maven.apache.org/guides/mini/guide-encryption.html
2 Sonar plugin for static code analysis
3 URL of the sonar server
Configure Jenkins
Note You will need to run through these steps even if you are using the container image.
Configure Maven
1. Return to the Jenkins console (http://localhost:8083) and login as admin
- Click the Manage Jenkins link on the left
- Click Global Tool Configuration
- At the very bottom, click Add Maven. For the name specify M3. Deselect Install automatically and set the value of MAVEN_HOME to /usr/share/maven
- Click the Save button at the bottom of the page.
Install Additional Plugins
1. On the Manage Jenkins page, select Manage Plugins
- Click the Available tab and type sonarqube in the search filter.
- Select the check box next to SonarQube Plugin and click the Install without restart button
- Repeat the previous steps to install the Sidebar Link plugin by specifying sidebar link in the search filter
Set JAVA_HOME
1. Navigate to Manage Jenkins → Configure System
- Under Global Properties select Environment Variables
- Enter JAVA_HOME for the name and /usr/lib/jvm/java-1.8.0 as the value
- Click the Save button at the bottom of the page
Add links to SonarQube, Nexus and Gerrit (optional)
These steps will create links on the main Jenkins page to the Nexus, Gerrit and SonarQube pages. 1. Navigate to Manage Jenkins → Configure System
- Under Additional Sidebar Links enter the following information:
Field |
Value |
Link URL |
http://localhost:8084 |
Link Text |
SonarQube |
Link Icon |
gear.png |
- Click the Add Link button and enter the following values for Nexus
Field |
Value |
Link URL |
http://localhost:8081 |
Link Text |
Nexus |
Link Icon |
gear.png |
- Click the Add Link button and enter the following values for Gerrit
Field |
Value |
Link URL |
http://localhost:8082 |
Link Text |
Gerrit |
Link Icon |
gear.png |
- Click the Save button at the bottom of the screen
Push code to Gerrit
These directions will refer to $GIT_HOME as the directory where you store your git projects, for example ~/git. With these steps we will create a new project in Gerrit, seeding it with source code from an existing GitHub repository.
Note If you are using the container image, you can perform these steps within the container. Log in to the container and create a git directory within the home directory mkdir ~/git && cd ~/git
- Clone the demo application (https://github.com/kharyam/dzone-ci-demo-app) from GitHub and remove the .git directory
$ cd $GIT_HOME $ git clone https://github.com/kharyam/dzone-ci-demo-app.git $ rm -fr $GIT_HOME/dzone-ci-demo-app/*.git
- Create a new project named Demo in Gerrit as the teamlead user
$ ssh -p 29418 teamlead@localhost gerrit create-project Demo --empty-commit*
- Clone the new project as the teamdeveloper user
$ cd $GIT_HOME
$ git clone ssh://teamdeveloper@localhost:29418/Demo.git
- Move the files from the github project into the Gerrit project
$ mv $GIT_HOME/dzone-ci-demo-app/* $GIT_HOME/Demo $ mv $GIT_HOME/dzone-ci-demo-app/.gitignore $GIT_HOME/*Demo
- Install the Gerrit Git commit hook. This hook will append a change-id to the commit message, which is required by Gerrit. For more information see https://Gerrit-review.googlesource.com/Documentation/user-changeid.html
$ cd $GIT_HOME/Demo $ gitdir=$(git rev-parse --git-dir); scp -p -P 29418 teamdeveloper@localhost:hooks/commit-msg ${gitdir}/h*ooks/
- Commit and push the code to Gerrit
Note If you are running this within the container, run the following commands first:
$ git config --global user.name "teamdeveloper"
$ git config --global user.email "email@somewhere.com" (1)
$ cd $GIT_HOME/Demo
$ git add . (1)
$ git commit -am "Initial commit" (2)
$ git push origin HEAD:refs/for/master (3)
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (20/20), 3.32 KiB | 0 bytes/s, done.
Total 20 (delta 0), reused 0 (delta 0)
remote: Processing changes: new: 1, refs: 1, done
remote:
remote: New Changes:
remote: http://localhost:8082/6 Initial commit
remote:
To ssh://teamdeveloper@localhost:29418/Demo.git
1 Recursively stage all files into the local Git repository 2 Commit all newly added / modified files into the local Git repository 3 Push the commit into the remote Gerrit repository. HEAD:refs/for/master is a special Git reference used by Gerrit to stage changes for review prior to making them available on the authoritative Git repository.
- Now if you navigate to the Open Reviews in Gerrit (http://localhost:8082/#/q/status:open) you will see the pending review for this initial commit.
Create the Jenkins Project
Here we will create a new Jenkins project that will deploy our Continuous Integration Pipeline. The Pipeline itself is defined in the Jenkinsfile of our sample project.
1. Login to the Jenkins console (http://localhost:8083/) as the admin user
- Click the New Item link
- Enter a project name of CI Demo, Select the Pipeline option then click the OK button
- Under Build Triggers select Trigger builds remotely (e.g., from scripts). For the Authentication Token enter super_secret_build_token
Note This will allow us to trigger a build by performing an HTTP get on the URL http://localhost:8083/job/CI%20Demo/build?token=super_secret_build_token We will use this to create a Git hook in Gerrit that will trigger a Jenkins build when code has been successfully peer reviewed.
- In the Pipeline section, set the Definition to Pipeline script from SCM
- Set the SCM to Git and enter the URL of the Gerrit repository - http://localhost:8082/p/Demo.git
- Click the Save button
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}