Skip to content

atulkamble/azure-pipeline-basic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Azure Pipeline Basic Demo

Build Status

A minimal Python project demonstrating Azure DevOps CI/CD pipeline with automated testing. This repository serves as a template for setting up basic continuous integration workflows in Azure Pipelines.

πŸ“‹ Table of Contents

🎯 Overview

This project demonstrates a basic Azure Pipeline that:

  • βœ… Triggers automatically on pushes to main branch
  • βœ… Uses hosted Ubuntu agents (ubuntu-latest)
  • βœ… Sets up Python 3.11 environment
  • βœ… Installs project dependencies
  • βœ… Runs automated tests with pytest
  • βœ… Reports test results

Build Stats:

  • Average build time: ~30-40 seconds
  • Latest build: 20251012.1 - βœ… Succeeded
  • Test coverage: 3/3 tests passing

πŸ“ Project Structure

azure-pipeline-basic/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py          # Package marker
β”‚   └── hello.py             # Main application with greet() function
β”œβ”€β”€ tests/
β”‚   └── test_hello.py        # Pytest test suite (3 tests)
β”œβ”€β”€ azure-pipelines.yml      # Pipeline definition
β”œβ”€β”€ requirements.txt         # Python dependencies (pytest)
└── README.md               # This file

Key Files

azure-pipelines.yml - Pipeline configuration:

  • Defines build triggers
  • Configures build agent
  • Specifies build steps
  • Sets Python version

app/hello.py - Simple Python application:

  • greet(name: str) -> str - Returns greeting message
  • Input validation (raises ValueError for empty names)
  • CLI entrypoint for testing

tests/test_hello.py - Test suite:

  • test_greet_happy() - Tests standard greeting
  • test_greet_empty() - Tests error handling
  • test_greet_whitespace() - Tests edge cases

βš™οΈ Pipeline Configuration

The pipeline is defined in azure-pipelines.yml:

trigger:
  - main                    # Auto-trigger on main branch commits

pool:
  vmImage: 'ubuntu-latest'  # Use Ubuntu build agent

variables:
  pythonVersion: '3.11'     # Python version

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(pythonVersion)'

  - script: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt
    displayName: 'Install dependencies'

  - script: |
      pytest -q
    displayName: 'Run tests'

πŸ”§ Prerequisites

Local Development

  • Python 3.11 or higher
  • Git
  • pip (Python package manager)

Azure DevOps Setup

  • Azure DevOps account (Sign up free)
  • Azure CLI (optional, for command-line operations)
  • Personal Access Token (PAT) for authentication

πŸ’» Local Development

1. Clone the Repository

git clone https://github.com/atulkamble/azure-pipeline-basic.git
cd azure-pipeline-basic

2. Set Up Virtual Environment

# Create virtual environment
python3 -m venv .venv

# Activate virtual environment
# On macOS/Linux:
source .venv/bin/activate
# On Windows:
# .venv\Scripts\activate

3. Install Dependencies

pip install --upgrade pip
pip install -r requirements.txt

4. Run the Application

# Run the main application
python app/hello.py
# Output: Hello, World!

# Import and use in Python
python -c "from app.hello import greet; print(greet('Azure'))"
# Output: Hello, Azure!

5. Run Tests Locally

# Run all tests
pytest -v

# Run with coverage
pytest --cov=app --cov-report=term-missing

# Quick run (quiet mode)
pytest -q

Expected output:

...                                              [100%]
3 passed in 0.01s

☁️ Azure DevOps Setup

Option 1: Using Azure Portal (Recommended for Beginners)

Step 1: Create Azure DevOps Project

  1. Navigate to https://dev.azure.com
  2. Sign in with your Microsoft account
  3. Click + New project
  4. Fill in project details:
    • Project name: azure-pipeline-basic
    • Description: Basic Azure Pipeline demo
    • Visibility: Private
  5. Click Create

Step 2: Push Code to Azure Repos

# Add Azure DevOps remote
git remote add azure https://dev.azure.com/YOUR-ORG/azure-pipeline-basic/_git/azure-pipeline-basic

# Push code
git push azure main

Authentication Issues? See Troubleshooting section.

Step 3: Create Pipeline

  1. In Azure DevOps, navigate to Pipelines β†’ Pipelines
  2. Click New pipeline or Create Pipeline
  3. Select Azure Repos Git
  4. Select your repository: azure-pipeline-basic
  5. Azure DevOps will detect azure-pipelines.yml
  6. Review the configuration
  7. Click Run

The pipeline will start automatically!

Option 2: Using Azure CLI (Advanced)

Step 1: Install and Configure Azure CLI

# Install Azure CLI (macOS)
brew install azure-cli

# Login to Azure
az login

# Set default organization and project
az devops configure --defaults organization=https://dev.azure.com/YOUR-ORG project=azure-pipeline-basic

Step 2: Create Project (if needed)

az devops project create \
  --name "azure-pipeline-basic" \
  --description "Basic Azure Pipeline demo" \
  --organization https://dev.azure.com/YOUR-ORG \
  --source-control git \
  --visibility private

Step 3: Set Up Authentication

Create Personal Access Token (PAT):

  1. Go to https://dev.azure.com/YOUR-ORG
  2. Click profile icon β†’ Personal access tokens
  3. Click + New Token
  4. Name: Git Push Token
  5. Scopes: Code (Read & write)
  6. Click Create and copy the token

Configure Git with PAT:

git remote add azure https://YOUR_PAT@dev.azure.com/YOUR-ORG/azure-pipeline-basic/_git/azure-pipeline-basic
git push azure main

Step 4: Create Pipeline

az pipelines create \
  --name "azure-pipeline-basic" \
  --repository azure-pipeline-basic \
  --repository-type tfsgit \
  --branch main \
  --yml-path azure-pipelines.yml \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic

This command will:

  • Create the pipeline definition
  • Automatically trigger the first build
  • Return build details

πŸš€ Pipeline Operations

List All Pipelines

az pipelines list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

Trigger a Manual Build

az pipelines run \
  --name "azure-pipeline-basic" \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic

Check Build Status

# Get latest build status
az pipelines build show \
  --id BUILD_ID \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --query "{status: status, result: result, buildNumber: buildNumber}" \
  --output table

List Recent Builds

az pipelines runs list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

View Build Logs

# Get detailed build information
az pipelines build show \
  --id BUILD_ID \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic

Monitor Build Progress

# Wait and check status
sleep 10 && az pipelines build show \
  --id BUILD_ID \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --query "{status: status, result: result, buildNumber: buildNumber, finishTime: finishTime}" \
  --output table

πŸ” Troubleshooting

Common Issues and Solutions

1. Git Push Hangs or Fails

Problem: git push azure main hangs waiting for authentication

Solution A - Personal Access Token (Recommended):

# 1. Create PAT in Azure DevOps (Settings β†’ Personal Access Tokens)
# 2. Update remote URL with PAT
git remote set-url azure https://YOUR_PAT@dev.azure.com/YOUR-ORG/azure-pipeline-basic/_git/azure-pipeline-basic

# 3. Push again
git push azure main

Solution B - SSH Authentication:

# 1. Generate SSH key (if you don't have one)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 2. Copy public key
cat ~/.ssh/id_rsa.pub

# 3. Add to Azure DevOps (Settings β†’ SSH public keys)

# 4. Update remote to use SSH
git remote set-url azure git@ssh.dev.azure.com:v3/YOUR-ORG/azure-pipeline-basic/azure-pipeline-basic

# 5. Push
git push azure main

2. Pipeline Not Found

Problem: There were no build definitions matching name "azure-pipeline-basic"

Solution: Create the pipeline first:

az pipelines create \
  --name "azure-pipeline-basic" \
  --repository azure-pipeline-basic \
  --repository-type tfsgit \
  --branch main \
  --yml-path azure-pipelines.yml \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic

3. Import Errors in Tests

Problem: ModuleNotFoundError: No module named 'app'

Solution: The test file includes a sys.path fix. Ensure you have app/__init__.py:

# tests/test_hello.py already includes this fix
import os
import sys
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if ROOT not in sys.path:
    sys.path.insert(0, ROOT)

4. Azure CLI Not Authenticated

Problem: Please run 'az login' to setup account

Solution:

az login
az account show  # Verify authentication

5. Check Git Configuration

# View all remotes
git remote -v

# Check current branch
git branch

# View git status
git status

# View recent commits
git log --oneline -5

πŸ”§ Advanced Configuration

Add Code Coverage

Update requirements.txt:

pytest
pytest-cov

Update azure-pipelines.yml:

- script: |
    pytest --cov=app --cov-report=xml --cov-report=html
  displayName: 'Run tests with coverage'

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: 'Cobertura'
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/coverage.xml'

Matrix Testing (Multiple Python Versions)

strategy:
  matrix:
    Python39:
      python.version: '3.9'
    Python310:
      python.version: '3.10'
    Python311:
      python.version: '3.11'
    Python312:
      python.version: '3.12'

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'

Add Linting

- script: |
    pip install flake8 black
    flake8 app/ tests/
    black --check app/ tests/
  displayName: 'Lint code'

Scheduled Builds

Add to azure-pipelines.yml:

schedules:
  - cron: "0 0 * * *"  # Daily at midnight
    displayName: Daily midnight build
    branches:
      include:
        - main

Multi-Stage Pipeline

stages:
  - stage: Build
    jobs:
      - job: BuildAndTest
        steps:
          # Build and test steps

  - stage: Deploy
    dependsOn: Build
    condition: succeeded()
    jobs:
      - deployment: DeployToStaging
        environment: 'staging'
        strategy:
          runOnce:
            deploy:
              steps:
                # Deployment steps

πŸ“Š Build Status

View your pipeline builds at:

https://dev.azure.com/YOUR-ORG/azure-pipeline-basic/_build

Latest Build Information:

  • Build Number: 20251012.1
  • Status: βœ… Succeeded
  • Duration: ~31 seconds
  • Tests: 3/3 passed

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests locally (pytest -v)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

πŸ“š Additional Resources

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

Atul Kamble

πŸ™ Acknowledgments

  • Azure DevOps team for excellent CI/CD platform
  • Python community for pytest framework
  • Contributors and users of this template

Quick Start Summary:

# Clone and setup
git clone https://github.com/atulkamble/azure-pipeline-basic.git
cd azure-pipeline-basic
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# Run locally
python app/hello.py
pytest -v

# Push to Azure DevOps
git remote add azure https://YOUR_PAT@dev.azure.com/YOUR-ORG/azure-pipeline-basic/_git/azure-pipeline-basic
git push azure main

# Create and run pipeline
az pipelines create --name "azure-pipeline-basic" --repository azure-pipeline-basic --repository-type tfsgit --branch main --yml-path azure-pipelines.yml --organization https://dev.azure.com/YOUR-ORG --project azure-pipeline-basic

Happy Building! πŸš€


πŸ—‘οΈ Cleanup and Deletion Commands

Delete Pipeline

Warning: These commands will permanently delete resources. Use with caution!

Delete a Specific Pipeline

# List pipelines to get the pipeline ID
az pipelines list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

# Delete pipeline by ID
az pipelines delete \
  --id PIPELINE_ID \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --yes

Delete a Build/Run

# List recent builds
az pipelines runs list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

# Delete a specific build (Note: This only deletes the build record, not the pipeline)
az pipelines build delete \
  --id BUILD_ID \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --yes

Delete Repository

Delete Azure Repo

# List repositories
az repos list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

# Delete repository
az repos delete \
  --id REPO_ID \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --yes

Alternative via Portal:

  1. Go to Repos β†’ Files
  2. Click repository dropdown
  3. Select Manage repositories
  4. Click ... β†’ Delete repository

Delete Project

# Delete entire project (includes all pipelines, repos, builds)
az devops project delete \
  --id PROJECT_ID_OR_NAME \
  --organization https://dev.azure.com/YOUR-ORG \
  --yes

Alternative via Portal:

  1. Go to Project settings (bottom left)
  2. Scroll to bottom β†’ Delete
  3. Type project name to confirm
  4. Click Delete

Clean Local Environment

Remove Virtual Environment

# Deactivate if active
deactivate

# Remove virtual environment directory
rm -rf .venv

Remove Git Remotes

# Remove Azure DevOps remote
git remote remove azure

# Remove origin remote (if needed)
git remote remove origin

# Verify remotes removed
git remote -v

Clean Python Cache

# Remove Python cache files
find . -type d -name "__pycache__" -exec rm -r {} +
find . -type f -name "*.pyc" -delete
find . -type f -name "*.pyo" -delete

# Remove pytest cache
rm -rf .pytest_cache

# Remove coverage files
rm -f .coverage
rm -rf htmlcov
rm -f coverage.xml

Complete Local Cleanup

# Remove all Python artifacts and virtual environment
rm -rf .venv
rm -rf __pycache__
rm -rf .pytest_cache
rm -rf htmlcov
rm -f .coverage
rm -f coverage.xml
find . -type d -name "__pycache__" -exec rm -r {} + 2>/dev/null
find . -type f -name "*.pyc" -delete
find . -type f -name "*.pyo" -delete

Revoke Personal Access Token (PAT)

Important: Always revoke PAT tokens when you no longer need them.

Via Portal:

  1. Go to https://dev.azure.com/YOUR-ORG
  2. Click profile icon β†’ Personal access tokens
  3. Find your token
  4. Click ... β†’ Revoke
  5. Confirm revocation

Security Best Practice:

  • Revoke PATs immediately if compromised
  • Use short expiration periods (30-90 days)
  • Create separate PATs for different purposes
  • Never commit PATs to version control

Remove Local Repository

# Navigate to parent directory
cd ..

# Remove entire project directory
rm -rf azure-pipeline-basic

Warning: This will delete all local files. Ensure you have pushed any important changes!

Verify Deletion

# Verify pipeline deleted
az pipelines list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

# Verify repository deleted
az repos list \
  --organization https://dev.azure.com/YOUR-ORG \
  --project azure-pipeline-basic \
  --output table

# Verify project deleted
az devops project list \
  --organization https://dev.azure.com/YOUR-ORG \
  --output table

Complete Cleanup Script

Save this as cleanup.sh for easy cleanup:

#!/bin/bash

# Complete cleanup script for azure-pipeline-basic

echo "🧹 Starting cleanup..."

# Set variables (update these)
ORG="YOUR-ORG"
PROJECT="azure-pipeline-basic"
PIPELINE_ID="15"  # Update with your pipeline ID

echo "πŸ“‹ Organization: $ORG"
echo "πŸ“‹ Project: $PROJECT"
echo ""

# Azure Resources Cleanup
read -p "Delete Azure Pipeline? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "πŸ—‘οΈ  Deleting pipeline..."
    az pipelines delete --id $PIPELINE_ID --organization https://dev.azure.com/$ORG --project $PROJECT --yes
fi

read -p "Delete Azure Project? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "πŸ—‘οΈ  Deleting project..."
    az devops project delete --id $PROJECT --organization https://dev.azure.com/$ORG --yes
fi

# Local Cleanup
read -p "Clean local Python artifacts? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "🧹 Cleaning Python cache..."
    rm -rf .venv
    rm -rf __pycache__
    rm -rf .pytest_cache
    rm -rf htmlcov
    rm -f .coverage
    rm -f coverage.xml
    find . -type d -name "__pycache__" -exec rm -r {} + 2>/dev/null
    find . -type f -name "*.pyc" -delete
fi

read -p "Remove Git remotes? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "πŸ”— Removing Git remotes..."
    git remote remove azure 2>/dev/null
    git remote remove origin 2>/dev/null
fi

echo ""
echo "βœ… Cleanup complete!"
echo ""
echo "πŸ“Œ Remember to:"
echo "   - Revoke Personal Access Tokens in Azure DevOps"
echo "   - Remove local directory if no longer needed: rm -rf azure-pipeline-basic"

Usage:

chmod +x cleanup.sh
./cleanup.sh

⚠️ Important Notes

Before Deleting:

  • βœ… Backup any important pipeline configurations
  • βœ… Export build history if needed
  • βœ… Save any custom YAML files
  • βœ… Document any custom settings

After Deleting:

  • βœ… Verify all resources are removed
  • βœ… Revoke all PAT tokens
  • βœ… Remove local clones if not needed
  • βœ… Clear browser cache/credentials

Cannot Delete:

  • Build history is retained for auditing (30-90 days)
  • Organization-level settings require admin access
  • Some resources may have retention policies

End of Documentation | Last Updated: October 12, 2025

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages