loading...

Introduction to Azure Functions Pt. 5 - Automate the function deployment with a CI/CD pipeline

jsmithdenverdev profile image Jacob Smith ・5 min read

Introduction to Azure Functions (6 Part Series)

1) Introduction to Azure Functions 2) Introduction to Azure Functions Pt. 1 - Creating the .NET Core Solution 3 ... 4 3) Introduction to Azure Functions Pt. 2 - Creating the Azure Function 4) Introduction to Azure Functions Pt. 3 - Creating the Azure Function resources 5) Introduction to Azure Functions Pt. 4 - Deploying the Azure Function 6) Introduction to Azure Functions Pt. 5 - Automate the function deployment with a CI/CD pipeline

Introduction

In this post we will automate the deployment process of our Azure Function. We will setup a CI/CD process in Azure Devops that is run when we check into master. Next we will create a build pipeline that will build our entire solution and output an artifact for the function project. Finally we will create a release pipeline that will run automatically after the build succeeds and will take the build artifact and deploy it to our Function App.

Prerequisites

An Azure Devops account. Sign up for an account here. I am using the free tier.

Create an Azure Devops project

  1. Login to the Azure Devops portal if you have not yet already.
  2. Create a new Azure Devops project.
  3. Under Project Name name your project Hello Azure.
  4. Click on Create project.

Create a build pipeline

  1. Login to the Azure Devops portal if you have not yet already.
  2. Navigate to your Hello Azure project.
  3. Click on Pipelines.
  4. Click on Builds.
  5. Click on New pipeline.
  6. Under Connect select the source control provider your code is hosted under. In my case GitHub.
    1. If you have not granted Azure Devops access to your GitHub account you will be redirected to a screen to grant access.
  7. Under Select select your repository. In my case HelloAzure.
    1. You will be redirected to a screen to grant Azure Devops access to your repository.
  8. Under Configure select Starter Pipeline
  9. Under Review modify the contents of azure-pipelines.yml to be the following.

    trigger:
    - master
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - script: |
        dotnet restore
        dotnet build --configuration Release
    - task: DotNetCoreCLI@2
      inputs:
        command: publish
        arguments: '--configuration Release --output publish_output'
        projects: 'HelloAzure.Functions/HelloAzure_Functions.csproj'
        publishWebProjects: false
        modifyOutputPath: false
        zipAfterPublish: false
    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: "$(System.DefaultWorkingDirectory)/publish_output"
        includeRootFolder: false
        archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
        ArtifactName: "drop"
    
    
    1. trigger lets the pipeline know what can trigger it. In this case a checkin to master.
    2. pool is the environment we will run our build in.
    3. steps are the build steps.

      1. The first step runs two command line commands

        $ dotnet restore
        $ dotnet build --configuration Release
        
        1. The first command restores any nuget packages our solution depends on.
        2. The second command will build the entire solution in release configuration.
      2. The second step uses the dotnet core CLI to run a publish command on the project.

        1. We specify the configuration to be Release and the output of the publish to go into a directory named publish_output.
        2. We specify the HelloAzure.Functions project as the one to publish.
        3. We set publishWebProjects to false to prevent the dotnet cli from throwing an exception while it looks for web projects.
        4. We set modifyOutputPath to false in order to keep the output directory flat. This will be an important piece to publishing.
          If modifyOutputPath is set to true we will get get a publish_output that looks like the following.

          .
          +-- publish_output/
          |   +-- HelloAzure.Functions/
          |       +-- ... published files
          
          

          We want an output that looks like the following.

          .
          +-- publish_output/
          |   +-- ... published files
          
        5. We will be running a separate step to archive the publish_output directory so we set zipAfterPublishing to false.

      3. The third step uses the ArchiveFiles task to turn publish_output into an archive that has the name of the build.

      4. The last step uses the PublishBuildArtifacts task to publish the build artifacts archive in a spot that our release pipeline can look for it.

  10. Once you are happy with the azure-pipeline.yml file click on Save and run

  11. In the Save and run pane enter a commit message and choose a branch to commit the file to. In my case I just went with master.

    1. This will add the azure-pipelines.yml file to the root of your repo and commit the change.
  12. The build will now attempt to run.

    1. If you get build failures verify that the branch you commited the azure-pipelines-yml file to is a branch that has your solution in it.

Create a release pipeline

  1. Login to the Azure Devops portal if you have not yet already.
  2. Navigate to your Hello Azure project.
  3. Click on Pipelines.
  4. Click on Releases.
  5. Click on New pipeline.
  6. In the Select a template pane select Or start with an Empty job.

Adding a Stage

  1. The Stage pane will open after selecting Or start with an Empty job.
  2. In the Stage pane under Stage name enter Dev.
  3. Close the Stage pane.

Adding an Artifact

  1. Click on Add an artifact.
  2. In the Add an artifact pane ensure Source type is set to Build.
  3. Ensure Project is set to Hello Azure.
  4. Under Source (build pipeline) select HelloAzure.
  5. Click on Add
  6. Click on Continuous deployment trigger
  7. In the Continuous deployment trigger pane toggle the switch for Continuous deployment trigger.
  8. Under Build branch filters select the branch you'd like to run on.

Adding tasks to the Stage

  1. Click on 1 job, 0 task under the Dev Stage.
  2. In the Tasks list click the + icon on the Agent job list item.
  3. In the Add tasks pane search for Azure Functions.
  4. Click Add on the Azure Functions task.

Configure the Azure Function App Deploy task

  1. In the configuration pane under Azure subscription select your Free Trial subscription.
    1. Click on Authorize
  2. Under App type select Function App on Windows
  3. Under App name select your Function App name.
  4. Under Package or folder ensure the value is set to $(System.DefaultWorkingDirectory)/*/.zip.
  5. Click on Save in the menu-bar.
  6. Click on Create release in the menu-bar.
  7. Follow the steps in the Create release pane.
  8. Navigate back to the pipeline details page and click on Deploy under dev.
  9. Follow the steps in the Deploy pane.
  10. Your deployment will now be queued.

Recap

In this post we setup a CI/CD process in Azure devops. We created a build pipeline that will automatically build our project. We then created a release pipeline that will take those build artifacts and automatically deploy them to our Function App.

Introduction to Azure Functions (6 Part Series)

1) Introduction to Azure Functions 2) Introduction to Azure Functions Pt. 1 - Creating the .NET Core Solution 3 ... 4 3) Introduction to Azure Functions Pt. 2 - Creating the Azure Function 4) Introduction to Azure Functions Pt. 3 - Creating the Azure Function resources 5) Introduction to Azure Functions Pt. 4 - Deploying the Azure Function 6) Introduction to Azure Functions Pt. 5 - Automate the function deployment with a CI/CD pipeline

Posted on by:

jsmithdenverdev profile

Jacob Smith

@jsmithdenverdev

Outside of work I am an avid outdoorsman. I am a member of Douglas County Search and Rescue and on our ATV, Technical Rescue, and Medical teams.

Discussion

markdown guide
 

Thanks Jacob for the amezing article and great series on Azure functions.👍🏻
In above post CI is setup using YAML and CD is through Azure devops client UI. We can also setup both CI and CD through YAML in Single pipeline and based on conditions we can decide we have to release it on not.