4
sudo apt update
sudo apt upgrade -y
sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the displayed password and paste it into the "Administrator password" field in your browser. Click "Continue".
On the next screen, click "Install suggested plugins" to install a recommended set of plugins.
After the plugins are installed, you will be asked to create an administrator user. Fill in the required details and click "Save and Finish".
You should now see the Jenkins dashboard, ready for use.
- Maven Project:
https://github.com/devops-ds/your-maven-project.git
- Gradle Project:
https://github.com/devops-ds/your-gradle-project.git
2. Log in to Jenkins
Open your web browser and go to your Jenkins instance (e.g., localhost:8080
). Log in with the administrator credentials you created during the initial setup.
Create a New Jenkins Job (Freestyle Project)
- On the Jenkins dashboard, click "New Item" (or "Create a job" if it's a fresh installation).
- Enter an item name (e.g.,
Maven-GitHub-Freestyle
orGradle-GitHub-Freestyle
). - Select "Freestyle project".
- Click "OK".
Configure Source Code Management (SCM) for Freestyle
In the job configuration page for your Freestyle project:
- Scroll down to the "Source Code Management" section.
- Select "Git".
- In the "Repository URL" field, enter the URL
- For Maven:
https://github.com/devops-ds/your-maven-project.git
- For Gradle:
https://github.com/devops-ds/your-gradle-project.git
- For Maven:
- If you are using your own private repository, you will need to add credentials. Click "Add" next to "Credentials" and provide your GitHub username and password or a personal access token. If using the provided public repositories, no credentials are needed.
*/main
is specified. If your repository uses a different default branch (like */master
), adjust this accordingly. For the provided public repositories, */main
is correct.5. Configure the Build Step for Freestyle
This is where you tell Jenkins how to build your project using Maven or Gradle.
If you are building a Maven Project:Scroll down to the "Build" section.Click "Add build step".
Select "Execute shell" (or "Execute Windows batch command" on Windows).
In the "Command" area, enter the full path to your Maven executable followed by the goals you want to run (e.g., clean install
).
To find the full path to your mvn
executable on open terminal in Ubuntu and run:
5. Configure the Build Step for Freestyle
This is where you tell Jenkins how to build your project using Maven or Gradle.
- If you are building a Maven Project:
Click "Add build step".
Select "Execute shell" (or "Execute Windows batch command" on Windows).
In the "Command" area, enter the full path to your Maven executable followed by the goals you want to run (e.g., clean install
).
To find the full path to your mvn
executable on open terminal in Ubuntu and run: which mvn
The output will give you the full path (e.g., /usr/bin/mvn or /opt/maven/bin/mvn). Use this path in the command below. #/path/to/your/maven/bin/mvn clean install
# Example:
/usr/bin/mvn clean install
- If you are building a Gradle Project:
Click "Add build step".
Select "Execute shell".
In the "Command" area, enter the full path to your Gradle executable followed by the tasks you want to run (e.g., build
).
To find the full path to your gradle
executable on open terminal in Ubuntu and run: which gradle
The output will give you the full path (e.g., /usr/local/bin/gradle or /opt/gradle/bin/gradle). Use this path in the command below.
#/path/to/your/gradle/bin/gradle build
# Example using the path provided:
/opt/gradle/gradle-8.13/bin/gradle build
6. Configure Post-Build Actions for Freestyle
To publish the JUnit test reports generated by your build tool so they are visible in the Jenkins UI, follow these steps:
- Scroll down to the "Post-build Actions" section.
- Click "Add post-build action".
- Select "Publish JUnit test result report".
- Test report XMLs: Enter the path to the test report XML files generated by your build tool.
- For Maven Surefire/Failsafe:
**/target/surefire-reports/*.xml
- For Gradle:
**/test-results/**/*.xml
- For Maven Surefire/Failsafe:
- You can leave other options as default for a basic setup
- Test report XMLs: Enter the path to the test report XML files generated by your build tool.
7. Save the Freestyle Job Configuration
Scroll to the bottom of the page and click "Save".
8. Run the Freestyle Jenkins Job
- You will be redirected to the job's dashboard page.
- On the left-hand menu, click "Build Now".
Jenkins will now clone your repository, execute the build command, and then process the test report XML files.
Monitor the Freestyle Build and View Results
- Under "Build History" on the left, you will see a new build appear with a progress bar.
- Click on the build number once it starts.
- Click "Console Output" on the left to view the live build logs.
- After the build completes, you should see a "Test Result" link on the build's page if test reports were found and published. Click this link to see a summary of your test results.
10. Create a New Jenkins Job (Pipeline)
Pipeline is a more modern approach using Groovy scripts to define your build process.
- On the Jenkins dashboard, click "New Item".
- Enter an item name (e.g.,
Maven-GitHub-Pipeline
orGradle-GitHub-Pipeline
). - Select "Pipeline".
- Click "OK
11. Configure the Pipeline Script
In the job configuration page for your Pipeline project:
- Scroll down to the "Pipeline" section.
- Under "Definition", select "Pipeline script". This allows you to write or paste the Pipeline script directly into the Jenkins job configuration.
- A text area labeled "Script" will appear. This is where you will paste your Groovy Pipeline script.
12. Example Pipeline Scripts to Paste Directly
13. Save the Pipeline Job Configuration
Scroll to the bottom of the page and click "Save".
14. Run the Pipeline Jenkins Job
- You will be redirected to the job's dashboard page.
- On the left-hand menu, click "Build Now".
Jenkins will now execute the stages defined in the Pipeline script you pasted.
15. Monitor the Pipeline Build and View Results
- Under "Build History" on the left, you will see a new build appear.
- Click on the build number.
- You will see a visual representation of the pipeline stages. Click on a stage to see its details.
- Click "Console Output" on the left to view the logs for the entire pipeline run.
- Similar to the Freestyle job, if test reports are published, you will see a "Test Result" link on the build's page.
This covers setting up both Freestyle and basic Pipeline jobs in Jenkins to build Java projects from GitHub and publish test results.
Comments
Post a Comment