Git Lab Exercises
Jenkins- Day -1
step-by-step guide for setting up Jenkins on an EC2 instance running Amazon Linux 2 and performing various tasks:
### Step 1: Set Up Jenkins on an EC2 Instance (Amazon Linux 2)
1. Launch an EC2 Instance:
- Go to the AWS Management Console.
- Launch a new EC2 instance.
- Choose an Amazon Machine Image (AMI) for Amazon Linux 2.
- Select an instance type (e.g., t2.micro for free tier).
- Configure instance details, add storage, and add tags as needed.
- Configure security group: Add rules to allow HTTP (port 80) and custom TCP rule for Jenkins (port 8080).
- Review and launch the instance.
2. Connect to the EC2 Instance:
- Connect to your instance via SSH using the key pair you created.
3. Install Jenkins:
- Update the package index:
```bash
sudo yum update -y
```
- Install Java (Jenkins requires Java):
```bash
sudo amazon-linux-extras install java-openjdk11 -y
```
- Add Jenkins repository and key:
```bash
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
```
- Install Jenkins:
```bash
sudo yum install jenkins -y
```
- Start Jenkins:
```bash
sudo systemctl start jenkins
```
- Enable Jenkins to start on boot:
```bash
sudo systemctl enable jenkins
```
### Step 2: Check the Jenkins Port
1. Verify Jenkins is Running:
```bash
sudo systemctl status jenkins
```
2. Check Jenkins Port:
- By default, Jenkins runs on port 8080. Verify by opening a browser and navigating to:
```
http://<your-ec2-public-ip>:8080
```
### Step 3: Add the Port to Security Group
1. Update Security Group:
- Go to the AWS Management Console.
- Navigate to EC2 Dashboard > Instances.
- Select your instance and note the security group.
- Navigate to Security Groups under Network & Security.
- Select your security group and edit inbound rules.
- Add a custom TCP rule for port 8080.
### Step 4: Check the Home Directory
1. Locate Jenkins Home Directory:
```bash
echo $JENKINS_HOME
```
- Default location: `/var/lib/jenkins`
### Step 5: Post-Install Task
1. Unlock Jenkins:
- Get the initial admin password:
```bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```
- Use this password to unlock Jenkins via the web interface.
2. Install Suggested Plugins:
- Follow the setup wizard to install suggested plugins.
3. Create Admin User:
- Set up your admin user as prompted by the setup wizard.
### Step 6: Create a Freestyle Job
1. Create a New Freestyle Project:
- In Jenkins dashboard, click on "New Item".
- Enter an item name (e.g., "SampleProject").
- Select "Freestyle project" and click OK.
2. Configure the Job:
- In the project configuration page, add a simple build step:
- Under "Build", click "Add build step" and select "Execute shell".
- Enter a shell command, e.g., `echo "Hello, Jenkins!"`.
- Save the configuration.
### Step 7: Find the Workspace Location
1. Locate Workspace:
- The workspace for the job can be found at:
```bash
/var/lib/jenkins/workspace/<your-job-name>
```
### Step 8: Check the Console Output
1. Run the Job and Check Output:
- Go to your project's page.
- Click "Build Now" to run the job.
- Click on the build number (e.g., #1) in the "Build History" on the left.
- Click "Console Output" to view the job's output.
By following these steps, you'll have a Jenkins instance set up on an EC2 instance running Amazon Linux 2, with the ability to create and manage jobs, and access various Jenkins features.