Zero to Azure Hero with ASP.NET Core, Blazor, Azure DevOps, Cognitive Services, SonarCloud and App Services - Part 3
In part 2 of this blog post series we started to build out our Twitter Sentiment Analysis functionality and tests, this blog post will cover pushing our library to GitHub and creating our first CI/CD pipeline using Azure DevOps services.
I'm assuming you are familiar with Git and GitHub. If you aren't you can find some documentation here to get you started https://help.github.com/articles/set-up-git/
Login to your GitHub account and navigate to the Create New Repository page. From here give your repository a name, in this case I'm going to use 'TwitterSentiment' and click create repository.
It will provide instructions on creating and pushing your repository from the command line (from the root of your project folder). If using my commands below, ensure you replace the GitHub project url with your own
git init git add . git commit -m "initial commit" git remote add origin https://github.com/stevenknox/TwitterSentiment.git git push -u origin master
You may be prompted to login and once completed if you refresh the GitHub page in your browser, you should now see your code.
Next we will create our first pipeline on Azure DevOps. You can find out more about Azure DevOps here
If you are already an Azure subscriber, you can go ahead and click 'Sign-In to Azure DevOps' or you can click 'Start for Free' to register.
Once Signed In, click 'Create Project' and fill in the project name, I'm going to go with TwitterSentiment again, and you can choose to keep the project private or set it to public. I'm going to set mine to 'Public'
Once the project has been created you will land on the Summary screen, with the different Azure DevOps features listed in the menu to the left.
This blog post will focus on Pipelines and Artifacts however feel free to explore some of the other areas such as leveraging Boards for managing work items and kanban boards etc
Ok, so click on Pipelines then 'New pipeline' on the next screen
Select GitHub and Authorize with your GitHub credentials when prompted
Your repository should be listed on the next screen, find the the name of your GitHub project and select it from the list
You will then be prompted to select a template, we will go for ASP.NET Core
The initial pipeline YAML will be presented, we can directly make any changes here, for example I'm going to change 'Ubuntu 16.04' to 'VS2017-Win2016'
Once you have made that change, click Save and run at the top right, then Save and run again when prompted. This will save your YAML file back to our GitHub repository.
The pipeline will now configure and trigger the first run. If everything has been setup correctly the pipeline job should complete successfully and you should be presented with the following:
Right now our pipeline will trigger every time a commit is pushed to our GitHub repository and the pipeline will restore any packages needed from NuGet (we added several earlier when setting up our project) then build our projects in Release mode.
It's a great start but we now want to run our unit tests and fail the build if they don't pass. We will also add some static analysis using SonarCloud and finally package up our library and push to Azure Artifacts to make it available for consumption or our Web application (and any other 3rd party applications that wish to use the library)
Before we return to our command line to continue building out our YAML file, return to your GitHub project page. You will see there is now a new file in the root of the repository 'azure-pipelines.yml'. Clicking on the file will show you the YAML script we modified during the pipeline setup.
Return to your command line (in the root of our project) and run the following command, we need to pull the YAML file down from GitHub so we can start making changes.
You should see similar output below with azure-pipelines.yml shown as a new file.
Back in VS Code, open the pipeline and replace your YAML with the following:
# ASP.NET Core # Build and test ASP.NET Core projects targeting .NET Core. # Add steps that run tests, create a NuGet package, deploy, and more: # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core pool: vmImage: 'VS2017-Win2016' variables: buildConfiguration: 'Release' projectName: 'TwitterSentiment' steps: - script: | dotnet build $(projectName)/$(projectName).csproj --configuration $(buildConfiguration) dotnet build $(projectName).Tests/$(projectName).Tests.csproj --configuration $(buildConfiguration) dotnet test $(projectName).Tests/$(projectName).Tests.csproj --configuration $(buildConfiguration) --logger trx dotnet publish $(projectName)/$(projectName).csproj --configuration $(buildConfiguration) --output $BUILD_ARTIFACTSTAGINGDIRECTORY displayName: 'Build & Test' - task: PublishTestResults@2 inputs: testRunner: VSTest testResultsFiles: '**/*.trx'
We have added a 'ProjectName' parameter then that we can use in our tasks and updated our script to run our restore, build, test and publish commands.
Save the file then return to the command line and run the following:
git add . git commit -m "Update pipeline to run tests and collect coverage" git push
Return to Azure Devops and view the latest running/completed build
Once completed we should see the following:
Clicking on Tests should show our Green test run
And finally under Summary we should see
Our first Azure DevOps pipeline has now been configured to retrieve code pushed to our GitHub repository, restore sources, build then finally run our unit tests.