CI/CD in a nutshell


Deploying your API service in Azure DevOps using Infrastructure as Code involves several steps, including configuring the pipeline, setting up ARM/Bicep templates, and managing environment-specific settings. Here's a detailed guide to help you get started:

 

Steps Required for Deployment

1. Set Up Azure Resources

  • Define your resources in Azure (e.g., App Service, Application Insights, Key Vault).

  • Use ARM or Bicep templates to declare resources as Infrastructure as Code.

 

2. Repository Structure

Organize your files in your Git repository. Suggested structure:

/src - Your API service code

/iac - Infrastructure as Code templates (ARM/Bicep) 

/pipelines - Azure DevOps pipeline YAML files

 

3. Infrastructure Templates

 

  • Create an ARM/Bicep template to define Azure resources.

    Assume your project is an API for managing Addresses.

    Steps to Create a Bicep Template

    1. Define the Resource Group (Optional): If needed, define a Resource Group within the template or deploy it beforehand.

    2. Define the App Service Plan: This plan provides compute resources for the App Service.

    3. Define the App Service: The web application that will host Address.API.

    4. Define Application Insights: Monitor the performance and health of Address.API.

     

    Example of  Bicep Template:

    // Parameters for environment-specific settings
    param environment string
    param location string = 'West Europe'
    param appServicePlanSku string = 'P1v2' // Pricing tier// Resource Group (if you're not pre-creating it)
    resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = {
      name: 'address-api-rg-${environment}'
    }// App Service Plan
    resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
      name: 'address-api-plan-${environment}'
      location: location
      sku: {
        tier: 'PremiumV2'
        name: appServicePlanSku
        capacity: 1
      }
    }// App Service
    resource appService 'Microsoft.Web/sites@2022-03-01' = {
      name: 'address-api-${environment}'
      location: location
      properties: {
        serverFarmId: appServicePlan.id
      }
    }// Application Insights
    resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
      name: 'address-api-insights-${environment}'
      location: location
      properties: {
        Application_Type: 'web'
      }
    }// Output Resources
    output appServiceEndpoint string = appService.properties.defaultHostName


    Environment specific parameter file

    parameters-dev.json

    {
      "environment": "dev",
      "location": "West Europe",
      "appServicePlanSku": "P1v2"
    }



    Deploy template using Azure CLI:

    az deployment group create \
      --resource-group address-api-rg-dev \
      --template-file deploy.bicep \
      --parameters @parameters-dev.json



    Add step to your pipeline:

    - task: AzureResourceManagerTemplateDeployment@3
      inputs:
        deploymentScope: 'Resource Group'
        azureResourceManagerConnection: 'AzureServiceConnection'
        subscriptionId: '<Your-Subscription-ID>'
        action: 'Create Or Update Resource Group'
        resourceGroupName: 'address-api-rg-$(environment)'
        location: 'West Europe'
        templateLocation: 'Linked artifact'
        csmFile: 'iac/deploy.bicep'
        csmParametersFile: 'iac/parameters-$(environment).json'



  • Example Bicep Template for App Service and Application Insights:

     

    resource appService 'Microsoft.Web/sites@2022-03-01' = {
      name: 'my-app-service-${environment}'
      location: 'East US'
      properties: {
        serverFarmId: appServicePlan.id
      }
    }

    resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
      name: 'my-app-insights-${environment}'
      location: 'East US'
      properties: {
        Application_Type: 'web'
      }
    }

 

4. Azure DevOps Pipeline

 

  • Configure pipelines to automate build and deployment.

  • Example pipeline YAML for Build:

     

    trigger:
      branches:
        include:
          - main

    pool:
      vmImage: 'windows-latest'

    steps:
      - task: UseDotNet@2
        inputs:
          packageType: 'sdk'
          version: '9.x'

      - script: dotnet build
        displayName: 'Build Application'


     

  • Example pipeline YAML for Deployment:

     

    trigger:
      branches:
        include:
          - main

    pool:
      vmImage: 'windows-latest'

    steps:
      - task: AzureResourceManagerTemplateDeployment@3
        inputs:
          deploymentScope: 'Resource Group'
          azureResourceManagerConnection: 'AzureServiceConnection'
          subscriptionId: '<Subscription ID>'
          action: 'Create Or Update Resource Group'
          resourceGroupName: 'my-resource-group-${environment}'
          location: 'East US'
          templateLocation: 'Linked artifact'
          csmFile: 'iac/deploy.bicep'
          csmParametersFile: 'iac/parameters-${environment}.json'

 

5. Environment-Specific Parameters

 

  • Create parameter files for dev and prod environments.

  • Example parameters-dev.json:

     

    {
      "appServiceName": {
        "value": "my-app-service-dev"
      },
      "appInsightsName": {
        "value": "my-app-insights-dev"
      }
    }

     

  • Example parameters-prod.json:

     

    {
      "appServiceName": {
        "value": "my-app-service-prod"
      },
      "appInsightsName": {
        "value": "my-app-insights-prod"
      }
    }

 

6. Secure Configuration

  • Use Azure Key Vault to store sensitive information, such as connection strings and secrets.

  • Ensure pipelines retrieve secrets securely using Key Vault tasks.

 

7. Testing and Validation

  • Test your pipeline in the dev environment first.

  • Deploy to prod once everything is validated.

 

By following these steps and using the provided templates and pipeline examples, you can deploy your API service seamlessly across dev and prod environments. Let me know if you need help with specific configurations or troubleshooting! 🚀


No files yet, migration hasn't completed yet!