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
Step 2: Install and Configure Jenkins on EC2 Instance
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
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
领英推荐
sudo systemctl start jenkins
sudo systemctl enable jenkins
Connect to https://<your_server_public_DNS>:8080 from your browser
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
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