Jenkins setup (CI/CD)

Setting up Jenkins

1. Installation steps

Skip the Installation steps, if jenkins is already installed on your pc.

1.1. Install java

Commands to install java

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install -y fontconfig openjdk-11-jre openjdk-11-jdk

1.2. Install maven (On Jenkins machine)

Commands to install maven

cd /tmp ; sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
cd /tmp ; sudo tar -xzf apache-maven-3.9.4-bin.tar.gz -C  /opt/

1.3. Install jenkins

Commands to install jenkins

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

2. Initial configuration

Jenkins login
  • Sign in with required credentials.

  • After signing in, from the dashboard go to Manage Jenkins tab on the left to start the configuration.

Jenkins dashboard

2.1. Steps for java configuration

Manage Jenkins → Tools → JDK installations → Add JDK Name: java11 (can be any string) JAVA_HOME: /path/to/javahome (ex: /usr/lib/jvm/java)

  • Ensure the "Install Automatically" button is unchecked, as all essential installations have been completed prior to this step.

  • Based on the requirements, multiple java versions can also be configured.

Java configuration in jenkins console

2.2. Steps for maven configuration

Manage Jenkins → Tools → Maven installations → Add Maven Name: maven3.6 (can be any string) MAVEN_HOME: /path/to/mavenhome (/opt/maven)

Based on the requirements, multiple maven versions can also be configured.

Maven configuration in jenkins console

2.3. Steps for Git configuration

Manage Jenkins → Tools → Git installations → Add Git Name: git (can be any string) MAVEN_HOME: /path/to/mavenhome (/usr/bin/git)

Based on the requirements, multiple Git versions can also be configured.

Git configuration in jenkins console

2.4. Required plugins

  • The JaCoCo plugin generates code coverage reports by processing the .xml files generated by the Jenkins.

  • Steps to install JaCoCo plugin

    • Manage Jenkins -→ Plugins-→ Available plugins -→ JaCoCo.

    • After installing from the 'Available plugins' section, verify the installation by checking the 'Installed plugins' tab in the 'Plugins' panel on the left side.

JaCoCo plugin
  • The JUnit plugin in Jenkins publishes and visualizes JUnit test results, providing detailed reports and trend analysis to track test performance and failures over time.

  • Steps to install JUnit plugin

    • Manage Jenkins → Plugins→ Available plugins → JUnit.

    • After installing from the 'Available plugins' section, verify the installation by checking the 'Installed plugins' tab in the 'Plugins' panel on the left side.

JUnit plugin
  • The Warnings next generation plugin in Jenkins collects and visualizes compiler warnings, static analysis warnings, and other warnings from various tools to provide a detailed report and trend analysis of code quality issues.

  • Steps to install warning next generation plugin

    • Manage Jenkins → Plugins → Available plugins → Warnings.

    • After installing from the 'Available plugins' section, verify the installation by checking the 'Installed plugins' tab in the 'Plugins' panel on the left side.

Warning next generation plugin

3. Build executors

  • In Jenkins, an executor is a computational resource allocated to run a build. By default, each Jenkins instance is configured with a single executor. However, for better resource utilization and to handle multiple jobs concurrently, you might need to configure additional executors or manage their settings.

Accessing the executor configuration

  • Dashboard → Build executor status.

Build executor status
  • From the required list of nodes, select the gear icon of the node in which the build is going to be configured.

Gear icon node
  • In the “Number of executors” tab, enter the number of concurrent builds that this node can run.

  • Labels can be used to assign attributes to your nodes. This can be useful for scheduling jobs to run on specific nodes.

Built in node configuration

4. Jenkins new job creation

  • To create a new jenkins job go to

    • Dashboard → New Item

    • Enter the name of the job in the Enter an item name section.

    • Select Freestyle project click OK.

Free-style project

5. Configuring new job

5.1. Source code management for Bitbucket server

  • In the left-hand sidebar menu, click on Configure.

  • Under the Source Code Management section, select the Git option from the dropdown menu.

  • In the Repository URL field, enter the URL of Bitbucket Server repository.

  • For authentication, enter the credentials in the Credentials field. Create or choose from existing credentials by clicking on Credentials and selecting either Add or Existing Credentials.

  • Click Save to save the configuration.

Bitbucket integration
  • Under the Build Triggers section, locate the Branches to build field.

  • Enter a branch specifier in the text field.

  • Here are some options to enter in the Branches to build field

    • '*' (asterisk): This will trigger a build for every change made to any branch or tag in the repository.

    • 'origin/master': This will only trigger a build for changes made to the master branch.

    • 'feature/*': This will trigger a build for any branch that starts with the word "feature/".

    • '!hotfix/*': This will trigger a build for any branch except for branches that start with the word "hotfix/".

    • 'tags/*': This will trigger a build for any new tag pushed to the repository.

  • Click Save to save the configuration.

Building branches
  • Sparse checkout allows to selectively clone only specific directories or files from the Git repository. This can be useful for reducing disk space usage and build times, especially for large repositories where there is only a need to build a specific portion of the code.

  • Steps to enable sparse checkout

    • Click on the Additional Behaviors section.

    • Click on Add and select Sparse Checkout paths.

    • In the text field, enter the path(s) to the directory or file that is to be checked out.

    • Click Save to save the configuration.

Sparse checkout paths

5.2. Build environment

  • Secret text credentials are a way to securely store sensitive information, such as API tokens, passwords, or SSH keys, in Jenkins. This helps to avoid hardcoding sensitive information into your Jenkins jobs, which can improve security and make your jobs more portable.

  • Steps to create secret text credentials

    • Scroll down to the Build Environment section.

    • Tick the checkbox Use secret text(s) or file(s).

    • Click Add under the Secret text section.

    • In the Variable field, enter a name for the environment variable that will store the secret text. This name will be used to reference the secret text in the build scripts.

    • Under Credentials, select the secret text credential that is to be used. A new secret text can be created by going to Credentials > System > Global credentials (or appropriate scope) and clicking Add Credentials. Then select Secret text from the dropdown menu.

    • Click Save to save your configuration.

It is recommended to avoid storing sensitive information directly in the build scripts. Using secret text credentials is a more secure way to manage sensitive data in Jenkins.

Bindings
  • Creation of multiple secret texts is also allowed.

Secret text

5.3. Build steps

  • Build steps are the individual tasks that make up a build process. Each build step performs a specific function, such as compiling code, running tests, packaging the application, or deploying to a server.

  • Steps to configure build steps

    • Click on Add build step.

    • Select Invoke top-level Maven targets from the dropdown menu and select the required maven versions and goals.

Top level maven targets
  • Click on Add build step.

    • Select Execute shell from the dropdown menu.

    • In the text box, enter the shell command that wants to be run.

    • Optionally configure things like whether to capture environment variables or fail the build if the command fails.

Execute shell
  • Click Save to save the configuration.

5.4. Post build actions

  • Post-build actions in Jenkins are automated tasks that execute after a build job completes. These actions allows us to perform various tasks depending on the build’s outcome (success, failure, unstable) or simply as a final step in the build pipeline.

  • Steps to configure post-build actions (Publish JUnit test result report).

    • Scroll down to the Post-build Actions section.

    • From the list of available actions, choose Publish JUnit test result report.

    • In the Test report XMLs field, specify the path to the JUnit test report files using the Ant glob syntax (similar to wildcard patterns). This pattern will match all JUnit XML files with the .xml extension located within sub-directories of target/surefire-reports (assuming that’s where the test reports are generated).

    • Click the Save button at the bottom of the configuration page.

JUnit test result report
  • Steps to configure post-build actions ( Record JaCoCo coverage report ).

    • Select the Record JaCoCo coverage report action.

    • Specify the path for JaCoCo execution data files.

    • These are typically generated during the build process and have a .exec extension ( e.g., target/jacoco.exec ). Use Ant path patterns for flexibility ( e.g., */target/.exec ).

    • Define the path to the compiled class files. This helps JaCoCo map the execution data to the source code (e.g., target/classes ).

    • Optionally specify the location of the source code files (e..g., src/main/java ). This allows JaCoCo to display line numbers and source code snippets in the report.

    • Optionally provide an Ant path pattern to exclude specific source files or directories from the coverage report.

    • Click the Save button to apply the configuration.

JaCoCo coverage report
  • Steps to configure post-build actions ( Record compiler warnings and static analysis report ).

    • Select the Record compiler warnings and static analysis results.

    • Click the question mark icon (?) next to "Tool."

    • Select the tool from the dropdown list ( e.g., C++ compiler, static analysis tool, PMD ).

    • By default, the plugin searches for files named pmd.xml. If the reports use a different filename pattern, enter it here using Ant glob syntax (e.g., */target/.xml to match all XML reports in the "target" sub-directory ).

    • Click the Save button at the bottom of the configuration page.

Compiler warnings and static analysis results

6. Build now

  • After the configuration, go back to the created jenkin job and click on Build Now on the left side panel to start the build process.

  • To check the progress of the build, go to Build History.

Build