Automating Excellence: A Journey with Jenkins from AWS Setup to Web Deployment

Ayushmaan Srivastav
5 min readMar 3, 2024

--

In today’s fast-paced world, automation is the key to efficiency. In our recent class, we explored the installation of Jenkins on the AWS Cloud, a powerful automation tool that can streamline tasks across various technologies. This blog will guide you through the practical steps of setting up Jenkins on a Linux-based AWS instance, creating your first job, and running automated commands.

Setting up Jenkins on AWS:

Connecting to the Instance: To check if Jenkins is running, connect to your AWS instance. Use the following commands:

sudo su — root
systemctl status jenkins

Accessing Jenkins Dashboard: Obtain the public IP address of your instance and access the Jenkins dashboard in your browser using the address: http://<your_instance_ip>:8080.

Automating Commands with Jenkins:

Jenkins simplifies repetitive tasks, such as running Linux commands at specified intervals.

Creating Your First Job:

  • On the Jenkins dashboard, create a new job by clicking on “New Item” or “Create a Job.”
  • Provide a name for your job and select “Freestyle Project.”

Setting Up the Job:

  • Configure the job based on your requirements.
  • For instance, if you want to run the date or cal command, navigate to "Build Steps" and add an "Execute Shell" build step.
  • Input the necessary commands and click “Save.”

Running Your Jenkins Job:

Initiating a Build:

  • To run the job, go to the job page and click on “Build Now.”
  • Monitor the build history; a green mark indicates successful execution, and the timestamp is provided.

Editing Jobs:

  • To make changes to your job, go to “Configure,” update the settings, and save. Jenkins will assign a new build number.

Viewing Job Output:

  • If you wish to inspect the output, click on the job history, and then access “Console Output” to see detailed information about the job execution.

Granting Root Powers to Jenkins:

Understanding Privilege Escalation: In Linux, running programs requires a user account. Jenkins, by default, has limited powers, causing job failures when attempting certain actions like creating a user.

Accessing the sudoers File: To grant Jenkins additional powers, we edit the sudoers file. This file controls who can run what commands with elevated privileges. Execute the following commands:

sudo visudo

Adding Jenkins to sudoers:

  • Inside the sudoers file, make the necessary changes to provide Jenkins with root powers.
  • For example, add the following line to give root powers: jenkins ALL=(ALL:ALL) NOPASSWD:ALL.

Enabling Non-Interactive Commands: In Jenkins, it’s crucial to ensure that commands are non-interactive to prevent errors during job execution. This is achieved by appending -y to commands.

Configuring Jenkins for Privilege Escalation:

Modifying Jenkins Jobs:

  • When configuring Jenkins jobs, consider tasks that require elevated privileges.
  • Ensure that commands are non-interactive, for instance, by appending -y to commands like yum install.

Executing Jobs with Sudo Powers:

  • With the sudo privileges granted, Jenkins can now execute tasks that were previously restricted.
  • Run the job again, and this time it should execute successfully without encountering permission issues.

Setting up a web server manually involves several steps, from installing the necessary software to configuring files and restarting the system. In this blog, we will explore how Jenkins, with its automation capabilities, can streamline this process, allowing for the automatic configuration of a web server.

Automating Web Server Setup with Jenkins:

Installing the Web Server:

  • The first step is to install the HTTP server (httpd). This can be done manually, but we aim to automate the process.
  • Create a new Jenkins job and select the freestyle project.

Configuring the Jenkins Job:

  • Provide a meaningful name and description for the job.
  • In the build steps, select “Execute Shell” to define the commands that Jenkins will run.

Writing Commands for Web Server Setup:

  • Utilize sudo powers for necessary commands by prefixing them with sudo.
  • Ensure non-interactivity by appending -y to commands that require user input.
  • For example:

sudo yum install httpd -y
cd /var/www/html
sudo sh -c “echo “Hello, Jenkins Automation!” > index.html”
sudo systemctl restart httpd

Executing the Jenkins Job:

  • Save the job configuration and trigger its execution.
  • Observe the job running successfully, indicating that the web server setup commands were executed without issues.

Verifying the Web Server Configuration:

  • Obtain the public IP address of the instance running the web server.
  • Paste the IP address into your browser to access the web server.

In the dynamic landscape of web development, efficiency and automation play a pivotal role. This blog aims to guide you through the process of automating website deployment using Jenkins, integrating GitHub as the source code repository, and implementing triggers to ensure seamless updates.

Understanding the Workflow:

Code Development on GitHub:

  • Developers write and store code on their local machines.
  • Code changes are then pushed to GitHub, a popular repository tool.

Setting up GitHub Repository:

  • Create a new repository on GitHub, providing a name and adding a README file.
  • Developers push code changes to this repository using Git Bash.

Configuring Jenkins for GitHub Integration:

  • Install Git and Git plugin in Jenkins to enable GitHub integration.
  • Add the GitHub repository URL to the Jenkins job configuration.

Automating Code Pull and Deployment:

  • Create a new Jenkins job with a freestyle project.
  • In the job configuration, specify the GitHub repository URL.

Installing Git Command in Jenkins:

  • Ensure that the Git command is installed on the instance where Jenkins is running:

sudo yum install git

Defining Jenkins Workspace:

  • Jenkins creates a workspace, downloading and storing code from GitHub.

Copying Files and Triggering Jobs:

  • In the Jenkins job, use the Execute Shell build step to copy files or perform necessary tasks.
  • Manually trigger the job to observe changes in the deployed website.

sudo cp -f index.html /var/www/html/

Implementing Triggers for Automation:

Understanding Jenkins Triggers:

  • Explore different triggers available in Jenkins, such as Build periodically and Poll SCM.

Using Build Periodically Trigger:

  • Configure the build to run at scheduled intervals, ensuring regular updates.
  • This trigger is suitable for scenarios where periodic deployments are needed.

Implementing Poll SCM Trigger:

  • Configure the job to poll the source code repository for changes.
  • This trigger is more efficient, as it only deploys the code when changes are detected.

Conclusion:

In the ever-evolving landscape of technology, efficiency and automation are not just buzzwords but crucial elements that drive success. This blog has guided you through the practical steps of harnessing the power of Jenkins, a robust automation tool, to streamline tasks ranging from Linux command execution to setting up a web server and automating website deployment.

From the initial setup of Jenkins on an AWS instance to granting it the necessary privileges for seamless execution, you’ve learned how to make Jenkins a potent ally in automating repetitive tasks. The blog then delved into the automation of web server setup, demonstrating how Jenkins can handle intricate processes, ensuring a hassle-free configuration.

In the dynamic world of web development, the blog took a step further by showcasing the integration of Jenkins with GitHub, elucidating the workflow from code development to deployment. The automation of code pull and deployment, coupled with triggers like Build Periodically and Poll SCM, ensures that your website stays updated with minimal manual intervention.

As we conclude this journey, remember that automation is not just about saving time but about fostering a culture of continuous improvement. Jenkins, with its versatility and adaptability, serves as a catalyst in achieving this goal. Whether you are a seasoned developer or just stepping into the realm of automation, this blog has equipped you with practical insights to navigate the fast-paced world of technology. Embrace automation, streamline your processes, and pave the way for a more efficient and productive future.

--

--

No responses yet