Testing Shifts and Continuous Integration
Modern software development practices involve testing early in the lifecycle (shift-left) and extending testing into production (shift-right). Continuous Integration (CI) is a core DevOps practice where developers frequently merge code changes into a central repository. Automated builds and tests run on each integration, enabling rapid error detection and preventing feature branches from diverging significantly from the main codebase. The primary goal is to facilitate rapid product iteration while maintaining high quality, enforced by requiring all automated tests to pass before any code merges into the main branch.
Common Branch Management Strategies
Organizations typically adopt structured branching models to manage development workflows.
- Master/Main Branch: This branch contains stable, production-ready code. No direct development occurs here. New features are developed in separate branches, then merged into master after testing.
- Development (Dev) Branch: Serves as an integration branch for ongoing work. For instance, separate feature branches (e.g.,
feature-cart,feature-shops) are created fromdev. Once development is complete, these are merged into a release branch for testing. - Release Branch: A branch created for final testing, bug fixing, and stabilization before a production release. After successful testing and deployment, the release branch is merged into the master branch and can be deleted.
Configuring the CI Toolchain
A typical CI setup involves Gitee (code hosting), Git (version control), Ant (build automation), and Jenkins (CI server). Note: The build tool (e.g., Ant, Maven, Gradle) must be chosen based on the specific project requirements.
1. Gitee Setup
Visit https://gitee.com to register an account and create a new repository for your project.
2. Git Configuration
Installation and Setup
- Download the Git installer from https://git-scm.com/download/win and follow the installation steps.
- Configure your global Git identity using the terminal (Git Bash):
git config --global user.name "Your Name" git config --global user.email "your-email@example.com" - Generate an SSH key pair for secure authentication with Gitee:
Follow the prompts, then copy the contents of the generatedssh-keygen -t ed25519 -C "your-email@example.com"id_ed25519.pubfile and add it to your Gitee account under Settings > SSH Public Keys.
Initial Project Upload to Gitee
Navigate to your project's root directory (ensure no existing .git folder) and execute:
git init
git add .
git commit -m "Initial commit"
git remote add origin git@gitee.com:your-username/your-repo.git
git push -u origin master
Refresh your Gitee repository page to confirm the upload.
Submitting Code Changes
After mdoifying files:
git add .
git commit -m "Description of changes"
git push origin master
Cloning and Updating a Repository
To obtain a local copy:
git clone git@gitee.com:your-username/your-repo.git
To fetch the latest changes in an existing clone:
git pull origin master
After pulling, you would typically build the project (e.g., using Ant to create a WAR file) and deploy it manually to an application server like Tomcat. This manual process is inefficient, which is where Jenkins automation becomes valuable.
3. Jenkins Automation
Jenkins automates the build, test, and deployment pipeline.
Installation and Startup
- Download the Jenkins WAR file from https://www.jenkins.io/download/.
- Ensure Java (JDK) is installed on the system.
- Start Jenkins from the command line:
Thejava -jar jenkins.war --httpPort=8081--httpPortargument changes the default port if necessary. - Access Jenkins at
http://localhost:8081. Unlock it using the initial administrator password provided in the terminal or log files, then install the suggested plugins and create an admin user.
Basic Job Configuration
Create a new Freestyle project in Jenkins.
- For executing a local Python script: Add a build step to execute a shell/Windows batch command.
cd C:\Projects\automation pytest test_suite.py - For building a project from Gitee:
- Configure the global Git path in Jenkins > Manage Jenkins > Global Tool Configuration.
- In the job configuration, under Source Code Management, select Git and provide the repository URL (e.g.,
git@gitee.com:your-username/your-repo.git). - Add build steps to invoke your build tool (e.g., Ant).
Scheduling Builds
To configure a job to run periodically (e.g., every day at 2 AM), use the Build Triggers > Build periodically section and enter a cron-style schedule: 0 2 * * *.
Email Notifications
Configure email alerts for build failures via Jenkins > Manage Jenkins > Configure System. Set the SMTP server details (e.g., for QQ邮箱, enable SMTP service in your email account first and use smtp.qq.com). In the job configuration, under Post-build Actions, add Editable Email Notification to define recipients and triggers.