You are currently viewing How to create and execute Azure Pipelines using REST API?

How to create and execute Azure Pipelines using REST API?

How to create and execute Azure Pipelines using REST API?

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

Today we are going to discuss how to create and execute Azure Pipelines using REST API. We will also discuss components and structure of Azure Pipelines.

Azure Pipeline Structure:

A pipeline is one or more stages that describe a CI/CD process. Stages are the major divisions in a pipeline. The stages “Build this app,” “Run these tests,” and “Deploy to preproduction” are good examples. A stage is one or more jobs, which are units of work assignable to the same machine. You can arrange both stages and jobs into dependency graphs. Examples include “Run this stage before that one” and “This job depends on the output of that job.” A job is a linear series of steps. Steps can be tasks, scripts, or references to external templates.

This hierarchy is reflected in the structure of a YAML file (azure-pipelines.yml) like:

  • Pipeline
    • Stage A
      • Job 1
        • Step 1.1
          • Task 1.1.1
          • Task 1.1.2
        • Step 1.2
      • Job 2
    • Stage B

Components of Azure Pipeline:

Pipeline: A pipeline defines the continuous integration and deployment process for your app. It’s made up of one or more stages. It can be thought of as a workflow that defines how your test, build, and deployment steps are run.

Stage: A stage is a collection of related jobs. By default, stages run sequentially. Each stage starts only after the preceding stage is complete unless otherwise specified via the dependsOn property.

Job: A stage contains one or more jobs. Each job runs on an agent. A job represents an execution boundary of a set of steps. All of the steps run together on the same agent. Jobs are most useful when you want to run a series of steps in different environments. For example, you might want to build two configurations – x86 and x64. In this case, you have one stage and two jobs. One job would be for x86 and the other job would be for x64.

Step: A step is the smallest building block of a pipeline. For example, a pipeline might consist of build and test steps. A step can either be a script or a task. A task is simply a pre-created script offered as a convenience to you.

Task: A task is the building block for defining automation in a pipeline. A task is packaged script or procedure that has been abstracted with a set of inputs.

Script: A script runs code as a step in your pipeline using command line, PowerShell, or Bash. You can write cross-platform scripts for macOS, Linux, and Windows. Unlike a task, a script is custom code that is specific to your pipeline.

Trigger: A trigger is something that’s set up to tell the pipeline when to run. You can configure a pipeline to run upon a push to a repository, at scheduled times, or upon the completion of another build. All of these actions are known as triggers.

Pool: The pool keyword specifies which agent to use to execute your pipeline, stage, or jobs.

Variables: You can pass variables in your Azure Pipeline using variable block.

How to create and execute Azure Pipelines using REST API?

Image source: https://docs.microsoft.com/en-us/azure/devops/pipelines/get-started/media/key-concepts-overview.svg?view=azure-devops

I know its hard to graphs all these at once, particularly if you are just starting with Azure Pipeline. But don’t worry as I will be explaining each component of an Azure pipeline separately in the upcoming blogs. I have intentionally left out some components like resource, template, parameters, artifacts etc. as we are just getting started, once again I will cover those as well at a later stage once your basic understanding is clear.

Next, we are going to create and execute a new Azure pipeline using REST API.

How to create and execute Azure Pipelines using REST API?

Prerequisites:

Step 1: Check if you can make API call to your Azure DevOps account.

Step 2: Create a new Azure DevOps project.

Step 3: Clone the default Azure Repo created in the new Azure DevOps project.

Step 4: Create the Azure Pipeline configuration file and push it to your Azure Repo.

Step 5: Create a new Azure Pipeline using REST API.

Now we are ready to execute our Azure Pipeline. We can execute our Azure Pipeline in two ways, either by pushing any change to our Azure Repo (Since Trigger in the pipeline configuration file is set to *, any push to any branch in the Azure repo will result execution of your Azure Pipeline) or using REST API to trigger the pipeline manually. I will cover both 😊

Step 6: Update the Azure Pipeline configuration file and push the changes to your Azure Repo.

As soon as the change is pushed to your Azure Repo, a new Azure Pipeline will get triggered.

How to create and execute Azure Pipelines using REST API?

Next, we will execute our Azure Pipeline manually using REST API.

Step 7: Execute an Azure Pipeline manually using REST API.

Step 8: Clean up.

Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation

https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops

This Post Has One Comment

  1. Avatar
    sivasai

    hi, Can you please help in create and executing release as well using rest api. as of now with the above article we are able to create pipeline automatically. like wise need help in release and deploy applicaiton to AZURE

Comments are closed.