Lab Steps

lock
Logging in to the Amazon Web Services Console
lock
Connecting to the Virtual Machine using SSH
lock
Launch Jenkins and SonarQube Docker Containers
lock
Login to SonarQube and Generate Security Token
lock
Log in to Jenkins and Complete the Default Installation
lock
Install and Configure SonarQube and Gradle Plugins
lock
Create and Execute Jenkins Pipeline Gradle Job
lock
Review SonarQube Static Analysis Report
lock
Validate AWS Lab
Need help? Contact our support team

Here you can find the instructions for this specific Lab Step.

If you are ready for a real environment experience please start the Lab. Keep in mind that you'll need to start from the first step.

Introduction

In this Lab Step, you will continue using the Jenkins administration web console. You will create a new build pipeline job. The pipeline job will itself git clone a sample Java servlet web application. Gradle is used to manage and download all the Java library dependencies, perform a compilation, and package the outputs into a deployable WAR (web archive) file. Finally, the build pipeline will publish the Java source code into SonarQube for static code analysis.

 

Instructions 

1. Click create new jobs on the Jenkins home page:

alt

2. Enter a build job name such as BuildJob1, and select Pipeline, followed by clicking OK:

alt

3. Within the current build job, click the Pipeline tab to be taken directly to the Pipeline section:

alt

4. Change the Pipeline Definition to Pipeline script from SCM and set SCM to Git. Set the Repository URL to https://github.com/cloudacademy/devops-jenkins-sonarqube and leave all remaining settings as defaults, ensuring in particular that the Script Path setting is set to Jenkinsfile. Click Save:

alt

5. Before you kickoff the Jenkins build job, take a quick look at the Jenkinsfile located within the GitHub repository (https://github.com/cloudacademy/devops-jenkins-sonarqube) that we just configured in the previous step. For convenience, this is also displayed here:

Copy code
//START-OF-SCRIPT
node {
    def SONARQUBE_HOSTNAME = 'sonarqube'

    def GRADLE_HOME = tool name: 'gradle-4.10.2', type: 'hudson.plugins.gradle.GradleInstallation'
    sh "${GRADLE_HOME}/bin/gradle tasks"

    stage('prep') {
        git url: 'https://github.com/cloudacademy/devops-webapp.git'                
    }

    stage('build') {
        sh "${GRADLE_HOME}/bin/gradle build"
    }

    stage('sonar-scanner') {
      def sonarqubeScannerHome = tool name: 'sonar', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
      withCredentials([string(credentialsId: 'sonar', variable: 'sonarLogin')]) {
        sh "${sonarqubeScannerHome}/bin/sonar-scanner -e -Dsonar.host.url=http://${SONARQUBE_HOSTNAME}:9000 -Dsonar.login=${sonarLogin} -Dsonar.projectName=WebApp -Dsonar.projectVersion=${env.BUILD_NUMBER} -Dsonar.projectKey=GS -Dsonar.sources=src/main/ -Dsonar.tests=src/test/ -Dsonar.java.binaries=build/**/* -Dsonar.language=java"
      }
    }

}
//END-OF-SCRIPT

The Jenkinsfile is a scripted pipeline and drives the overall Jenkins CICD build process. It contains five (5) main build stages. It contains three (3) main build stages prep, build, and sonar-scanner. The prep stage git clones the sample Java servlet web application, which is then compiled using Gradle in the build stage. Finally, the sonar-scanner stage sends the Java source code over to SonarQube for static analysis.

6. Click Build Now. This will queue up and execute the build, eventually inidicated by the build progress bar in the Build History section:

alt

7. Once the build becomes active, within the Build History pane, click the build progress bar to be taken to the Console Output view for the current build. Alternatively you can click the build number and select the Console Output option from the resulting context menu:

alt

8. Within the Console Output view, examine the logged output for the build. In particular take note which agent the job was executed on. In this Lab Step, the build job executed on the Jenkins (master) agent, as indicated by the Running on Jenkins in /var/jenkins_home/workspace/BuildJob1 message. Knowing which build agent the build job runs on can help with troubleshooting in multi node setups:

alt

9. Scroll through the Console Output build details all the way to the bottom, confirming there are no build errors, and that the build has completed successfully as per the Finished: SUCCESS message at the end:

alt

Also note how there are logging statements regarding the static code analysis performed by SonarQube as per the INFO: ANALYSIS SUCCESSFUL, you can browse http://sonarqube:9000/dashboard/index/GS message.

10. Click Back to Project and review the Stage View, confirming that all stages have completed successfully as indicated by the color green (red indicates a failure of some kind):

 alt

Summary

In this Lab Step, you used the Jenkins administration web console to create and configure a new build pipeline job. You manually executed the build, and reviewed the corresponding build log ensuring that it completed successfully.