Azure Pipelines is a cloud service that you can use to automatically build and test your code project and make it available to other users. It combines continuous integration (CI) and continuous delivery (CD) to constantly and consistently test and build your code and ship it to any target.

Key Concepts

  1. Continuous Integration (CI):

    • Automatically build and test code every time a team member commits changes to version control.
    • Helps catch bugs early in the development cycle, which makes them less expensive to fix.
  2. Continuous Delivery (CD):

    • Automatically deploys code to a production environment after it passes the CI pipeline.
    • Ensures that the code is always in a deployable state.
  3. Pipeline:

    • A series of steps that include building, testing, and deploying code.
    • Can be defined using YAML or through the classic editor in the Azure DevOps portal.
  4. Agent:

    • A machine that runs the pipeline jobs.
    • Can be Microsoft-hosted or self-hosted.
  5. Stages, Jobs, and Steps:

    • Stages: High-level divisions of the pipeline, such as build, test, and deploy.
    • Jobs: A collection of steps that run sequentially on the same agent.
    • Steps: Individual tasks, such as running a script or installing dependencies.

Setting Up Azure Pipelines

Step 1: Create a New Pipeline

  1. Navigate to your Azure DevOps project.
  2. Select Pipelines from the left-hand menu.
  3. Click on New pipeline.
  4. Choose the source where your code is stored (e.g., GitHub, Azure Repos).

Step 2: Configure the Pipeline

You can configure your pipeline using YAML or the classic editor. Here, we'll use YAML for its flexibility and version control benefits.

Example YAML Pipeline Configuration

# .azure-pipelines.yml
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UseNode@2
  inputs:
    version: '14.x'
- script: |
    npm install
    npm run build
  displayName: 'Install dependencies and build'
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

Explanation of the YAML Configuration

  • trigger: Specifies the branches that will trigger the pipeline. In this case, it triggers on changes to the main branch.
  • pool: Defines the agent pool. Here, we use a Microsoft-hosted agent with the latest Ubuntu image.
  • steps: A list of tasks to be executed.
    • UseNode@2: Uses Node.js version 14.x.
    • script: Runs a shell script to install dependencies and build the project.
    • PublishBuildArtifacts@1: Publishes the build artifacts to a container.

Step 3: Run the Pipeline

  1. Save and run the pipeline.
  2. Monitor the pipeline's progress in the Azure DevOps portal.
  3. Check the logs for any errors or warnings.

Practical Exercise

Exercise: Create a Simple CI Pipeline

  1. Objective: Set up a CI pipeline for a simple Node.js application.
  2. Steps:
    • Create a new Azure DevOps project.
    • Push your Node.js application to a repository in Azure Repos.
    • Create a new pipeline using the YAML configuration provided above.
    • Run the pipeline and ensure it completes successfully.

Solution

  1. Create a New Azure DevOps Project:

    • Navigate to Azure DevOps.
    • Click on New Project and follow the prompts.
  2. Push Your Node.js Application:

    • Initialize a Git repository in your Node.js project directory.
    • Add the remote URL for your Azure Repos repository.
    • Push your code to the repository.
  3. Create and Run the Pipeline:

    • Follow the steps outlined in the "Setting Up Azure Pipelines" section.
    • Use the provided YAML configuration.
    • Save and run the pipeline.

Common Mistakes and Tips

  • Incorrect YAML Syntax: YAML is indentation-sensitive. Ensure proper indentation to avoid syntax errors.
  • Missing Dependencies: Make sure all necessary dependencies are listed in your package.json file.
  • Agent Pool Issues: If using a self-hosted agent, ensure it is properly configured and running.

Conclusion

In this section, you learned about Azure Pipelines, including key concepts like CI/CD, pipelines, agents, stages, jobs, and steps. You also set up a simple CI pipeline using YAML and ran it in Azure DevOps. This foundational knowledge prepares you for more advanced topics in Azure DevOps and continuous delivery practices.

© Copyright 2024. All rights reserved