Untitled
UNIT III Build Automation and Continuous Integration (CI)
Build Automation Concepts
- Build Automation: The process of automatically compiling source code, running tests, performing static code analysis, packaging applications, and preparing them for deployment.
- Benefits:
- Eliminates manual intervention.
- Reduces human errors.
- Ensures consistent and repeatable builds.
Need for Build Automation
- Eliminates manual build errors.
- Saves developer time.
- Ensures consistent builds across environments.
- Enables frequent integration and testing.
- Supports CI/CD pipelines.
Build Automation Tasks
- Source Code Compilation: Converts source code into executable binaries.
- Dependency Management: Automatically downloads required libraries.
- Unit Testing: Executes automated test cases.
- Code Quality Checks: Static code analysis, linting.
- Packaging: Creates JAR, WAR, EAR, Docker images, etc.
- Artifact Storage: Stores build outputs in repositories (e.g., Nexus, Artifactory).
Common Build Automation Tools
- Maven: Java
- Gradle: Java, Kotlin
- Ant: Java
- MSBuild: .NET
- npm/yarn: JavaScript
- Make: C / C++
Build Lifecycle (Typical Flow)
- Code checkout from repository
- Resolve dependencies
- Compile source code
- Run unit tests
- Perform code analysis
- Package artifacts
- Store artifacts
- Trigger deployment (optional)
Continuous Integration (CI)
- Continuous Integration (CI): A DevOps practice where developers frequently integrate code into a shared repository. Each integration is automatically verified by a build and test process using CI tools.
- Objectives of CI:
- Detect bugs early.
- Improve software quality.
- Reduce integration problems.
- Enable faster releases.
- Maintain a stable codebase.
CI Workflow
- Developer commits code
- CI server detects changes
- Automated build starts
- Tests are executed
- Results are reported
- Feedback sent to developers
Popular CI Tools
- Jenkins: Open-source, widely used CI tool
- GitHub Actions: CI/CD integrated with GitHub
- GitLab CI: Built-in CI for GitLab
- Bamboo: Atlassian CI tool
- Travis CI: Cloud-based CI
- CircleCI: Fast cloud CI tool
Features of Build Automation and CI
- Automates build, test, and packaging processes.
- Ensures consistent builds across environments.
- Detects errors early through frequent integration.
- Reduces manual effort and human errors.
- Improves software quality and reliability.
- Enables faster delivery through automation.
- Integrates with version control systems (e.g., Git).
- Supports DevOps and Agile methodologies.
Introduction to Jenkins
- Jenkins: A powerful open-source automation server used to build, test, and deploy software in a continuous integration/continuous deployment (CI/CD) pipeline.
Jenkins Architecture
- Organization User: Responsible for code changes that trigger Jenkins build.
- Source Code Repository: Uses version control system (VCS) like Git, integrated with Jenkins through Source Code Management (SCM).
- Master Node (Controller): Schedules jobs, monitors agents, and assigns jobs based on predefined rules.
- Slave Nodes (Agents): Performs actual jobs, with communication established via SSH or JNLP.
- Build, Test, Deploy:
- Build: Compiles source code and creates build artifacts.
- Test: Runs automated tests to ensure stability.
- Deploy: Application is deployed if all tests pass.
Pipeline Concept
- Pipeline: A series of connected events/tasks to make quick software releases.
- Each stage must be completed before progressing to the next.
- Continuous Integration (CI): Integrates code and uses automated verification.
- Continuous Delivery (CD): Decides when code should deploy to customers.
Jenkins Master-Slave Architecture
- Allows workloads to be distributed across various slave nodes.
- Supports running identical test cases in different environments.
- Results are collected and combined on the master node for monitoring.
Jenkins Jobs
- Represents a series of tasks Jenkins automates (e.g., compile using Maven, execute tests, deploy artifacts).
Different Types of Jenkins Jobs
- Freestyle Project: Basic project setup without scripting.
- Steps to create:
- Log in to Jenkins.
- Click on "New Item" > Name project > Select "Freestyle project".
- Configure project settings (repository, triggers, environment).
- Steps to create:
- Pipeline Job:
- Uses a Jenkinsfile for complex workflows or continuous delivery.
User Management in Jenkins
- Involves creating users with specific roles and permissions.
- Prerequisite: Administrative privileges to manage users and install plugins.
Steps to Create Users
- Create users manually in Jenkins' internal database.
- Install the Role-based Authorization Strategy plugin for advanced management.
- Configure user roles:
- Global roles: Permissions across Jenkins.
- Item roles: Permissions for specific projects.
- Assign roles to users.
Scheduling Builds and Launching on Slave Nodes
- Steps:
- Go to Manage Jenkins > Manage Nodes
- Create a new node, entering required information and credentials.
- Set agent availability.
Continuous Testing
- The process of automatically executing tests within the CI/CD pipeline.
- Benefits:
- Detect bugs early, reduce manual testing, improve reliability, and enable faster delivery.
- Selenium Testing: Automation tool for web applications.
- Integration with Jenkins automates testing during development.
Example Testing Tools
- Selenium: Tests web applications, supporting multiple browsers/languages.
- JavaScript Testing:
- Tools: Jest (unit testing), Mocha (flexible framework).
Integrating Selenium & JavaScript Testing with Jenkins
- Install required plugins.
- Configure tools in Jenkins (JDK, Maven, NodeJS).
- Create automated test cases and integrate with Jenkins.
- Create pipelines to fetch code, build, run tests, and publish test reports.
Sample Jenkins Pipeline Script
groovy
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git branch: 'development', credentialsId: 'github-credentials-id', url: 'https://github.com/your-username/your-repository.git'
}
}
stage('Build') {
steps {
sh "{mavenHome}/bin/mvn clean package"
}
}
stage('Selenium Testing') {
steps {
// Execute UI automation tests
}
}
stage('JavaScript Testing') {
steps {
// Run unit tests
}
}
stage('Reports') {
steps {
// Publish test results
}
}
}
}
Benefits of Integrating Testing with Jenkins
- Automation, early bug detection, consistency, time-saving, improved quality, and continuous feedback.
Test Reports in Jenkins
- After execution, Jenkins generates pass/fail results, logs, error details, test summaries.
Real-World Use Case
- Developer updates code:
- Jenkins builds application
- Selenium tests UI
- JavaScript tests verify logic
- If tests pass, the application gets deployed; if any fail, developer is notified.