1. Update Your System
Open your terminal and ensure your package list is updated and upgrades are applied:
sudo apt update
sudo apt upgrade -y
2. Add the Jenkins Repository Key
Download and add the GPG key for the Jenkins repository:
sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
## 3. Add the Jenkins Repository to Your Sources List
Add the Jenkins stable repository to your system's software sources:
bash
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
---
## 4. Update Your Package List and Install Jenkins
Update your package list again to include the Jenkins repository, then install Jenkins:
bash
sudo apt update
sudo apt install jenkins -y
5. Start and Enable the Jenkins Service
Start the Jenkins service and configure it to launch automatically on system boot:
sudo systemctl start jenkins
sudo systemctl enable jenkins
Check the status to confirm it's running:
sudo systemctl status jenkins
## 6. Access Jenkins and Complete Initial Setup
1. Open a web browser and navigate to the Jenkins default port:
[localhost:8080](http://localhost:8080)
2. You will be prompted to unlock Jenkins. Retrieve the initial administrator password by running the following command in your terminal:
bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
3. Copy the displayed password and paste it into the "Administrator password" field in your browser. Click "Continue".
4. On the next screen, click "Install suggested plugins" to install a recommended set of plugins.
5. After the plugins are installed, you will be asked to create an administrator user. Fill in the required details and click "Save and Finish".
6. You should now see the Jenkins dashboard, ready for use.
## 1. Prepare Your Project on GitHub
Ensure your Java project (either Maven or Gradle) is hosted on GitHub. For this experiment, we will use the following public example repositories:
- *Maven Project:* [https://github.com/devops-ds/your-maven-project.git](https://github.com/devops-ds/your-maven-project.git)
- *Gradle Project:* [https://github.com/devops-ds/your-gradle-project.git](https://github.com/devops-ds/your-gradle-project.git)
*Note:* You are welcome to use your own GitHub repository if you prefer. If you use the links provided above, you will not need to configure credentials as they are public repositories, and the default branch to build is main.
Make sure your project has a build file (pom.xml for Maven or build.gradle/build.gradle.kts for Gradle) that can compile your code and generate JUnit test reports (typically XML files).
---
## 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.
## 3. Create a New Jenkins Job (Freestyle Project)
1. On the Jenkins dashboard, click "New Item" (or "Create a job" if it's a fresh installation).
2. Enter an item name (e.g., Maven-GitHub-Freestyle or Gradle-GitHub-Freestyle).
3. Select "Freestyle project".
4. Click "OK".
## 4. Configure Source Code Management (SCM) for Freestyle
In the job configuration page for your Freestyle project:
1. Scroll down to the "Source Code Management" section.
2. Select "Git".
3. In the "Repository URL" field, enter the URL for your chosen GitHub repository (either one of the examples provided or your own).
- For Maven: https://github.com/devops-ds/your-maven-project.git
- For Gradle: https://github.com/devops-ds/your-gradle-project.git
4. 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.
5. Under "Branches to build", ensure */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:
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:
Scroll down to the "Build" section.
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
- You can leave other options as default for a basic setup.
## 7. Save the Freestyle Job Configuration
Scroll to the bottom of the page and click "Save".
---
## 8. Run the Freestyle Jenkins Job
1. You will be redirected to the job's dashboard page.
2. 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.
## 9. Monitor the Freestyle Build and View Results
1. Under "Build History" on the left, you will see a new build appear with a progress bar.
2. Click on the build number once it starts.
3. Click "Console Output" on the left to view the live build logs.
4. 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.
1. On the Jenkins dashboard, click "New Item".
2. Enter an item name (e.g., Maven-GitHub-Pipeline or Gradle-GitHub-Pipeline).
3. Select "Pipeline".
4. Click "OK".
## 11. Configure the Pipeline Script
In the job configuration page for your Pipeline project:
1. Scroll down to the "Pipeline" section.
2. Under "Definition", select *"Pipeline script"*. This allows you to write or paste the Pipeline script directly into the Jenkins job configuration.
3. A text area labeled "Script" will appear. This is where you will paste your Groovy Pipeline script.
---
## 12. Example Pipeline Scripts to Paste Directly
Here are basic examples of Groovy Pipeline scripts for Maven and Gradle projects that you can paste directly into the "Script" text area in your Jenkins job configuration. These scripts define stages for checking out code, building, testing, and publishing results.
- *Example Pipeline Script for a Maven Project:*
pipeline {
agent any // Or specify a specific agent label
stages {
stage('Checkout') {
steps {
// Checkout code from your GitHub repository
git url: 'https://github.com/devops-ds/your-maven-project.git', branch: 'main' // Replace with your repo URL if different, adjust branch name if needed
// If your repository is private, you'll need to add credentials here, e.g.:
// git url: 'https://github.com/devops-ds/your-maven-project.git', branch: 'main', credentialsId: 'your-credential-id'
}
}
stage('Build') {
steps {
// Execute Maven build using the 'mvn' command with the full path
// To find the full path to your 'mvn' executable on Linux/macOS, open your terminal and run:
// which mvn
sh '/usr/bin/mvn clean package' // Replace with actual path
}
}
stage('Test') {
steps {
// Optionally, separate test execution if needed
// This step is often not strictly necessary if 'mvn clean package' already runs tests
// sh '/path/to/your/maven/bin/mvn test' // Replace with actual path
echo 'Tests are typically run during the Build stage with Maven.'
}
}
}
post {
always {
// Archive test reports
junit '/target/surefire-reports/*.xml'
}
success {
echo 'Build and tests succeeded!'
}
failure {
echo 'Build or tests failed.'
}
// Add other post conditions like 'unstable', 'changed' as needed
}
}
Remember to replace the placeholder paths for mvn with the actual full path on your Jenkins agent if you are using the Maven example.
Example Pipeline Script for a Gradle Project:
pipeline {
agent any // Or specify a specific agent label
stages {
stage('Checkout') {
steps {
// Checkout code from your GitHub repository
git url: 'https://github.com/devops-ds/your-gradle-project.git', branch: 'main' // Corrected URL
// If your repository is private, you'll need to add credentials here, e.g.:
// git url: 'https://github.com/devops-ds/your-gradle-project.git', branch: 'main', credentialsId: 'your-credential-id'
}
}
stage('Build') {
steps {
// Run Gradle build using the wrapper
sh './gradlew clean build'
}
}
stage('Test') {
steps {
// Run tests (if not already run in the build stage)
// This step is often not strictly necessary if './gradlew clean build' already runs tests
sh './gradlew test'
}
}
}
post {
always {
// Archive test reports (modify the path according to your project structure)
junit '/build/test-results/test/*.xml'
}
success {
echo 'Build and tests succeeded!'
}
failure {
echo 'Build or tests failed.'
}
// Add other post conditions like 'unstable', 'changed' as needed
}
}
## 13. Save the Pipeline Job Configuration
Scroll to the bottom of the page and click "Save".
---
## 14. Run the Pipeline Jenkins Job
1. You will be redirected to the job's dashboard page.
2. 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
1. Under "Build History" on the left, you will see a new build appear.
2. Click on the build number.
3. You will see a visual representation of the pipeline stages. Click on a stage to see its details.
4. Click "Console Output" on the left to view the logs for the entire pipeline run.
5. 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