Continuous Integration Work- shop with Jenkins and Git



install jdk
sudo dnf install java-17-amazon-corretto -y

enable Jenkins to start automatically once server started ok.


add inbound port 8080 for Jenkins:


then you can just access the Jenkins UI.
http://your_hostname:8080

Run the sudo command to display the contents of /var/lib/jenkins/secrets/initialAdminPassword after that, you will be prompted to enter the password.

create user
install suggest plugin
Create First Admin User

go to manage Jenkins, then Plugins

install maven
manage Jenkins, then Tools

initiate a springboot project

import to IDEA:

add java facker and springboot web
com.github.javafaker
javafaker
1.0.2
org.springframework.boot spring-boot-starter-web 3.2.9 add API:
add application configuration


test the API using Postman. If the application isn't installed, download it initially from Google’s website.

create a new project at github.com

配置GitHub Actions 以便更好地了解关于GitHub Actions的信息。你可能有兴趣访问“https://docs.github.com/en/actions/learn-github-actions/understanding-github-
actions”
配置GitHub Actions 以便更好地了解关于GitHub Actions的信息。你可能有兴趣访问“https://docs.github.com/en/actions/learn-github-actions/understanding-github-
actions”

add yml file in the project folder
.github/workflow/master.yml

maven.yml:
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Java CI with Maven
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml


add Jacoco plugin to make sure the code coverage
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

Integrate SonarCloud in CI pipeline
login sonarcloud

carry out a comprehensive analysis of a new project, and then adhere to the necessary procedures. choose the repository accordingly.

Create an organization

create a project manually


create project

with Github Actions


go to github page.


add code part to your Springboot pom file.
<properties>
<java.version>17</java.version>
<sonar.organization>allenwu-zhiwei</sonar.organization>
<sonar.host.url>https://sonarcloud.io/</sonar.host.url>
</properties>
update permission on github

add .github/workflows/sonar.xml
name: SonarCloud
on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build and analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=allenwu-zhiwei_cicd-demo
bash

after scanned, you will see below page.

