Jenkins-Day-4
### Project: Running Maven Tests and Validation on Jenkins GUI
#### Scenario Overview
This project will guide you through setting up a Jenkins Freestyle job to run Maven tests for a Java project cloned from a GitHub repository. The tasks include configuring the job, running the Maven tests, and validating the results—all through the Jenkins GUI.
1. Create a New Jenkins Freestyle Project
- Task: Set up a new Jenkins job.
- Action:
- Navigate to the Jenkins dashboard.
- Click on `New Item`.
- Enter a project name (e.g., `AddressBook-Test`).
- Select `Freestyle project` and click `OK`.
- Explanation: This creates a new Jenkins Freestyle project where you'll configure the Maven build.
2. Configure Source Code Management
- Task: Link the project to the GitHub repository.
- Action:
- In the job configuration, scroll down to the `Source Code Management` section.
- Select `Git`.
- Enter the Repository URL: `https://github.com/preethid/addressbook.git`.
- Explanation: This step pulls the Java project from the GitHub repository.
3. Add Maven as a Build Tool
- Task: Set up Maven for the project.
- Action:
- Scroll down to the `Build` section.
- Click `Add build step` and select `Invoke top-level Maven targets`.
- In the `Goals` field, enter `test`.
- Select the appropriate Maven version from the drop-down list.
- Explanation: This step configures Jenkins to run Maven tests as part of the build process.
4. Configure Java Path Manually
- Task: Ensure the correct Java version is used.
- Action:
- Go to `Manage Jenkins` > `Tools`.
- Under the `JDK` section, ensure the correct Java version is set, or add a new JDK if necessary.
- In the job configuration, under the `Build Environment` section, select the JDK version.
- Explanation: This ensures that the job uses the correct Java version installed on your system.
5. Save and Run the Job
- Task: Execute the configured job.
- Action:
- Click `Save` to save the job configuration.
- From the project dashboard, click `Build Now` to run the job.
- Explanation: This triggers the job to clone the repository, run the Maven tests, and compile the code.
6. Validate Test Results
- Task: Review the output of the Maven tests.
- Action:
- After the build is complete, click on the build number in the `Build History` section.
- Select `Console Output` to view the results.
- Optionally, navigate to `Workspace` > `target` > `surefire-reports` to view detailed test reports.
- Explanation: This allows you to verify that the tests ran successfully and to inspect any failures or issues.
#### Summary
In this project, you created a Jenkins Freestyle job, configured it to run Maven tests for a Java project from GitHub, and validated the results—all using the Jenkins GUI. Each step included brief explanations to ensure a smooth setup and execution.
### Project: Installing Jenkins JUnit Plugin and Publishing Test Reports
#### Scenario Overview
This project will guide you through installing the JUnit plugin in Jenkins, setting up a job to run tests, and configuring post-build actions to publish the test reports.
1. Install the JUnit Plugin
- Task: Install the JUnit plugin in Jenkins.
- Action:
- Go to `Manage Jenkins` > `Manage Plugins`.
- In the `Available` tab, search for `JUnit`.
- Select the `JUnit` plugin and click `Install without restart`.
- Explanation: This step installs the JUnit plugin, enabling Jenkins to process and display JUnit test results.
2. Create a New Jenkins Freestyle Project
- Task: Set up a new Jenkins job for running tests.
- Action:
- From the Jenkins dashboard, click `New Item`.
- Enter a project name (e.g., `JUnit-Test-Project`).
- Select `Freestyle project` and click `OK`.
- Explanation: This creates a new Jenkins job where you can configure test execution and report publishing.
3. Configure Source Code Management
- Task: Link the project to a GitHub repository with JUnit tests.
- Action:
- In the job configuration, scroll down to the `Source Code Management` section.
- Select `Git` and enter the repository URL (e.g., `https://github.com/preethid/addressbook.git`).
- Explanation: This pulls the project code from GitHub, including JUnit test cases.
4. Add a Build Step to Run Maven Tests
- Task: Run the Maven test phase to execute JUnit tests.
- Action:
- Scroll down to the `Build` section.
- Click `Add build step` and select `Invoke top-level Maven targets`.
- In the `Goals` field, enter `test`.
- Explanation: This step configures Jenkins to run the JUnit tests during the build process.
5. Configure Post-Build Action to Publish JUnit Test Results
- Task: Set up Jenkins to publish JUnit test reports after the build.
- Action:
- Scroll down to the `Post-build Actions` section.
- Click `Add post-build action` and select `Publish JUnit test result report`.
- In the `Test report XMLs` field, enter `target/surefire-reports/*.xml`.
- Explanation: This action tells Jenkins to collect and publish the JUnit test results after the build completes.
6. Save and Run the Job
- Task: Execute the job to run tests and publish reports.
- Action:
- Click `Save` to save the job configuration.
- Click `Build Now` to start the job.
- Explanation: This triggers the job to clone the repository, run the tests, and publish the test results.
7. Validate the Published Test Reports
- Task: Review the test report published by Jenkins.
- Action:
- After the build is complete, click on the build number in the `Build History` section.
- Navigate to `Test Result` to view the test report summary.
- Explanation: This allows you to verify the test results and ensure they are correctly published.
### Summary
In this project, you installed the JUnit plugin in Jenkins, set up a Freestyle job to run tests, and configured post-build actions to publish the test reports. Each step included one-line explanations to guide you through the process.
### Project: Configuring Maven Package Goal in Jenkins GUI with Validation
#### Scenario Overview
This project will guide you through configuring a Jenkins Freestyle job to run the `mvn package` goal for a Java project. You'll configure the job, run the Maven package goal, and validate the output—all through the Jenkins GUI.
1. Create a New Jenkins Freestyle Project
- Task: Set up a new Jenkins job.
- Action:
- Navigate to the Jenkins dashboard.
- Click on `New Item`.
- Enter a project name (e.g., `Maven-Package-Project`).
- Select `Freestyle project` and click `OK`.
- Explanation: This creates a new Jenkins job where you can configure the Maven build.
2. Configure Source Code Management
- Task: Link the project to a GitHub repository.
- Action:
- In the job configuration, scroll down to the `Source Code Management` section.
- Select `Git`.
- Enter the Repository URL: `https://github.com/preethid/addressbook.git`.
- Explanation: This step pulls the Java project from the GitHub repository.
3. Add a Build Step to Run Maven Package
- Task: Set up the Maven package goal.
- Action:
- Scroll down to the `Build` section.
- Click `Add build step` and select `Invoke top-level Maven targets`.
- In the `Goals` field, enter `package`.
- Select the appropriate Maven version from the drop-down list.
- Explanation: This step configures Jenkins to run the Maven `package` goal, which compiles, tests, and packages the code.
4. Save and Run the Job
- Task: Execute the job to run the Maven package goal.
- Action:
- Click `Save` to save the job configuration.
- From the project dashboard, click `Build Now` to run the job.
- Explanation: This triggers the job to clone the repository, run the Maven package goal, and compile the code.
5. Validate the Package Output
- Task: Verify that the JAR file was created successfully.
- Action:
- After the build is complete, click on the build number in the `Build History` section.
- Select `Workspace` > `target` to check for the generated JAR file (e.g., `addressbook-1.0-SNAPSHOT.jar`).
- Explanation: This allows you to verify that the Maven package goal was executed successfully and the JAR file was created.
6. Inspect the Build Logs
- Task: Review the console output to ensure the packaging process completed without errors.
- Action:
- Click on the build number in the `Build History` section.
- Select `Console Output` to view the detailed build logs.
- Explanation: This helps confirm that the Maven `package` goal ran as expected and the build was successful.
### Summary
In this project, you configured a Jenkins Freestyle job to run the Maven `package` goal for a Java project from GitHub, executed the job, and validated the output—all through the Jenkins GUI. Each step included one-line explanations to guide you through the process.
### Project: Configuring Jenkins with Build Pipeline Plugin, Build Triggers, and Post-Build Actions for Compile, Unit Test, and Package
#### Scenario Overview
This project will guide you through installing the Build Pipeline plugin in Jenkins, configuring a Jenkins job to compile, test, and package a Java project using Maven, and setting up build triggers and post-build actions. You will validate each step throughout the process.
1. Install the Build Pipeline Plugin
- Task: Install the Build Pipeline plugin from the Jenkins Plugin Manager.
- Action:
- Navigate to `Manage Jenkins` > `Manage Plugins`.
- Search for `Build Pipeline` in the `Available` tab.
- Select the plugin and click `Install without restart`.
- Explanation: This step installs the Build Pipeline plugin, which allows you to create and manage build pipelines.
2. Create a New Jenkins Freestyle Project
- Task: Set up a new Jenkins job.
- Action:
- Navigate to the Jenkins dashboard.
- Click on `New Item`.
- Enter a project name (e.g., `Pipeline-Maven-Project`).
- Select `Freestyle project` and click `OK`.
- Explanation: This creates a new Jenkins job to configure Maven build steps and triggers.
3. Configure Source Code Management
- Task: Link the project to a GitHub repository.
- Action:
- In the job configuration, scroll down to the `Source Code Management` section.
- Select `Git`.
- Enter the Repository URL: `https://github.com/preethid/addressbook.git`.
- Explanation: This step pulls the Java project from the GitHub repository.
4. Add a Build Trigger
- Task: Set up a trigger to automatically build the job on SCM changes.
- Action:
- Scroll down to the `Build Triggers` section.
- Select `Poll SCM` and enter the schedule (e.g., `H/5 * * * *` for every 5 minutes).
- Explanation: This configures Jenkins to automatically trigger a build whenever there are changes in the source code repository.
5. Add a Build Step to Compile the Code
- Task: Run the Maven `compile` goal.
- Action:
- Scroll down to the `Build` section.
- Click `Add build step` and select `Invoke top-level Maven targets`.
- In the `Goals` field, enter `compile`.
- Explanation: This step configures Jenkins to compile the code as part of the build process.
6. Add a Build Step to Run Unit Tests
- Task: Run the Maven `test` goal.
- Action:
- Click `Add build step` again and select `Invoke top-level Maven targets`.
- In the `Goals` field, enter `test`.
- Explanation: This step configures Jenkins to run unit tests during the build process.
7. Add a Build Step to Package the Application
- Task: Run the Maven `package` goal.
- Action:
- Click `Add build step` again and select `Invoke top-level Maven targets`.
- In the `Goals` field, enter `package`.
- Explanation: This step configures Jenkins to package the application into a JAR file.
8. Add a Post-Build Action to Publish Test Results
- Task: Publish the results of the unit tests.
- Action:
- Scroll down to the `Post-build Actions` section.
- Click `Add post-build action` and select `Publish JUnit test result report`.
- In the `Test report XMLs` field, enter `target/surefire-reports/*.xml`.
- Explanation: This action publishes the JUnit test results after the build is completed.
9. Save and Run the Job
- Task: Execute the job to perform the Maven build steps.
- Action:
- Click `Save` to save the job configuration.
- Click `Build Now` to manually trigger the first build.
- Explanation: This triggers the job to compile, test, and package the code, and publish the test results.
10. Validate the Build Outputs
- Task: Verify that the build steps completed successfully.
- Action:
- After the build is complete, click on the build number in the `Build History` section.
- Review the `Console Output` for successful completion of `compile`, `test`, and `package` goals.
- Explanation: This allows you to confirm that each Maven goal was executed as expected.
11. Create a New Build Pipeline View
- Task: Visualize the build process using the Build Pipeline plugin.
- Action:
- From the Jenkins dashboard, click on the `+` sign to add a new view.
- Select `Build Pipeline View` and enter a name (e.g., `Maven Pipeline`).
- Configure the view to include the `Pipeline-Maven-Project` as the initial job.
- Explanation: This step creates a pipeline view that visualizes the sequence of jobs and their results.
12. Inspect the Pipeline and Validate
- Task: Review the pipeline view to ensure that all stages are represented and that the job has executed successfully.
- Action:
- Click on the `Maven Pipeline` view.
- Inspect the different stages (Compile, Unit Test, Package) for successful execution.
- Explanation: This allows you to visualize and validate the entire build process in a pipeline format.
### Summary
In this project, you installed the Build Pipeline plugin, configured a Jenkins Freestyle job to compile, test, and package a Java project using Maven, set up build triggers, and post-build actions, and validated the build process through the Jenkins GUI. The final step involved creating and inspecting a pipeline view to visualize the entire build process.
### Project: Password-less SSH Configuration Between Master and Slave
#### Objective:
To set up password-less SSH authentication between a master and slave server for the `user1` account, allowing secure and convenient access without a password prompt.
1. Switch to Root User on Both Servers
- Explanation: Ensure you have administrative privileges on both servers to perform the required tasks.
- Command:
sudo su -
2. Create `user1` on Both Master and Slave Servers
- Explanation: Create a new user account (`user1`) on both the master and slave servers.
- Command:
useradd user1
3. Set Password for `user1` on Both Servers
- Explanation: Assign a password to `user1` on both servers for initial SSH login.
- Command:
passwd user1
4. Enable `PasswordAuthentication` on Slave Server
- Explanation: Allow password-based SSH authentication on the slave server by enabling `PasswordAuthentication`.
- Commands:
vi /etc/ssh/sshd_config
- Edit the file:
- Find the line `PasswordAuthentication no` and change it to `PasswordAuthentication yes`.
- Restart the SSH Daemon:
systemctl restart sshd
5. Generate SSH Key Pair for `user1` on Master Server
- Explanation: Create a public/private SSH key pair for `user1` on the master server.
- Command:
su - user1
ssh-keygen -t rsa -b 2048
- Details:
- Accept the default file location by pressing `Enter`.
- Optionally set a passphrase or press `Enter` for no passphrase.
6. Copy the SSH Public Key to the Slave Server
- Explanation: Transfer `user1`'s public key from the master to the slave server to allow password-less login.
- Command:
ssh-copy-id -i ~/.ssh/id_rsa.pub user1@slave_ip
- Details:
- Replace `slave_ip` with the actual IP address of the slave server.
- When prompted, enter `user1`'s password on the slave to complete the key transfer.
7. Test SSH Connection from Master to Slave for `user1`
- Explanation: Verify that `user1` can SSH into the slave server without being prompted for a password.
- Command:
ssh user1@slave_ip
- Details:
- Replace `slave_ip` with the actual IP address of the slave server.
- If successful, `user1` should log in to the slave server without a password prompt.
#### Validation Steps
1. Check User Existence on Both Servers
- Command:
grep user1 /etc/passwd
2. Verify SSH Configuration on Slave
- Command:
grep PasswordAuthentication /etc/ssh/sshd_config
3. Confirm SSH Key Pair on Master
- Command:
ls -l /home/user1/.ssh/
4. Check for the Copied Public Key on Slave
- Command:
cat /home/user1/.ssh/authorized_keys
5. Test Password-less SSH Login
- Command:
ssh user1@slave_ip
This project outlines the step-by-step process of setting up password-less SSH authentication between a master and slave server. Each command is explained briefly, providing a clear and practical guide for implementation.
### Project: Prepare and Add Node as Jenkins Slave with Configuration
#### Objective
Prepare a Jenkins slave node by installing the necessary tools (Git, Java, Maven), set the environment, and add the node to Jenkins master with specified settings for executors, remote root directory, labels, SSH launch method, and availability.
#### 1. Prepare the Node as Jenkins Slave
##### 1.1 Install Git
- Task: Install Git for version control.
- Command:
sudo yum install git -y
- Explanation: Git is installed to enable code version management.
##### 1.2 Install Java
- Task: Install Java Development Kit (JDK).
- Command:
sudo amazon-linux-extras install java-openjdk11 -y
- Explanation: Java is required for Jenkins and Maven builds.
##### 1.3 Set JAVA_HOME Directory
- Task: Set the JAVA_HOME environment variable.
- Command:
echo "export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which javac)))))" | sudo tee -a /etc/profile
source /etc/profile
- Explanation: This sets and exports the Java home directory globally.
##### 1.4 Install Maven
- Task: Install Apache Maven for building projects.
- Command:
sudo yum install maven -y
- Explanation: Maven is needed for building and managing Java projects.
#### 2. Add a New Node to Jenkins Master
1. Login to Jenkins GUI
- Navigate to Manage Jenkins > Nodes > New Node.
2. Provide the following settings:
- Node Name: Enter a unique name for the new node.
- Type: Permanent Agent.
- Remote Root Directory: `/tmp` or any other Folder
- Explanation: Defines the root directory for Jenkins on the slave node.
- No. of Executors: `2` or as per your system Capacity
- Explanation: Number of parallel jobs the node can handle.
- Labels: `slave-node`
- Explanation: Labels allow Jenkins to assign specific jobs to this node.
- Usage: `Only build jobs with label expressions matching this node`
- Explanation: Restrict jobs to build only if they match the label.
- Launch Method: Launch agent via SSH.
- Host: Enter the IP address or DNS of the slave node.
- Credentials: Select pre-configured SSH credentials for the slave.
- Host Key Verification Strategy: Manually Trusted Key Verification Strategy.
- Availability: Keep this agent online as much as possible.
- Explanation: Configures the node to remain online and available for jobs.
3. Save and Launch
- Once all settings are provided, save the configuration and click on "Launch agent" to initiate the connection.
#### 3. Validation
- Task: Validate the connection by checking the node's status in Jenkins.
- Steps:
- After launching the agent, go to Manage Jenkins > Manage Nodes and Clouds.
- The newly added node should show online status.
This setup ensures the node is ready to run builds from Jenkins, with Java, Maven, and Git installed for necessary build tasks.
### Project: Add a Job on Jenkins Slave Node (Label: `slave-node`) to Run Maven Compile Job Without Poll SCM
1. Add a New Maven Compile Job on Jenkins
1. Login to Jenkins GUI
- Task: Access Jenkins dashboard.
- Explanation: Navigate to Jenkins’ web interface.
2. Create a New Job
- Task: Create a new Maven compile job.
- Steps:
- From the Dashboard, click New Item.
- Enter the Job Name: `Maven-Compile-Job-Slave`.
- Choose Freestyle Project.
- Click OK.
- Explanation: This creates a new job to compile a Maven project.
3. Configure the Job to Run on the Slave Node
- Task: Set the job to run on the node labeled `slave-node`.
- Steps:
- Under General section, check Restrict where this project can be run.
- In the Label Expression field, enter `slave-node`.
- Explanation: Ensures the job runs only on the slave node with the label `slave-node`.
2. Configure Source Code Management
1. Source Code Management Setup
- Task: Pull the source code from a Git repository.
- Steps:
- Under Source Code Management, select Git.
- In the Repository URL field, enter the Git URL: `https://github.com/preethid/addressbook.git`.
- Explanation: Configures the job to clone the project from the repository.
3. Configure Build Step for Maven Compilation
1. Set Up Maven Goals
- Task: Add the Maven compilation steps.
- Steps:
- Under the Build section, click Add build step > Invoke top-level Maven targets.
- Set Goals: `clean compile`.
- Explanation: This will run Maven `clean` and `compile` tasks, ensuring the project is compiled on the slave node.
4. Save the Job
1. Save the Job Configuration
- Task: Save the configured job.
- Steps:
- Scroll down and click Save.
- Explanation: This saves the job setup and makes it ready for execution.
5. Manually Trigger and Validate the Maven Compile Job
1. Manually Trigger the Job
- Task: Trigger the job manually.
- Steps:
- On the job's page, click Build Now.
- Explanation: This manually triggers the Maven compile job on the slave node.
2. Monitor the Build Execution
- Task: Monitor the build process on the slave node.
- Steps:
- Go to the Build History section and click on the running build.
- Check the Console Output for the Maven build steps and ensure there are no errors.
- Explanation: Verifies that the job is running as expected and compiling the Maven project successfully.
6. Validate Execution on Slave Node
1. Ensure Job Runs on the Slave Node
- Task: Check the node on which the job ran.
- Steps:
- Go to Manage Jenkins > Nodes > slave-node.
- Verify that the job ran on the `slave-node` by checking the Console Output.
- Explanation: This ensures the Maven compile job is being executed on the correct slave node.
By following this sequence, you can configure and manually run a Maven compile job on a slave node without using Poll SCM. The job will be manually triggered and compiled on the node labeled `slave-node`.