Getting Azure Function under source control

Martin Therkelsen/ October 31, 2021/ Automation, Azure, DevOps, Microsoft/ 5 comments

Intro
In this post, I want to show you how you can easily set up Azure DevOps to control your Azure Function Apps (AF). Out of the box, AF supports editing in the portal or using VS Code / Visual Studio for both editing and updating. While both methods are fine, I prefer to use Azure DevOps for AF instead, enabling source control. Want to know how I do this? Read on, and I will show you. If you prefer to see it on a video instead, you can find it on my YouTube below.

https://www.youtube.com/watch?v=itLCNLGz9XI

Caution! When you start to deploy AF with Azure DevOps, you will no longer be able to edit the AF inside the Azure portal.

Architecture

Create Azure DevOps environment
First, I create a new project in Azure DevOps, creating a new repository in that project. I will then clone the repository to my local machine to use VS Code to write my AF.

I click on “Clone in VS Code.”

I click on “Open” when prompted to open the link.

Again, I click on “Open” when prompted.

I select the folder location for the repository.

Finally, I click on “Open” to open the repository in VS Code.

I now have my local repository configured, so I can now create the AF in Azure.

Azure function
I have created an AF with all the defaults, so I want to create the first function. I do this by clicking on “Create.”

I choose “Develop in portal,” the other option requires plugins or downloads to my local machine to deploy functions. In this case, I select “HTTP trigger” and name the function “Demo,” finally, I click on “Create.”

Now my AF is created, and I can see the status below.

I want to develop my AF in VS code and deploy it with Azure DevOps, so the first thing I do is download the function files to my computer. I download the files from the “Overview” section on the function app.

I choose the settings as shown and click on “Download.”

The download is a ZIP file which I will extract into my repository folder. I have created the folder structure below that fits into how I like to arrange my files.

I can now use VS Code to develop my AF and push the changes into Azure DevOps. I have made a few changes to the PS1 file inside the “Demo” folder.

I can now push my files into Azure DevOps to deploy my updated function app with a pipeline.

Create an Azure DevOps pipeline
All my files are in the Azure DevOps repository, and I can view them in the portal.

The next thing I do is to create a pipeline. In this guide, I will be using the YAML-based pipeline to deploy my code into Azure. So I click on “Pipelines” and then on “Create pipeline.”

I will choose “Azure Repos Git” as the location for my code.

I select my repository.

I select “Start pipeline.”

I remove the code shown below from the pipeline.

I use the assistant to add the tasks which deploy the code into Azure.

The deployment requires the files to be zipped, so I added an “Archive” task to the steps in my pipeline.

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(System.DefaultWorkingDirectory)/Artifacts/AzureFunctions/func-azurefunctionblog'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true

Next, I will deploy this zip file into my AF. I chose my service connection to Azure. If you don’t have this created, look at this documentation site from Microsoft that describes how to configure it. I can then select the App type, “Function App on Windows,” and then choose my Function App. For the package, I paste the archive path I created in the previous step.

- task: AzureFunctionApp@1
  inputs:
    azureSubscription: 'Cloudninja Azure'
    appType: 'functionApp'
    appName: 'func-AzureFunctionBlog'
    package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    deploymentMethod: 'auto'

I click on “Save and run,” which will kick off the first deployment to Azure.

In my case, the deployment took 1m 54s to complete, and my AF is now live with the changes I made.

As mentioned at the start of this post, the AF can no longer be edited in the portal. When deployed with DevOps, all changes must come from a deployed zip file.

Conclusion
I hope this post helps you get all your Azure Function Apps under source control since I know firsthand that any changes made in the portal can be tough to revert based on memory or a lot of additional code commented out. Using Azure DevOps to deploy Azure Functions makes it easier to collaborate on the development, and it ensures that you always have a trail of changes to your code.

Share this Post

5 Comments

  1. Let’s try to go full code based all the way and not use any UI clicking.
    Just add some tasks to use the az cli and create the function from code/variables that are part of the yaml pipeline πŸ™‚

    1. I agree, but that would be creating Azure functions from scratch, this guide was to get already existing functions under source control. Thanks for the feedback

  2. Pingback: Top Stories from the Microsoft DevOps Community – 2021.11.05 - IT Skills You Need

  3. Pingback: Top Stories from the Microsoft DevOps Community – 2021.11.05 - Power Community

  4. Pingback: Top Stories from the Microsoft DevOps Community – 2021.11.05 - Azure DevOps Blog

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*