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
- Login to the Azure Devops portal if you have not yet already.
- Create a new Azure Devops project.
- Under Project Name name your project Hello Azure.
- Click on Create project.
Create a build pipeline
- Login to the Azure Devops portal if you have not yet already.
- Navigate to your Hello Azure project.
- Click on Pipelines.
- Click on Builds.
- Click on New pipeline.
- Under Connect select the source control provider your code is hosted under. In my case GitHub.
- If you have not granted Azure Devops access to your GitHub account you will be redirected to a screen to grant access.
- Under Select select your repository. In my case
HelloAzure
.- You will be redirected to a screen to grant Azure Devops access to your repository.
- Under Configure select Starter Pipeline
-
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"
-
trigger
lets the pipeline know what can trigger it. In this case a checkin tomaster
. -
pool
is the environment we will run our build in. -
steps
are the build steps.-
The first step runs two command line commands
$ dotnet restore $ dotnet build --configuration Release
- The first command restores any nuget packages our solution depends on.
- The second command will build the entire solution in release configuration.
-
The second step uses the dotnet core CLI to run a publish command on the project.
- We specify the configuration to be
Release
and the output of the publish to go into a directory namedpublish_output
. - We specify the
HelloAzure.Functions
project as the one to publish. - We set
publishWebProjects
tofalse
to prevent the dotnet cli from throwing an exception while it looks for web projects. -
We set
modifyOutputPath
to false in order to keep the output directory flat. This will be an important piece to publishing.
IfmodifyOutputPath
is set to true we will get get apublish_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
We will be running a separate step to archive the
publish_output
directory so we setzipAfterPublishing
tofalse
.
- We specify the configuration to be
The third step uses the
ArchiveFiles
task to turnpublish_output
into an archive that has the name of the build.The last step uses the
PublishBuildArtifacts
task to publish the build artifacts archive in a spot that our release pipeline can look for it.
-
-
Once you are happy with the
azure-pipeline.yml
file click on Save and run
-
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
.- This will add the
azure-pipelines.yml
file to the root of your repo and commit the change.
- This will add the
-
The build will now attempt to run.
- 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.
- If you get build failures verify that the branch you commited the
Create a release pipeline
- Login to the Azure Devops portal if you have not yet already.
- Navigate to your Hello Azure project.
- Click on Pipelines.
- Click on Releases.
- Click on New pipeline.
- In the Select a template pane select Or start with an Empty job.
Adding a Stage
- The Stage pane will open after selecting Or start with an Empty job.
- In the Stage pane under Stage name enter
Dev
. - Close the Stage pane.
Adding an Artifact
- Click on Add an artifact.
- In the Add an artifact pane ensure Source type is set to Build.
- Ensure Project is set to Hello Azure.
- Under Source (build pipeline) select HelloAzure.
- Click on Add
- Click on Continuous deployment trigger
- In the Continuous deployment trigger pane toggle the switch for Continuous deployment trigger.
- Under Build branch filters select the branch you'd like to run on.
Adding tasks to the Stage
- Click on 1 job, 0 task under the Dev Stage.
- In the Tasks list click the + icon on the Agent job list item.
- In the Add tasks pane search for Azure Functions.
- Click Add on the Azure Functions task.
Configure the Azure Function App Deploy task
- In the configuration pane under Azure subscription select your Free Trial subscription.
- Click on Authorize
- Under App type select Function App on Windows
- Under App name select your Function App name.
- Under Package or folder ensure the value is set to
$(System.DefaultWorkingDirectory)/*/.zip
. - Click on Save in the menu-bar.
- Click on Create release in the menu-bar.
- Follow the steps in the Create release pane.
- Navigate back to the pipeline details page and click on Deploy under dev.
- Follow the steps in the Deploy pane.
- 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.
Top comments (1)
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.