DEV Community

Jagan
Jagan

Posted on

Step by step for build , test and deploy using azuredevops pipeline

Creating an Azure Pipeline for building, testing, and publishing artifacts for a Python web application involves defining a pipeline configuration file in your source code repository. Here, I'll provide a step-by-step guide to creating a simple Azure Pipeline for a Python web application:

Step 1: Prerequisites

Ensure you have an Azure DevOps organization and project set up.
Have your Python web application code stored in a Git repository (e.g., Azure Repos or GitHub).
Step 2: Create an Azure Pipeline Configuration File

Create a file named azure-pipelines.yml in the root directory of your repository. This file will define the build, test, and artifact publishing stages of your pipeline.

Here's a basic example of an azure-pipelines.yml file:

trigger:

  • '*'

pool:
vmImage: 'ubuntu-latest'

stages:

  • stage: Build
    jobs:

    • job: BuildJob steps:
    • task: UsePythonVersion@0 inputs: versionSpec: '3.x' addToPath: true
    • script: | python -m venv venv source venv/bin/activate pip install -r requirements.txt displayName: 'Install Python dependencies'
    • script: | python -m unittest discover tests displayName: 'Run Unit Tests'
    • task: PublishPipelineArtifact@1 inputs: targetPath: '$(Build.ArtifactStagingDirectory)' artifact: 'webapp-artifact' condition: succeeded()
    • stage: Deploy jobs:
    • job: DeployJob steps:
      • download: current artifact: 'webapp-artifact' displayName: 'Download Artifact'

Matrix based upon on deployment

trigger:

  • main

pool:
vmImage: ubuntu-latest
strategy:
matrix:
Python38:
python.version: '3.8'
Python39:
python.version: '3.9'
Python310:
python.version: '3.10'

steps:

  • task: UsePythonVersion@0
    inputs:
    versionSpec: '$(python.version)'
    displayName: 'Use Python $(python.version)'

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

  • script: |
    pip install pytest pytest-azurepipelines
    pytest
    displayName: 'pytest'

Step 3: Configure Your Python Web Application

Ensure your Python web application is structured correctly with the required files:

requirements.txt: List of Python dependencies.
tests/: Directory containing your unit tests.
Any other necessary application files and directories.
Step 4: Create the Azure Pipeline

Go to your Azure DevOps project.
Navigate to Pipelines > New Pipeline.
Select your source code repository.
Choose "YAML" for the pipeline configuration.
In the YAML editor, make sure it reflects the content of your azure-pipelines.yml file.
Click "Save and Run" to create and trigger the pipeline.
Step 5: Monitor and Troubleshoot

Once the pipeline is running, you can monitor its progress and view logs and test results. If any issues arise, Azure DevOps provides a rich set of diagnostic tools to help you troubleshoot and fix them.

This example provides a basic starting point for your Azure Pipeline. Depending on your specific needs, you may need to add deployment steps, environment variables, or additional configurations to customize your pipeline further.

Regenerate

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay