Executing Automated Tests from a Git Repository with Jenkins on AWS EC2 Instances

Executing Automated Tests from a Git Repository with Jenkins on AWS EC2 Instances

Automated testing is a pivotal component in today's software engineering landscape, which helps maintain code integrity, identify defects, and enhance the dependability of software. Jenkins, a renowned continuous integration and continuous delivery (CI/CD) platform, can be united with AWS EC2 Instances to execute automated tests from a Git repository in a streamlined manner. In this guide, we'll explore how to establish Jenkins on an AWS EC2 Instance and conduct automated testing from a Git repository.


Why Use AWS EC2 Instances with Jenkins for Automated Tests from a Git Repository? Here are the benefits:

Scalability and Flexibility: AWS EC2 furnishes computing resources that can be scaled to your needs, enabling the customization of your Jenkins setup. Whether increasing or decreasing capacity, EC2 ensures that your CI/CD pipeline can effectively accommodate fluctuating demands.

Cost Efficiency: With EC2's pay-as-you-use model, you only incur costs for what you need, making Jenkins hosting economical. Launching instances when needed and terminating them when unnecessary prevents extra expenditures.

Isolation and Uniformity: Operating Jenkins on isolated EC2 instances averts conflicts with other applications. You can craft and store personalized Amazon Machine Images (AMIs), equipped with all essential configurations and dependencies, ensuring a uniform testing environment.

Safety Measures: AWS safeguards your Jenkins setup, offering control through AWS IAM and configuring Security Groups to permit access to specific ports only. For added security, AWS Key Management Service (KMS) can encrypt data.

Constant Availability: By deploying Jenkins across various EC2 instances in different AWS data centers, high availability and fault tolerance are maintained, diminishing the chances of downtime from infrastructure complications.

Automated Recovery and Backup Solutions: AWS supplies backup options for EC2 instances, such as automated backups and Amazon S3 data storage, easing the recovery process during unexpected setbacks.

Interoperability with AWS Services: EC2's integration with other AWS offerings enhances your CI/CD pipeline's functionality. Examples include using Amazon S3 for artifact storage or AWS CloudWatch for monitoring.

Worldwide Accessibility: AWS's global presence allows Jenkins deployment near your developers or users, minimizing latency and boosting performance.

Streamlined Deployment and IaC (Infrastructure as Code): Provisioning Jenkins on EC2 can be automated through IaC tools like AWS CloudFormation or Terraform, leading to easier setup, environment duplication, and consistency maintenance.

Auto Scaling Compatibility: Coupling Jenkins with AWS Auto Scaling lets you modify instance numbers according to defined policies, ensuring resource efficiency during busy periods and cost reductions during slack times.


Prerequisites:

Before commencing, please ensure the following:

a) An active AWS account with EC2 creation rights.

b) Familiarity with AWS services such as EC2, IAM, and Security Groups.

c) A Git repository hosting the codebase and automated tests to run.


Step 1: Launch an AWS EC2 Instance

  • Log into your AWS Management Console, heading to the EC2 dashboard.
  • Click "Launch Instance," selecting a suitable AMI, like the latest Free Tier-eligible Amazon Linux HVM edition with Kernel 5.10.
  • Choose an instance type that matches your workload, consulting https://aws.amazon.com/ec2/pricing/on-demand/ for pricing information.
  • Select the appropriate key pair and security group, adding rules for SSH (using your IP), HTTP, and a custom TCP rule for port 8080.
  • Review, launch, and wait for the status to change from pending to running.


Step 2: Install and Configure Jenkins on EC2 Instance

  1. Once your EC2 instance is up and running, the tool that you use to connect to your Linux instance depends on your operating system:

a) If your computer runs Windows, you will connect using PuTTY.

b) If your computer runs Linux or Mac OS X, you will connect using the

SSH client.

Both of these tools require having a key pair.

2. Update the package repository and install Java Development Kit (JDK) if not already installed:

sudo yum update -y
sudo yum install java-1.8.0-openjdk-devel


  • Install Jenkins using the official Jenkins repository:

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
sudo yum install jenkins


  • Start Jenkins and enable it to start on boot:

sudo systemctl start jenkins
sudo systemctl enable jenkins


  • Open the Jenkins web interface by accessing your public IP - it can be found under Public IPv4 DNS section in the Instance summary information box

Connect to https://<your_server_public_DNS>:8080 from your browser


  • Retrieve the initial Jenkins admin password from the EC2 instance:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword


  • Copy the password and paste it into the Jenkins web interface to complete the setup.


Step 3: Implement the Necessary Jenkins Plugins

Once inside Jenkins, navigate to "Manage Jenkins" > "Manage Plugins."

Install the essential plugins for connecting with Git repositories and executing automated tests. Examples include the "Git Plugin," "Pipeline Plugin," and any plugins specific to your testing frameworks (e.g., JUnit, Selenium).


Step 4: Establish Integration with Your Git Repository

Within Jenkins, either initiate a new pipeline project or select a freestyle project.

In the project's configuration settings, proceed to the "Source Code Management" area and choose "Git."

Input your Git repository's URL and any necessary credentials.

Designate the branch you aim to build and test (e.g., main or master), then save these settings.


Step 5: Formulate and Set Up a Jenkins Pipeline (Optional)

For those requiring intricate automated test procedures, Jenkins Pipelines offer a solution. These pipelines let you articulate your build, testing, and deployment phases in a script-like manner (e.g., utilizing Groovy).

In your chosen Jenkins project, pick "Pipeline" from the assortment of project types.

Within the pipeline script, lay out the stages of your testing workflow, including code checkout, dependency installation, test execution, and result reporting.

Store the finalized pipeline configuration.


Step 6: Initiate Automated Testing

Manual Triggering: In the event of a freestyle project, manually instigate the build and tests by clicking "Build Now" on the Jenkins project's main page.

Automated Triggering: If utilizing a Jenkins Pipeline, the automated tests can be set in motion automatically with any code alterations pushed to the designated branch in your Git repository.


Conclusion:

Deploying Jenkins on an AWS EC2 Instance to conduct automated tests from a Git repository offers a formidable method to bolster code integrity and simplify software development. Through test automation, you can unearth bugs sooner, increase deployment assurance, and enhance the dependability of your software products. With the guidance provided in this article, you are poised to craft a robust CI/CD pipeline that will serve as a valuable asset in your development efforts. Here's to successful and efficient testing!


Author:

Milan Guzina, QA Engineer at ALAS doo

要查看或添加评论,请登录

ALAS doo的更多文章

社区洞察

其他会员也浏览了