Perform Build and Release in a Single YAML File

Azure Pipelines is a cloud service that helps developers and administrators simplify the end-to-end deployment of applications and resources to Azure. Azure Pipeline provides continuous integration (CI) and continuous delivery (CD) capabilities to help the users consistently build, test, and deploy the code seamlessly.

Creating and managing separate pipelines for build and release activities and can be a tedious and painstaking job. Azure Pipeline now gives you the freedom to create a single pipeline for both build and release activities. You can customize your pipeline further by developing the pipeline YAML.

In this article, we would learn to create a single YAML for build and release activities. As part of YAML, we will build the Azure Functions .Net project, provision Function App using an ARM template, and then deploy functions to Azure Function App.

Pre-requisites

You need to ensure below items are completed before implementing the solution:

  1. An Azure Subscription
  2. Azure DevOps Git Hub.
  3. ARM Template for Azure Function App
  4. Azure Functions .Net project.
  5. Service Connections for the respective services.

Flowchart

The below image depicts the steps to be implemented in the YAML:

Steps

Build Function Solution

This step uses DotNetCoreCLI task to build the Azure Function solution developed in C#.

#Build Functions Solution
- task: DotNetCoreCLI@2
  displayName: 'Build Azure Function Solution'
  enabled: true
  inputs:
    projects: '<Project Location>/*.csproj'
    arguments: '--output publish_output --configuration Release'

Publish Functions Solution

This step uses DotNetCoreCLI task to publish the Azure Function solution.

#Publish Functions Solution
- task: DotNetCoreCLI@2
  displayName: 'Publish Azure Functions'
  enabled: true
  inputs:
    command: publish
    publishWebProjects: false
    projects: '<Project Location>/*.csproj'
    arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'

Publish Build Artifact : Functions Solution

This step uses PublishBuildArtifacts task to publish the Function solution build artifact.

#Publish Artifact Functions Solution
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: Azure Function'
  enabled: true
  inputs:
    ArtifactName: 'drop_azurefunctions'

Copy ARM Template

This step uses CopyFiles task to copy ARM templates from project location to deployment folder under build artifact staging directory.

#Copy ARM Template
- task: CopyFiles@2
  displayName: 'Copy ARM Templates Artifact'
  enabled: true
  inputs:
    SourceFolder: '<ARM Template Location>'
    Contents: '*.json'
    TargetFolder: '$(Build.ArtifactStagingDirectory)/Deployment'

Publish Build Artifact : ARM Template

This step uses PublishBuildArtifacts task to publish ARM templates build artifacts.

#Publish ARM Template
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: ARM Deployment'
  enabled: true
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/Deployment'
    ArtifactName: 'drop_deployment'

Provision The Resource

This step uses AzureResourceManagerTemplateDeployment task to provision resources using ARM templates.

#Provision The Services
- task: AzureResourceManagerTemplateDeployment@3
  displayName: 'ARM Template deployment: Resources'
  name: ARMDeploy
  enabled: true
  inputs:
    azureResourceManagerConnection: '<Service Connection Name>'
    subscriptionId: '<Subscription ID>'
    resourceGroupName: '<Resource Group Name>'
    location: '<Azure Region>'
    csmFile: '$(Build.ArtifactStagingDirectory)/Deployment/armtemplate.json'
    csmParametersFile: '$(Build.ArtifactStagingDirectory)/Deployment/param-armtemplate.json'
    deploymentOutputs: DeployOutput

Deploy Functions to Functions App

This step uses AzureFunctionApp task to deploy functions to Azure Function App provisioned in previous step. The deployment method used in this step is ZipDeploy.

#Deploy Functions to Functions App
- task: AzureFunctionApp@1
  displayName: 'Azure Function Deploy'
  inputs:
    azureSubscription: '<Service Connection Name>'
    appType: functionApp
    appName: '<Function App Name>'
    package: '$(Build.ArtifactStagingDirectory)/**/*.zip'
    deploymentMethod: zipDeploy 

Final YAML File

Below is a sample YAML file to provision Function App, build .Net functions, and then deploy functions to Functions App:

pool:
  name: GenericPool1
steps:
#Build Functions Solution
- task: DotNetCoreCLI@2
  displayName: 'Build Azure Function Solution'
  enabled: true
  inputs:
    projects: '<Project Location>/*.csproj'
    arguments: '--output publish_output --configuration Release'
#Publish Functions Solution
- task: DotNetCoreCLI@2
  displayName: 'Publish Azure Functiosns'
  enabled: true
  inputs:
    command: publish
    publishWebProjects: false
    projects: '<Project Location>/*.csproj'
    arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
#Publish Artifact Functions Solution
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: Azure Function'
  enabled: true
  inputs:
    ArtifactName: 'drop_azurefunctions'
#Copy ARM Template
- task: CopyFiles@2
  displayName: 'Copy ARM Templates Artifact'
  enabled: true
  inputs:
    SourceFolder: '<ARM Template Location>'
    Contents: '*.json'
    TargetFolder: '$(Build.ArtifactStagingDirectory)/Deployment'
#Publish ARM Template
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: ARM Deployment'
  enabled: true
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/Deployment'
    ArtifactName: 'drop_deployment'
#Provision The Services
- task: AzureResourceManagerTemplateDeployment@3
  displayName: 'ARM Template deployment: Resources'
  name: ARMDeploy
  enabled: true
  inputs:
    azureResourceManagerConnection: '<Service Connection Name>'
    subscriptionId: '<Subscription ID>'
    resourceGroupName: '<Resource Group Name>'
    location: '<Azure Region>'
    csmFile: '$(Build.ArtifactStagingDirectory)/Deployment/armtemplate.json'
    csmParametersFile: '$(Build.ArtifactStagingDirectory)/Deployment/param-armtemplate.json'
    deploymentOutputs: DeployOutput
#Deploy Functions to Functions App
- task: AzureFunctionApp@1
  displayName: 'Azure Function Deploy'
  inputs:
    azureSubscription: '<Service Connection Name>'
    appType: functionApp
    appName: '<Function App Name>'
    package: '$(Build.ArtifactStagingDirectory)/**/*.zip'
    deploymentMethod: zipDeploy 

We have learnt to perform build and release activities in a single YAML. I am sure this would ease the building and deployment of your resources.



Categories: Azure, Azure DevOps

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: