The course is part of this learning path
Microsoft provides many ways to deploy code to App Service, but one of the best ways is to deploy from a source code repository, such as GitHub, Bitbucket, or Azure Repos. This is useful whether you need to upload your code once or need to implement continuous deployment in a DevOps environment. In this brief course, we’ll show you how to deploy code from GitHub to Azure App Service both manually and using continuous deployment.
- Deploy code from GitHub to Azure App Service manually using the command line
- Configure continuous deployment from GitHub to Azure App Service
- Anyone who wants to deploy code from a source code repository to Azure App Service
- Experience using Azure App Service (or take our Introduction to Azure App Service and Using Deployment Slots in Azure App Service courses)
- Experience with GitHub (not mandatory)
All the commands from the demo in this course can be found in the following GitHub repo: https://github.com/cloudacademy/azure-app-service-github
Welcome to “Deploying Code from GitHub to Azure App Service”. To get the most from this course, you should already have some experience using App Service. If you don’t, then you can take our “Introduction to Azure App Service” and “Using Deployment Slots in Azure App Service” courses. It would also be helpful to have some experience with GitHub, although it’s not a requirement.
Microsoft provides many ways to deploy code to App Service, but one of the best ways is to deploy from a source code repository, such as GitHub, Bitbucket, or Azure Repos. This is useful whether you need to upload your code once or need to implement continuous deployment in a DevOps environment.
In this short course, I’ll show you how to deploy code from GitHub to App Service both manually and using continuous deployment. Although it’s possible to do both using the Azure Portal’s graphical user interface, I’m going to use the command-line interface to do the manual deployment for a couple of reasons. First, you can use Azure commands in automation scripts. Second, Microsoft often asks command-line questions in its Azure Developer certification exam, so if you’re preparing to take that exam, it’s good to know these commands.
In this demo, I’m going to show you all of the commands you’ll need to use if you don’t have any App Service resources yet. First, I’m going to create a resource group to hold the resources. Then, I’ll create an App Service Plan to host the app. Next, I’ll create a webapp in the plan. And finally, I’ll deploy a sample app from one of Microsoft’s GitHub repositories to the webapp in App Service. The sample app is called “html-docs-hello-world”. It doesn’t do much, but it’ll be fine for demonstration purposes.
To use the command-line interface from your desktop, you need to install the Azure CLI. But an alternative is to use Azure Cloud Shell, which has the Azure CLI preinstalled. Click here to start it. If it’s your first time running Cloud Shell, then it’ll ask you to create a storage account that it can use. I’ve used Cloud Shell before, so mine already has a storage account.
Okay, first let’s create a resource group. You need to use the “az group create” command. Add the name parameter and call it “webapprg” for “webapp resource group”. Then add the location parameter and specify an Azure region. I’ll use “westus”.
As you’d expect in a course about GitHub, I put all of the commands from this demo in a readme file in a GitHub repository so you can copy and paste them. You can find a link to the repository in the Overview tab below this video.
All right, now we need to create an App Service plan using the “az appservice plan create” command. For the name parameter, I’ll call it “asplan” for “App Service Plan”. For the resource group, we need to say “webapprg”, which is the resource group we just created. For the location, we’ll use the same region as we did for the resource group. And finally, we have to specify the SKU, which means the pricing tier for the App Service Plan. F1 is the free tier, so we won’t have to pay anything.
Okay, now we need to create a webapp in the plan. This will essentially be a place to put our app when the time comes, so when we first create the webapp, it’ll be empty. We use the “az webapp create” command. The name has to be globally unique, so I’ll call it “ca” for “Cloud Academy”, “webapp”. You’ll have to call it something else. Then tell it to use the webapprg resource group and the App Service plan we just created, which we called asplan.
All right, now we have everything we need to deploy the app. The command we need is “az webapp deployment source config”. We need to give it a lot of parameters. You can put them in any order, but I’ll put them in the order that I think is most logical.
First, we’ll use the “repo-url” parameter to tell it where the GitHub repository is. This is the URL for the “html-docs-hello-world” sample app. Then we need to tell it which branch of the repository contains the code we want to deploy. In most cases, you’ll probably just want to deploy the master branch, but if you need to deploy a different branch, this is where you do it.
Next, we need to specify the “manual-integration” parameter. This means that we’re deploying the code manually rather than telling it to automatically deploy the app every time the code changes.
Then we tell it the name of the webapp where we want to deploy the code. And finally, we tell it which resource group the webapp is in. This’ll take a while, so I’ll fast-forward.
Okay, it’s done. Now we can see if it worked by going to the URL of the webapp, which is the name of the webapp and then “.azurewebsites.net”.
Yes, this is the sample hello world app, so it worked.
If you want to configure continuous deployment, which is where the app automatically gets deployed every time the code in the repository changes, then Microsoft recommends using the Azure Portal.
First, I’ll find my app in the portal. Here it is. Now, we need to go to the Deployment Center. Since we already did a deployment from GitHub, it shows the configuration we used. Here’s the URL of the GitHub repository. If we wanted to do another deployment manually, then we’d just click on “Sync” up here. But we’re going to set up continuous deployment, so first we need to get rid of this manual configuration, which we can do by clicking “Disconnect”.
It says, “You’re now in the production slot, which is not recommended for setting up continuous integration / continuous deployment. That’s because if a new version of the app got deployed to the production slot, it would cause the app to be unavailable until the deployment is finished, which is not something you want to do to your users. The solution is to configure continuous deployment in a non-production deployment slot.
Since our App Service Plan is using the Free tier, which doesn’t support deployment slots, we’ll need to upgrade our plan to the Standard tier. To do that, we go to “Scale up”, choose the S1 tier, which means “Standard”, and click “Apply”.
There. Now we need to create a deployment slot, so we select “Deployment slots” in the menu, and then click “Add Slot”. We’ll call it “staging” and tell it to clone settings from the production slot.
Then we click on the slot, and now we can go back to the Deployment Center. Good, the warning is gone because now we’re in the staging slot, which is a non-production deployment slot.
In the “Source” dropdown, it gives us the option to set up either continuous deployment or manual deployment. Let’s select “GitHub” in the continuous deployment section.
Before it can deploy the app, it needs to build it. By default, it uses GitHub Actions to do the build, but it’s usually easier to change this to “App Service Build Service”. This means that App Service will take care of building it.
Now I need to authorize App Service to access my GitHub account.
Then I need to choose the organization, the repository, and the branch. I don’t have privileged access to Microsoft’s sample repository, so it’s not something I can select here. Instead, I copied Microsoft’s repository to my own organization, where I do have privileged access. Then I’ll click “Save”.
That worked, so now whenever the code in the master branch of my copy of the repository changes, it will automatically build and deploy the app to the staging slot. Then I could swap the staging slot with the production slot to upgrade the production version of the app. Alternatively, I could configure auto swap on the staging slot, so it would automatically swap the slot with production once the app is running.
If you’ve been following along on your own, make sure to either downgrade the App Service Plan to the free tier or delete the whole resource group. That way, you won’t incur any more charges.
And that’s it for deploying code from GitHub to Azure App Service. Please give this course a rating, and if you have any questions or comments, please let us know. Thanks!
Guy launched his first training website in 1995 and he's been helping people learn IT technologies ever since. He has been a sysadmin, instructor, sales engineer, IT manager, and entrepreneur. In his most recent venture, he founded and led a cloud-based training infrastructure company that provided virtual labs for some of the largest software vendors in the world. Guy’s passion is making complex technology easy to understand. His activities outside of work have included riding an elephant and skydiving (although not at the same time).