Devops Lab Exercises Preeti Dovala
Devops Lab Exercises Preeti Dovala
GIT - Day-1
Sep-by-step guide to setting up Git on Windows, Mac, and an Amazon Linux 2
EC2 instance, along with a sample project to practice with.
#### On Windows
1. Download Git for Windows: Go to the [Git for Windows download page]
(https://git-scm.com/download/win) and download the installer.
2. Run the installer: Follow the installation prompts, accepting the default
settings. This will also install Git Bash, a command-line interface for Git.
#### On Mac
1. Install Xcode Command Line Tools: Open the Terminal and run:
xcode-select --install
Configure Git with your user name and email address. This is important for
tracking changes.
mkdir sample-git-project
cd sample-git-project
git init
#### Step 3.2: Create a README File
git status
git commit -m "Initial commit with README"
git status
### Summary
This guide walks you through installing Git on various operating systems,
configuring it, creating a sample project. Practicing these steps will help you get
comfortable with the basic Git workflow.
1. Create a new directory for your project and navigate into it:
mkdir my-git-practice
cd my-git-practice
git init
ls -la
*Explanation*: `ls -la` lists all files and directories, including hidden ones
(those starting with a dot), in the current directory. You'll see the `.git` directory
here, indicating that this is a Git repository.
touch README.md
*Explanation*: `echo` outputs the string to the terminal. Using `>` redirects
the output to the file `README.md`, overwriting its content if it already exists.
3. Check the status of your repository:
git status
*Explanation*: `git status` displays the state of the working directory and the
staging area. It shows which changes have been staged, which haven't, and
which files aren't being tracked by Git.
*Explanation*: `git add` adds changes in the working directory to the staging
area, preparing them to be included in the next commit.
git ls-files
*Explanation*: `git ls-files` lists all the files that are being tracked by Git.
2. View the commit history:
git log
*Explanation*: `git log` shows the commit history for the repository. By
default, it displays the commits in reverse chronological order.
- Untracked Files: These are files in your working directory that are not being
tracked by Git. They are new files that Git has not been told to keep track of yet.
When you create a new file in a Git repository, it starts as untracked.
- Unstaged Files: These are files that are tracked by Git and have been modified
but have not yet been added to the staging area using `git add`. Git knows about
these files and their changes, but they are not marked for inclusion in the next
commit.
1. Create a new directory for your project and navigate into it:
mkdir git-practice
cd git-practice
git init
git status
git status
You will see `hello.txt` listed under "Changes not staged for commit". This
means the file is unstaged.
git status
You will see `hello.txt` under "Changes not staged for commit" and
`newfile.txt` under "Untracked files".
git status
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
Home
GIT - Day-2
QUIZ
sample project to practice using `git diff` and `git diff --staged` commands.
1. Create a new directory for your project and navigate into it:
mkdir git-diff-practice
cd git-diff-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
### Step 4: Stage the Changes and Use `git diff --staged`
2. View the differences between the staging area and the last commit:
git diff --staged
*Explanation*: `git diff --staged` shows the changes between the staging area
and the last commit. This will display the changes that have been staged for the
next commit.
By following these steps, you'll gain hands-on experience with `git diff` and `git
diff --staged`, helping you understand how to track and compare changes in
your Git repository.
sample project to practice using `git rm`, and `git rm --cached` commands.
1. Create a new directory for your project and navigate into it:
mkdir git-rm-practice
cd git-rm-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create a couple of new files:
echo "File 1 content" > file1.txt
echo "File 2 content" > file2.txt
2. Stage and commit the files:
git add file1.txt file2.txt
git commit -m "Initial commit with file1.txt and file2.txt"
### Step 3: Practice Using `git rm`
1. Remove `file1.txt` from both the working directory and the staging area:
git rm file1.txt
2. Check the status to see that `file1.txt` is staged for removal:
git status
3. Commit the change:
git commit -m "Remove file1.txt"
4. List the files tracked by Git:
git ls-files
*Explanation*: `git ls-files` lists all the files that are being tracked by Git.
By following these steps, you'll gain hands-on experience with `git rm`, `git ls-
files`, and `git rm --cached`, helping you understand how to manage files in your
Git repository.
sample project to practice using the `.gitignore` file in Git.
1. Create a new directory for your project and navigate into it:
mkdir git-ignore-practice
cd git-ignore-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create new files and directories that match the patterns in `.gitignore`:
echo "Another log file" > anotherfile.log
mkdir temp2
echo "Temporary data 2" > temp2/tempfile2.txt
- `.gitignore`: A file used to tell Git which files or directories to ignore. The
patterns in the `.gitignore` file follow specific rules to match files and
directories.
- `git status`: Displays the state of the working directory and the staging area,
showing which changes are staged, unstaged, or untracked.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `touch <file>`: Creates an empty file.
By following these steps, you'll gain hands-on experience with creating and
using a `.gitignore` file, helping you manage which files and directories Git tracks
in your repository.
sample project to practice using `git revert`, `git log`, and `git show` commands.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-revert-practice
cd git-revert-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
By following these steps, you'll gain hands-on experience with `git revert`, `git
log`, and `git show`, helping you understand how to manage and inspect
commits in your Git repository.
project to practice using `git reset` with its different options: `--hard`, `--soft`,
and `--mixed`.
1. Create a new directory for your project and navigate into it:
mkdir git-reset-practice
cd git-reset-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
By following these steps, you'll gain hands-on experience with `git reset` in its
different modes (`--hard`, `--soft`, and `--mixed`), helping you understand how
to manage commits and changes in your Git repository.
explore the differences between `git revert` and `git reset` with examples.
`git revert` creates a new commit that undoes the changes from a previous
commit. It doesn't change the commit history but adds a new commit to reverse
the specified changes.
#### Example:
`git reset` changes the current branch's commit history and can modify the
staging area and working directory based on the mode used (`--soft`, `--mixed`,
`--hard`).
#### Example:
- Soft Reset:
git reset --soft HEAD~2
git log --oneline
git status
The `git reset --soft` command will move the HEAD pointer to the specified
commit but keep all changes staged. The commit history will be rewritten, but
no changes are lost.
- Mixed Reset:
git reset --mixed HEAD~1
git log --oneline
git status
The `git reset --mixed` command will move the HEAD pointer to the specified
commit and unstage all changes. The commit history will be rewritten, and the
changes will be in the working directory but not staged.
- Hard Reset:
git reset --hard HEAD~1
git log --oneline
git status
The `git reset --hard` command will move the HEAD pointer to the specified
commit and discard all changes. The commit history will be rewritten, and all
changes from the reset commit will be lost.
### Summary of Differences
- `git revert`:
- Creates a new commit that undoes changes from a specified commit.
- Keeps the commit history linear and intact.
- Useful for undoing changes in a collaborative environment without rewriting
history.
- `git reset`:
- Moves the HEAD pointer to a specified commit and optionally modifies the
staging area and working directory.
- Can rewrite commit history and potentially discard changes.
- Useful for undoing commits in a private branch or before sharing with others.
- Modes:
- `--soft`: Keeps changes staged.
- `--mixed`: Keeps changes in the working directory.
- `--hard`: Discards all changes.
By following these examples, you'll gain a clear understanding of how to use `git
revert` and `git reset` in different scenarios.
project to practice using `git branch` and `git checkout` & git merge.
1. Create a new directory for your project and navigate into it:
mkdir git-branch-checkout-practice
cd git-branch-checkout-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
3. Merge branches:
- `git merge <branch-name>`: Merges the specified branch into the current
branch.
By following these steps, you'll gain hands-on experience with `git branch` and
`git checkout`, helping you manage branches effectively in your Git repository.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
Home
GIT - Day-3
QUIZ
1. Create a new directory for your project and navigate into it:
mkdir git-conflict-practice
cd git-conflict-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
4. Git will show a message indicating there are conflicts. Open `file.txt` to see
the conflict markers:
cat file.txt
The content will look something like this:
txt
Initial content
Changes in branch1
<<<<<<< HEAD
=======
Conflicting changes in branch2
>>>>>>> branch2
5. Resolve the conflict by editing the file. You can manually choose how to
combine the changes:
txt
Initial content
Changes in branch1
Conflicting changes in branch2
6. Stage the resolved file:
git add file.txt
7. Commit the merge:
git commit -m "Resolve merge conflict between branch1 and branch2"
### Step 6: Verify the Merge
You should see a merge commit with the message you provided.
In this sample project, you'll work through a scenario where you're developing a
new feature but need to quickly fix a bug in the main branch. You'll use Git stash
to save your work on the feature, switch to the main branch to fix the bug, and
then return to your feature development.
1. Create a new directory for your project and navigate into it:
mkdir git-stash-scenario
cd git-stash-scenario
2. Initialize a Git repository:
git init
3. Create and commit initial files:
echo "Initial content" > main.txt
git add main.txt
git commit -m "Initial commit with main.txt"
### Step 2: Start Developing a New Feature
project that helps you practice the git rebase command with a realistic scenario.
1. Create a new directory for your project and navigate into it:
mkdir git-rebase-practice
cd git-rebase-practice
2. Initialize a Git repository:
git init
3. Create and commit initial files:
echo "Initial content" > main.txt
git add main.txt
git commit -m "Initial commit with main.txt"
### Step 2: Create and Switch Branches
1. If there are any conflicts during the rebase, Git will pause and prompt you to
resolve them. For example, if both branches modified the same part of a file,
you'll see conflict markers in the file:
cat main.txt
The content might look like this:
txt
Initial content
Main branch work 1
<<<<<<< HEAD
Main branch work 2
=======
Feature work 2
>>>>>>> feature-branch
2. Resolve the conflict manually by editing the file:
txt
Initial content
Main branch work 1
Main branch work 2
Feature work 2
3. Stage the resolved file:
git add main.txt
4. Continue the rebase:
git rebase --continue
5. Repeat steps 1-4 for any additional conflicts.
By following these steps, you'll gain hands-on experience with the `git rebase`
command and understand how it helps create a cleaner, linear commit history.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
Home
GIT - Day-4
QUIZ
---
---
---
---
2. Verify on GitHub:
- Go to your GitHub repository page and ensure that the `main` branch is visible
and contains the committed files.
---
mkdir sample-multi-branch-project
cd sample-multi-branch-project
- Initialize a Git repository:
git init
2. Create and Commit an Initial File:
- Create a README file:
git add .
git commit -m "Merge feature-a into dev"
2. Merge `feature-b` into `dev`:
- Merge `feature-b` into `dev`:
git add .
git commit -m "Merge feature-b into dev"
3. Push the Updated `dev` Branch to GitHub:
- Push the merged changes:
git branch -r
- You should see `origin/main`, `origin/dev`, `origin/feature-a`, and
`origin/feature-b`.
git add .
git commit -m "Merge dev into main"
2. Push the Final `main` Branch to GitHub:
- Push the `main` branch with merged changes:
### Sample Project for Practicing `git clone`, `git pull`, and `git fetch`
#### Overview
In this project, you'll simulate a collaborative workflow where multiple team
members work on a shared GitHub repository. You'll practice cloning a
repository, fetching updates, and pulling the latest changes from the remote
repository.
---
1. `git clone`:
- What it does: `git clone` is used to create a copy of an existing Git repository
from a remote server (like GitHub) to your local machine. It copies all the files,
branches, and commit history from the remote repository.
- Scenario: You join a new project, and you need to set up the project on your
local machine by cloning the repository from GitHub.
2. `git pull`:
- What it does: `git pull` is a combination of `git fetch` and `git merge`. It fetches
changes from the remote repository and immediately tries to merge them into
your current branch. If there are any conflicts, you need to resolve them before
completing the merge.
- Scenario: You’ve been working on a project, but before you start new work,
you need to pull the latest changes from the remote repository to ensure you’re
up to date.
3. `git fetch`:
- What it does: `git fetch` retrieves updates from the remote repository but does
not automatically merge them into your working branch. It updates your local
copy of the remote branches, allowing you to review changes before merging.
- Scenario: You want to see what changes have been made in the remote
repository without immediately merging them into your local branch.
---
### Project: Handling a File Conflict Between Local and Remote Repositories
#### Overview
In this project, you'll simulate a scenario where a file with the same name but
different content is created in both your local repository and the remote
repository. You'll then attempt to push your local changes to the remote
repository, which will result in a conflict. This scenario will help you practice
resolving conflicts and understanding how Git handles these situations.
This scenario helps you understand how Git handles conflicts when the same file
has different content in the local and remote repositories. By practicing this, you
can get comfortable with resolving conflicts in real-world situations.
#### Overview
This project will guide you through a scenario where you create a file with the
same name but different content in both your local repository and the remote
repository. You will then use `git pull --rebase` to integrate the changes from the
remote repository, which will result in a conflict. This scenario will help you
practice resolving conflicts during a rebase.
This scenario helps you understand how to handle conflicts during a rebase
using `git pull --rebase`, giving you practical experience with one of the more
complex aspects of Git.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
Home
GIT - Day-5
QUIZ
### Scenario 1: Practicing Pull Requests
#### Overview
In this scenario, you'll work on a shared repository and create a pull request to
merge your changes into the main branch. This is a common workflow in
collaborative projects.
#### Overview
In this scenario, you'll fork a public repository, make changes in your fork, and
then create a pull request to suggest your changes to the original repository.
1. Fork a Public Repository on GitHub:
- Find a public repository you want to contribute to (you can use any public
repository for practice).
- On the repository's GitHub page, click the "Fork" button at the top-right corner
to create a fork under your GitHub account.
These scenarios will help you practice and understand the workflows for
creating pull requests and working with forks in GitHub.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Back
Linux -Day-1
### Project: Basic Linux- Command Line Practice
#### Scenario:
You have just logged into a new Linux server as a regular user. Your task is to
navigate the file system, manage directories and files, gather system
information, and understand basic user management concepts. This project will
guide you through a sequence of commands that are essential for everyday
tasks in a Linux environment.
### Outcome:
By the end of this project, you will have practiced essential Linux commands in a
logical sequence, gaining a basic understanding of file system navigation, file
manipulation, system information retrieval, and user management. You will
also learn about the differences between root and normal users, and how to
switch to the root user when necessary.
#### Scenario:
You are setting up a development environment on a CentOS-based Linux server.
As part of this setup, you need to manage software packages using the YUM
package manager. Specifically, you'll be installing and configuring Git, a popular
version control system. This project will guide you through installing, updating,
and managing software packages with YUM, using Git as the primary example.
- Objective: Ensure that your system packages and YUM repositories are up to
date.
- Command:
sudo yum update -y
- Explanation: This command updates all installed packages to their latest
versions and refreshes the package index.
- Objective: Reinstall Git to ensure you can easily manage packages with YUM.
- Command:
sudo yum install git -y
- Explanation: Reinstalls Git, showing how easy it is to manage software
packages with YUM.
### Outcome:
By the end of this project, you will have gained practical experience in using
YUM to manage software packages on a CentOS-based system. You'll be able to
update your system, install and remove software packages like Git, manage
repositories, and verify installations. This knowledge is fundamental for
maintaining a well-functioning Linux environment.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Back
Linux-Day-2
### Project: Managing Users and Groups in Linux
#### Scenario:
You are the system administrator responsible for managing user accounts and
groups on a Linux server. This project will guide you through a series of tasks
related to user and group management. You will add new users, modify their
group memberships, and ensure proper group management.
### Outcome:
By the end of this project, you will have practical experience in adding users,
managing their group memberships, and ensuring proper group management
on a Linux system. You will be able to verify these changes by checking the
relevant system files, such as `/etc/passwd` and `/etc/group`.
### Project: User, Group, File, and Folder Management with Permissions in
Linux
#### Scenario:
You are setting up a new project on a Linux server and need to manage users,
groups, files, and folders. You will create users and groups, set up files and
folders, manage permissions, and assign ownership to ensure that only
authorized users can access and modify the resources.
5. Change Permission to Allow Others to Edit the File and Check the Permissions
- Objective: Modify the file permissions to allow others to edit `report.txt` and
verify the change.
- Commands:
sudo chmod o+w project/report.txt
ls -l project/report.txt
- Explanation: The permission should now include `w` for Others, i.e., `-rw-r--rw-
`.
Here is some example content you can use in `sample.log`:(Copy and paste)
### Project: Install and Setting Up an HTTP Server on EC2 with `httpd`
#### Scenario:
You are tasked with setting up a basic HTTP web server on an AWS EC2 instance.
You'll install the `httpd` package, configure the server to listen on port 80,
update security group rules to allow traffic on this port, host a simple HTML
page, and later change the listening port to 90.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Back
Linux-Day-3
### Basic Concepts of UNIX Shell Scripting
- Redirecting Output:
- `>`: Redirects output to a file (overwrites if file exists).
- `>>`: Appends output to a file.
- Example:
echo "This is a test" > testfile.txt
echo "Appending text" >> testfile.txt
#### 9. Script Arguments
- You can pass arguments to your script, which can be accessed using `$1`, `$2`,
etc.
- Example:
#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"
- Run the script with arguments:
./scriptname.sh arg1 arg2
#### 10. Exit Status
- Every command returns an exit status (`0` for success, non-zero for failure).
- Example:
#!/bin/bash
ls /nonexistent
echo "Exit status: $?"
#!/bin/bash
#!/bin/bash
Conditional Logic:
- Uses `if`, `elif`, and `else` to check if a number is positive, negative, or zero.
- The script reads a number from the user and compares it using `-gt` (greater
than) and `-lt` (less than)
#### 3. Create a Script to Back Up Files
#!/bin/bash
File Backup:
- Uses `cp` to copy all `.txt` files from the source directory to the backup
directory.
- Make sure to replace `/path/to/source_directory` and
`/path/to/backup_directory` with actual paths.
#!/bin/bash
#!/bin/bash
Greeting Function:
- Defines a function `greet_user` that takes a name as an argument and prints a
greeting.
- The function is then called with "Alice" as the argument.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Jenkins Home
Jenkins Day -1
QUIZ
step-by-step guide for setting up Jenkins on an EC2 instance running Amazon Linux
2 and performing various tasks:
3. Install Jenkins:
- Update the package index:
- Install Jenkins:
echo $JENKINS_HOME
```
- Default location: `/var/lib/jenkins`
### Step 5: Post-Install Task
1. Unlock Jenkins:
- Get the initial admin password:
1. Locate Workspace:
- The workspace for the job can be found at:
ls -l /var/lib/jenkins/jobs/
```
http://<your-ec2-public-ip>:8081
### Summary
1. Log in to Jenkins:
- Open your web browser and go to your Jenkins instance (e.g.,
`http://<your-ec2-public-ip>:8080`).
3. Go to Manage Users:
- Click on "Manage Users."
3. Enable Security:
- Check "Enable security."
1. Log out:
- Log out of Jenkins to test the new user permissions.
### Summary
project for practicing matrix-based authorization for a regular user, follow these
steps:
Steps:
Steps:
While still logged in as regular user, go to the SampleProject.
Click "Build Now" to start a build.
Wait for the build to complete.
In the "Build History," click on the build number.
Ensure that regular user can view the "Console Output" and see the
message Hello, Jenkins!.
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Jenkins Home
Jenkins-Day-2
QUIZ
### Project Scenario: Configuring Jenkins with Role-Based
Authorization with Global Roles and Validating with a Sample Job
1. Create Users:
- Go to `Manage Jenkins` > `Manage Users`.
- Create the following users:
- admin_user: Administrator with full control.
- dev_user1 and dev_user2: Developers who will manage and run
jobs.
- viewer_user: A user with read-only access.
Scenario: Validate that the roles and permissions are set correctly
by testing with each user account.
### Conclusion
1. Create Users:
- Go to `Manage Jenkins` > `Manage Users`.
- Create the following users:
- pm_user: A project manager responsible for managing specific
projects.
- dev_user1 and dev_user2: Developers responsible for working on
specific projects.
### Conclusion
This project scenario ensures that Jenkins is set up with item roles
tailored to the needs of your team. By following these steps, you
can effectively manage permissions and validate the setup using a
sample job, ensuring a secure and well-organized Jenkins
environment.
1. Create Users:
- Go to `Manage Jenkins` > `Manage Users`.
- Create the following users:
- admin_user: Administrator with full control over all projects and
settings.
- pm_user: Project Manager with control over specific projects.
- dev_user1 and dev_user2: Developers with limited permissions to
work on specific projects.
Scenario: Validate that the users have the correct permissions and
that the job builds according to the schedule.
### Conclusion
1. Configure SCM:
- On the project configuration page, scroll down to the `Source Code
Management` section.
- Select the appropriate SCM system (e.g., `Git`).
- Enter the repository URL (e.g.,
`https://github.com/your-repo/sample-project.git`).
- Add credentials if necessary for accessing the repository.
- Specify the branch to build (e.g., `*/main`).
Scenario: Save the project configuration and ensure that the job
builds when changes are detected in the SCM.
#### Scenario
In this project, you'll create a new GitHub repository, clone it to an
EC2 instance, create a file locally, and push the file to the remote
repository on GitHub. This scenario simulates a typical workflow for
managing code using Git and GitHub on a cloud server.
#### 1. Create a GitHub Repository
- Go to GitHub: Log in to your GitHub account.
- Create a New Repository:
- Click on the "New" button to create a new repository.
- Enter a repository name, for example, `ec2-git-demo`.
- You can add a description if you like.
- Choose "Public" or "Private" based on your preference.
- Do not initialize the repository with a README, `.gitignore`, or
license for this scenario.
- Click "Create repository".
- Wait for the next polling interval (e.g., 5 minutes) and check if
Jenkins detects the change and automatically triggers a build.
- Webhook:
- A webhook is a push mechanism where the source code repository notifies
Jenkins immediately whenever a change occurs.
- The repository sends an HTTP POST request to Jenkins with details about
the change (e.g., a commit).
- Jenkins then triggers a build based on this notification.
- Webhook:
- Webhooks are more efficient because they only trigger Jenkins when there
is a change in the repository.
- No continuous checking is required, reducing the load on both Jenkins and
the repository.
- Webhook:
- Real-time. Jenkins responds almost immediately after a change is pushed to
the repository.
- This is ideal for scenarios where fast feedback is critical.
- Webhook:
- Requires configuring the repository to send webhook notifications to
Jenkins.
- Additional setup may be needed for security (e.g., tokens, firewalls) to allow
the repository to communicate with Jenkins.
- Webhook:
- Ideal for environments where fast, real-time build triggering is needed.
- Preferred for repositories with frequent updates, as it reduces unnecessary
load and provides immediate feedback.
### Summary
- Poll SCM involves Jenkins checking the repository at regular intervals, which
can be resource-intensive and cause delays in build triggering.
- Webhooks allow the repository to notify Jenkins immediately when changes
occur, leading to more efficient and real-time build triggers.
In general, webhooks are preferred for modern CI/CD pipelines due to their
efficiency and immediacy, while Poll SCM remains a fallback option for
environments where webhooks aren't feasible.
1. Configure SCM:
- On the project configuration page, scroll down to the `Source Code
Management` section.
- Select `Git`.
- Enter the repository URL (e.g.,
`https://github.com/your-repo/sample-project.git`).
- Add credentials if necessary for accessing the repository.
- Specify the branch to build (e.g., `*/main`).
- under Build Triggers select "GitHub hook trigger for GITScm
polling"
2. Navigate to Webhooks:
- Click on `Settings` in the repository.
- On the left sidebar, select `Webhooks`.
Scenario: Test and validate that the webhook is correctly set up and
that Jenkins triggers builds when changes are pushed to the GitHub
repository.
- Install Git:
git --version
- Navigate to the GitHub repository you created earlier and copy the
HTTPS clone URL.
cd ec2-git-demo
git remote -v
- You should see the URL of the GitHub repository listed as `origin`.
- Go to GitHub:
### Conclusion
Jenkins-Day-3
QUIZ
### Installing Maven with YUM, Compatible Java Version, and
Configuring JAVA_HOME on Amazon Linux 2 EC2
#### Conclusion
You have successfully set up a Java development environment on an
Amazon Linux 2 EC2 instance by installing a compatible Java
version, installing Maven using YUM, and configuring the
`JAVA_HOME` environment variable. This setup ensures that Maven
can compile and build Java applications on your EC2 instance.
#### Scenario
You are tasked with cloning a Java-based project from a GitHub
repository to your local machine, compiling the code, running unit
tests, and packaging the application. The project is hosted at
`https://github.com/preethid/addressbook.git`.
8. Clean Up (Optional)
#### Conclusion
You have successfully cloned a project from a GitHub repository to
your local EC2 instance, compiled the code, ran unit tests, and
packaged the application using Maven. This process is essential for
ensuring that the project is correctly set up and that the code is
working as intended before deployment.
#### Scenario
You need to create a Jenkins Freestyle job that pulls a Java project
from a GitHub repository
(`https://github.com/preethid/addressbook.git`), compiles the code
using Maven, and uses a manually set Java path for an already
installed Java version. Maven will be configured as an automatic
installer in Jenkins.
1. Prerequisites
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Jenkins Home
Jenkins-Day-4
QUIZ
### Project: Running Maven Tests and Validation on Jenkins GUI
#### 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
### 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.
### 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.
### 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.
#### 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.
- 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.
#### 3. Validation
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
DevOps Training
WhatsApp *91 8886740600*
Preethi Dovala
akshay alone
Home
Syllabus
Live Training
Video Tutorials
Policies
Blog
< Jenkins Home
Jenkins-Day-5
QUIZ
### Project: Configuring Pipeline as Code in Jenkins GUI
pipeline {
agent any
stages {
stage('Compile') {
steps {
echo 'Compiling the source code...'
}
}
stage('Test') {
steps {
echo 'Running the tests...'
}
}
stage('Package') {
steps {
echo 'Packaging the application...'
}
}
}
}
- Click Save.
### 5. Run the Pipeline Job
Explanation: Trigger the pipeline execution manually.
- Go to the Project Dashboard of the newly created pipeline.
- Click Build Now to start the pipeline.
- You should see the stages run in sequence: Compile, Test, and
Package, with each stage echoing its name.
### Summary:
- Installed necessary pipeline plugins.
- Created a Jenkins pipeline job.
- Configured a basic pipeline script with three stages.
- Validated the output of each stage via console logs.
Groovy Code
pipeline {
agent any - This defines the start of a Jenkins pipeline block. The entire structure of the pipeli
stages {
stage('Compile') {
steps { - Specifies that the pipeline can be executed on any available Jenkins agent (no
echo 'Compiling the node in the cluster to run the pipeline unless yo
source code...'
}
}
stage('Test') { - The stages block is used to define multiple stages in the pipeline. Each stage r
steps { su
echo 'Running the tests...'
}
- Defines a stage in the pipeline named Compile. This is the first stage of the p
- The name inside `stage('...')` is the name that will appear in the Je
- Inside each stage, the steps block defines the actual commands or tasks to execut
this could involve running shell com
6
- `echo` is a Jenkins Pipeline step that outputs text to the console. This line will pr
- It serves as a simple
- This defines the second stage named Test. This stage will be responsible
- The steps block for the Test stage, just like in the Compile stage, defines the
- This defines the third stage called Package, where the source code is packag
### Scenario:
You need to create a Jenkins pipeline using the "Pipeline Script from
SCM" option, where the Jenkinsfile is stored in a GitHub repository.
- Explanation: Open Jenkins and create a new pipeline job that will
run based on the Jenkinsfile from a GitHub repository.
- Action:
- Navigate to Jenkins dashboard.
- Click on `New Item`.
- Enter a name for your project (e.g., "GitHub-Pipeline-Job").
- Select `Pipeline` and click `OK`.
By following these steps, you will have a fully functioning Jenkins pipeline,
pulling and executing a Jenkinsfile from a GitHub repository.
### Scenario:
You need to create a Git branch, clone a repository, add a Jenkinsfile
with `dev`, `test`, and `prod` stages, define parameters, push the
changes to GitHub, and configure Jenkins to execute the pipeline
using the "Pipeline script from SCM" feature.
if
echo "Bu
if (par
e
echo "Deploying to ${
- Explanation:
- Parameters:
- `ENVIRONMENT`: Defines the environment (default: dev).
- `RUN_TESTS`: Boolean parameter to decide whether to run tests.
- `DEPLOY_SERVER`: A choice parameter to specify which server to
deploy to (dev, test, or prod).
- Stages:
- Dev Stage: Simulates development environment build.
- Test Stage: Runs tests based on the parameter.
- Prod Stage: Deploys to the selected server.
- Explanation: Verify that each stage (`dev`, `test`, and `prod`) runs
as expected based on the parameter values you provided.
- Steps:
- Monitor the `Console Output` to check that the stages execute
correctly.
- Verify that the `Dev Stage`, `Test Stage`, and `Prod Stage` print
the expected messages based on the parameter inputs.
### Summary:
pipeline {
agent any
parameters {
string(name: 'ENVIRONMENT', defaultValue: 'dev', description:
'Environment to deploy to')
booleanParam(name: 'RUN_TESTS', defaultValue: true, description:
'Run tests?')
choice(name: 'DEPLOY_SERVER', choices: ['dev', 'test', 'prod'],
description: 'Choose deployment server')
}
stage('Test Stage') {
steps {
script {
if (params.RUN_TESTS) {
echo "Running tests in the ${params.ENVIRONMENT} environment"
} else {
echo "Skipping tests"
}
}
}
}
stage('Prod Stage') {
steps {
script {
if (params.DEPLOY_SERVER == 'prod') {
echo "Deploying to production server"
} else {
echo "Deploying to ${params.DEPLOY_SERVER} server"
}
}
}
}
}
}
**stage('Prod Stage')**: Defines the third stage, "Prod Stage".
**if (params.DEPLOY_SERVER == 'prod')**: Checks if the
`DEPLOY_SERVER` parameter is set to 'prod'.
**echo "Deploying to production server"**: Prints a message if
deploying to production.
**else**: Executes the next block if `DEPLOY_SERVER` is not 'prod'.
**echo "Deploying to ${params.DEPLOY_SERVER} server"**: Prints a
message indicating the chosen deployment server.
#### Scenario
We have three environments: Production, Development, and Test.
Based on the selected environment, a specific message will be
printed.
expression { param
echo 'Code D
expression { params.
echo 'Code De
expression {
echo
### Summary
This project demonstrates how to create a Jenkins pipeline that
selects actions based on a specified environment using parameters
and conditions. Each selected environment triggers a different
message to be printed in the build logs.
### Scenario:
We have three environments: Production, Development, and Test.
The pipeline will prompt for environment selection, and based on
that input, it will print a message for the chosen environment.
expression { env
expression { env.S
expressio
echo 'You
#### 3. Push the Jenkinsfile to GitHub:
- Add, commit, and push the `Jenkinsfile` to your Git repository.
pipeline {
agent any
- agent any: This directive specifies that the pipeline can run on any
available Jenkins agent.
stages {
stage('Input Environment') {
script {
env.SELECTED_ENV = input(
id: 'EnvironmentInput',
- id: A unique identifier for the input step to help Jenkins track it.
- message: The message shown to the user during the input prompt,
asking them to select the environment.
parameters: [
choice(name: 'ENVIRONMENT', choices: ['Production', 'Development',
'Test'], description: 'Choose the environment')
]
}
}
}
- Closing script, steps, and stage: These lines close the respective
blocks.
stage('Production Environment') {
steps {
echo 'You have selected the Production environment!'
}
stage('Development Environment') {
when {
expression { env.SELECTED_ENV == 'Development' }
}
steps {
echo 'You have selected the Development environment!'
}
}
stage('Test Environment') {
when {
expression { env.SELECTED_ENV == 'Test' }
}
steps {
echo 'You have selected the Test environment!'
}
}
}
}
### Summary:
- The pipeline asks the user to choose an environment (Production,
Development, or Test).
- Based on the user’s selection, the appropriate stage is triggered,
and a message is printed to the console.
- Conditional stages are implemented using the `when` directive
with an `expression` block.