Buddhi Eashwarage
Posted on July 22, 2023
Jenkins is a powerful open-source automation server that facilitates continuous integration and continuous deployment (CI/CD) in software development projects. In this article, we will guide you through the process of setting up Jenkins on an Amazon Web Services (AWS) Elastic Compute Cloud (EC2) instance and automating the deployment of changes to an Apache web server. This will enable you to streamline your development workflow and ensure smooth and efficient software deployment.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- An AWS account with the necessary permissions to create EC2 instances and configure security groups.
- Familiarity with basic Linux commands and the AWS Management Console.
Step 1: Launch an EC2 Instance
- Log in to your AWS Management Console.
- Navigate to the EC2 dashboard.
- Click on "Launch Instance" to create a new EC2 instance.
- Select an Amazon Linux AMI or any other suitable Linux distribution of your choice.
- Choose an instance type based on your requirements. A t2.micro instance will suffice for this demonstration.
- Configure the instance details (e.g., network, subnet, IAM role).
- Add storage as needed for your Jenkins installation.
- Configure security groups to allow inbound traffic on port 8080 (Jenkins web interface) and port 22 (SSH access). Additionally, open port 80 if your Apache server is already running.
Step 2: Connect to Your EC2 Instance
- After the instance is launched, select it in the EC2 dashboard.
- Click on the "Connect" button to view connection options.
- Use SSH to connect to your EC2 instance using the provided Public DNS or IP address:
ssh -i /path/to/your/key.pem ec2-user@your-ec2-public-dns
Step 3: Install Java
Jenkins requires Java to run. Amazon Linux usually comes with OpenJDK installed, but let's make sure we have Java installed:
- Update the package list:
sudo yum update -y
- Install Java:
sudo yum install java-1.8.0-openjdk-devel -y
- Check Java version:
java -version
Step 4: Install Jenkins
Now that Java is installed, let's proceed with installing Jenkins:
- Add the Jenkins repository to your system:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
- Import the Jenkins repository key:
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
- Install Jenkins:
sudo yum install jenkins -y
- Start the Jenkins service:
sudo service jenkins start
- Enable Jenkins to start on system boot:
sudo chkconfig jenkins on
For the latest Jenkins installation instructions can be found through the Jenkins official installation guide
Step 5: Access Jenkins Web Interface
- Open a web browser and access Jenkins using your EC2 instance's public IP address or Public DNS on port 8080:
http://your-ec2-public-ip:8080
- You will be prompted to enter an initial administrator password. Retrieve it using the following command on your EC2 instance:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy and paste the password into the Jenkins web interface.
Follow the setup wizard to install suggested plugins and create an admin user.
Step 6: Install Required Plugins
To deploy changes to the Apache server, we will use the "Publish Over SSH" plugin. Here's how to install it:
- In the Jenkins web interface, click on "Jenkins" in the top-left corner to go back to the dashboard.
- Click on "Manage Jenkins" on the left-hand side.
- Select "Manage Plugins."
- Navigate to the "Available" tab and search for "Publish Over SSH."
- Check the box next to "Publish Over SSH" and click on "Install without restart."
Step 7: Configure Jenkins Credentials
Before we can deploy changes via SSH, Jenkins needs SSH credentials to access your Apache server:
- In the Jenkins web interface, click on "Jenkins" in the top-left corner to go back to the dashboard.
- Click on "Credentials" on the left-hand side.
- Under "Stores scoped to Jenkins," click on "Jenkins."
- Click on "Global credentials (unrestricted)."
- Click on "Add Credentials" on the left-hand side.
- Select "SSH Username with private key" from the "Kind" dropdown.
- Enter your SSH private key or provide the path to your private key file.
- Add a description and username for your credentials, and click "Save."
Step 8: Create a Jenkins Job for Deployment
Now, let's create a Jenkins job to automate the deployment of changes to the Apache server:
- In the Jenkins web interface, click on "New Item" on the left-hand side.
- Enter a name for your job (e.g., "Deploy to Apache Server") and select "Freestyle project."
- Click "OK" to create the job.
- Scroll down to the "Build" section and click on "Add build step."
- Select "Send files or execute commands over SSH" from the dropdown.
- Choose the SSH server you configured earlier.
- In the "Source files" field, enter the path to the files you want to deploy (e.g.,
./dist/*
). - In the "Remove prefix" field, enter the relative path to the files from the Jenkins workspace, if applicable.
- In the "Remote directory" field, enter the path on the Apache server where you want to deploy the files (e.g.,
/var/www/html
). - Click "Save" to save the job configuration.
Step 9: Trigger the Deployment
Now that the job is set up, you can manually trigger the deployment:
- Go back to the Jenkins dashboard.
- Find your "Deploy to Apache Server" job and click on it.
- Click on "Build Now" to trigger the deployment.
Jenkins will now connect to your Apache server via SSH and deploy the changes specified in the job configuration.
Conclusion
Congratulations! You have successfully installed Jenkins on an AWS EC2 instance and set up a Jenkins job to automate the deployment of changes to your Apache web server. With Jenkins managing your CI/CD pipeline, you can enjoy faster and more reliable software deployments, enhancing your development workflow and overall productivity. Remember to monitor your Jenkins jobs regularly, and feel free to explore other Jenkins plugins and features to further enhance your automation process. Happy coding!
Posted on July 22, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.