Getting Azure Function under source control
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.
Caution! When you start to deploy AF with Azure DevOps, you will no longer be able to edit the AF inside the Azure portal.
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.
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.
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.