Exercise-10

10. Creating Build Pipelines: Building a Maven/Gradle Project with Azure Pipelines, Integrating Code Repositories (e.g., GitHub, Azure Repos), Running Unit Tests and Generating Reports

🎯 Objective:

To configure a self-hosted agent on a local machine, build a Maven or Gradle project using Azure DevOps Pipelines, run unit tests, and generate reports — all without incurring any Azure charges.


🔷 Theory Overview

What is a Self-Hosted Agent?

A Self-hosted agent is a personal or institutional computer (e.g., your own laptop or lab PC) that runs Azure Pipelines jobs instead of using Microsoft-hosted (cloud) agents.

  • No billing or time limits

  • Full control over environment

  • Great for offline or classroom scenarios


🛠️ Steps to Build a Maven/Gradle Project with a Self-Hosted Agent


🖥️ Step 1: Prepare Your System (Self-Hosted Agent)

You’ll need:

  • A PC with Java, Maven or Gradle installed

  • Git installed

  • Internet access to download Azure agent


🔗 Step 2: Create an Azure DevOps Project (One-Time Setup by Instructor)

  1. Go to https://dev.azure.com

  2. Sign in and create a new organization (e.g., college-devops-org)

  3. Create a project (e.g., BuildPipelineLab)


⚙️ Step 3: Register Your Self-Hosted Agent

  1. In Azure DevOps portal:

    • Go to Project Settings > Agent Pools

    • Click Default pool

    • Select New Agent > Choose OS (Windows/Linux/macOS)

  2. Follow the steps shown to:

    • Download agent package

    • Extract it to a folder, e.g., C:\azagent

    • Run these commands:

cd C:\azagent
config.cmd
  1. Provide:

    • DevOps URL

    • Personal Access Token (PAT)

    • Agent name (e.g., labagent1)

    • Choose "run as a service" if prompted

  2. Start the agent:

run.cmd

📦 Step 4: Push Code to Repository

  1. Create a repository in Azure Repos or GitHub

  2. Push your Maven/Gradle project there


🛠️ Step 5: Create azure-pipelines.yml File in Your Repo

✅ For Maven:

trigger:
- main pool: name: Default # Refers to your self-hosted agent pool steps: - task: Maven@3 inputs: mavenPomFile: 'pom.xml' goals: 'clean install' publishJUnitResults: true

✅ For Gradle:

trigger:
- main pool: name: Default steps: - script: ./gradlew clean build displayName: 'Build with Gradle'

🚀 Step 6: Run the Pipeline

  1. Commit the YAML file to your repository.

  2. Azure Pipelines will trigger automatically when changes are pushed.

  3. The job will run on your own PC or lab machine (self-hosted agent).


📊 Step 7: View Unit Test Reports

  • Go to Pipelines > Runs

  • Click the latest run

  • Go to Tests tab to view results

If you don’t see test results:

  • Make sure you're using a testing framework like JUnit/TestNG.

  • Ensure reports are being generated in standard format (e.g., target/surefire-reports for Maven).


Advantages of Using Self-Hosted Agent

  • 💸 Zero cost – no billing risk

  • 🖥️ Use lab computers or personal laptops

  • 🛠️ More control over software environment

  • 📦 Ideal for educational institutions



Comments