DevOps is an important aspect for any application development process. The developers can develop the code and check it in to the source control repository. As soon as the code check processing is complete, the DevOps continuous integration pipeline builds the code and keeps the application’s deployable package in a centralized location. Then the DevOps continuous deployment pipeline picks up the package and deploys it to the target environment. This entire process repeats every time the developer makes changes to the code base and checks in the code. There are lot of DevOps tool offerings in the industry. A few of the popular ones are Jenkins, Azure DevOps, and GitHub Actions, to name a few.
In this chapter we will explore GitHub Actions and how to deploy a .NET application to an Azure web app using GitHub Actions.
Structure
In this chapter, we will explore the following topics related to Azure Cognitive Service:
Introduction to GitHub Actions
Build a .NET application and push it to GitHub
Provision an Azure web app
Deploy an application to Azure the web app using GitHub Actions
Objectives
After studying this chapter, you should be able to
Understand the fundamentals of GitHub Actions
Deploy code to an Azure web app using GitHub Actions
Introduction to GitHub Actions
GitHub Actions helps you to build continuous integration (CI) and continuous deployment (CD) workflows in GitHub that can build the application code checked in to GitHub and deploy it on target environments with ease. You can write YAML-based workflows that have multiple steps to perform different DevOps activities. These workflows get triggered whenever an event occurs in GitHub. For example, whenever the developer checks in the code to GitHub, an event occurs and this triggers the GitHub Actions workflow that will build and deploy the checked in code. The workflows run on servers called runners. Figure 11-1 depicts the architecture of GitHub Actions.
GitHub actions are much more than DevOps tools. You can use GitHub actions to perform automations in the GitHub environment, like adding a label automatically whenever code is checked in to GitHub.
The workflows comprise a set of actions. Each of the actions performs an activity; for example, containerize the application, build the application, or push the container image to a container registry. GitHub provides a wide range of GitHub actions in the GitHub Marketplace. You can use GitHub actions to develop DevOps pipelines meeting your application and business needs.
Build a .NET Application and Push It to GitHub
Let’s build a .NET web application and commit it to a GitHub repository. We will then configure a GitHub action to deploy the code to an Azure web app. Open Visual Studio and click Create a new project as shown in Figure 11-2.
Select ASP.NET Core Web App as shown in Figure 11-3 and then click Next.
Provide the details for the project as in Figure 11-4 and click Next.
Select the framework for .NET that you will use to build the application, as shown in Figure 11-5, and then click Create. The default web application code will get generated. We can push this project to the GitHub repository without making any changes.
Now let’s push the code to the GitHub repository. Click the Git menu and then click Create Git Repository as shown in Figure 11-6.
Click the Account drop-down arrow and select GitHub account as shown in Figure 11-7. You need to sign it and authorize Visual Studio to push the code to the GitHub repository.
Provide your GitHub credentials when prompted and sign in to GitHub as shown in Figure 11-8.
Click Authorize github as shown in Figure 11-9 to authorize Visual Studio to push code to the GitHub repository.
Select your repository and click Create and Push as shown in Figure 11-10.
You can log in to your GitHub account and verify the checked in code, shown in Figure 11-11.
Provision Azure Web App
Now let’s provision an Azure web pp. We will deploy the web application checked in to GitHub to this web app using a GitHub action. Go to the Azure portal and click Create a resource as shown in Figure 11-12.
You will be navigated to the Azure Marketplace. Click Web and then click Web App as shown in Figure 11-13.
Provide the basic details for the web app as shown in Figure 11-14.
Create an App Service plan as shown in Figure 11-15 and then click Review + create.
Click Create as shown in Figure 11-16 to provision the web app.
Deploy Application to Azure Web App Using GitHub Actions
Not let’s configure GitHub actions for the Azure web app and deploy to the web app the application code checked in to the GitHub repository. Go to the Azure web app you created and click Deployment Center. Select GitHub as the source as shown in Figure 11-17.
Click Authorize as shown in Figure 11-18 to facilitate deployment of the application from GitHub to the web app.
Provide your credentials and sign in to GitHub as shown in Figure 11-19.
Click Authorize AzureAppService as shown in Figure 11-20.
Select the repository and branch details for the GitHub repository as shown in Figure 11-21 and then click Save.
Generic GitHub actions pipeline for building and pushing .NET Core application will automatically get generated. Deployment will get initiated as shown in Figure 11-22. You can click the Logs tab to check the build and deployment logs.
You can click one of the stages, either build or deploy, as shown in Figure 11-23 to view the detailed logs for each of the stages.
You can see the logs for the build and deploy stages as shown in Figure 11-24.
Once the deployment is complete, you can browse using the URL for the web app, which you can get from the Overview tab of the web app. You will see the deployed application rendered on the browser as shown in Figure 11-25.
Summary
In this chapter, we explored the basic concepts of GitHub Actions. Then we created a .NET web application and checked in the code to the GitHub repository. We then created an Azure web app and deployed the application to the web app using GitHub actions.