Saturday, October 28, 2017

Deploying web apps to Azure using Git

In previous posts, I showed two ways (GitHub & FTP) of deploying web apps to Azure. See the following articles:

Continuous deployment of ASP.NET MVC 5 app to Azure thru GitHub
http://blog.medhat.ca/2016/10/connecting-to-azure-website-with-ftp.html

In this post, I will show you how to deploy a PHP web app to Azure by directly using the Git repository that resides in Azure itself.
The following are prerequisites:
In this example, I am using a PHP app that simply shows some cartoon characters. The output of the PHP app looks like this:

image

Login into portal.azure.com. On the left-side, click on “App Services”.

image

Click on “+ Add”.

image

Click on “Web app”.

image

On the next blade, click on the “Create” button.

image

Enter a unique name for App name. Choose your subscription, select to create a new Resource Group, select an App Service plan/Location then click on Create.

image

The web application will be created for you in less than two minutes. When it is ready, you will see it in the list of your App services.

image

Click on the newly created app service on the first blade. In the above example it is toon. Then, click on Deployment credentials.

image

Enter a unique FTP/deployment username and Password. Note that these credentials can be used for both FTP and Git. Also, these same credentials can be used for this web app as well as other web apps that you may have already created or those that you plan to create in the future. Therefore, it important that you remember these credentials. If you forget the credentials, then you can always create a new pair of username and password.

image

Our next step is to let Azure know that we want to deploy our web app using Git. Click on “Deployment options”.

image

Click on “Choose Source” then select “Local Git Repository”.

image

Afterwards, click on OK.

image

Next, we need to obtain the URL of the remote Git repository on Azure. Click on Overview.

image

Copy the “Git clone url” and save it in a text editor like Notepad. This will later be used to push your code from your computer to Azure. Note that you can conveniently click on a toolbar on the right of the Git URL to copy it.

image

Now, back on your computer, open a terminal window in the working directory of the web application that you wish to deploy. Run the following Git commands:
git init
git add .
git commit –m “first commit”
git remote add azure {the url of the remote Azure Git repository}
git push azure master
Right after you execute the last push command, you will be prompted to authenticate with your deployment credentials in Azure. These are the credentials that you created earlier.

image

Once your credentials are accepted, the push process will commence. When it is all finished, you can verify that your code was accepted by Azure by clicking on “Deployment options” in Azure.You should see a check mark beside your commit.

image

The true test is to go to the website and see whether or not it indeed works. Click on Overview. The website URL is on the right side of the last blade. Click on it.

image

The website should show up in your browser.

image

What next? Make a change to your code and push it to the remote Git repository on Azure. You will soon after notice that the change you made is reflected on your web application.

Thanks for coming this far in my article.

No comments:

Post a Comment