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)
  1. Code checkout from repository
  2. Resolve dependencies
  3. Compile source code
  4. Run unit tests
  5. Perform code analysis
  6. Package artifacts
  7. Store artifacts
  8. 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
  1. Developer commits code
  2. CI server detects changes
  3. Automated build starts
  4. Tests are executed
  5. Results are reported
  6. 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
  1. Organization User: Responsible for code changes that trigger Jenkins build.
  2. Source Code Repository: Uses version control system (VCS) like Git, integrated with Jenkins through Source Code Management (SCM).
  3. Master Node (Controller): Schedules jobs, monitors agents, and assigns jobs based on predefined rules.
  4. Slave Nodes (Agents): Performs actual jobs, with communication established via SSH or JNLP.
  5. 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
  1. 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).
  2. 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
  1. Create users manually in Jenkins' internal database.
  2. Install the Role-based Authorization Strategy plugin for advanced management.
  3. Configure user roles:
    • Global roles: Permissions across Jenkins.
    • Item roles: Permissions for specific projects.
  4. Assign roles to users.
Scheduling Builds and Launching on Slave Nodes
  • Steps:
  1. Go to Manage Jenkins > Manage Nodes
  2. Create a new node, entering required information and credentials.
  3. 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
  1. Install required plugins.
  2. Configure tools in Jenkins (JDK, Maven, NodeJS).
  3. Create automated test cases and integrate with Jenkins.
  4. 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:
    1. Jenkins builds application
    2. Selenium tests UI
    3. JavaScript tests verify logic
    4. If tests pass, the application gets deployed; if any fail, developer is notified.