Instant Deployment Made Easy: How to Deploy Web Apps on Azure Static Web Apps.
Published: 2023-04-15 08:40:49

Azure Static Web Apps is a service that deploys full-stack web apps automatically to Azure from a code repository. This service has been designed to meet the daily workflow of developers, where apps are built and deployed based on code changes. When an Azure Static Web Apps resource is created, Azure interacts directly with GitHub or Azure DevOps to monitor a branch of choice. Whenever there is a code change through push commits or acceptance of pull requests, a build automatically runs and the app and API are deployed to Azure.

This service supports popular web development frameworks such as React, Angular, Vue.js, and static site generators such as Gatsby and Hugo. In this article, I will walk you through the steps required to deploy a web app on Azure Static Web App through two different methods:

1. Azure Portal
2. Azure Static Web App CLI

> 

> ## Contents
> - [Prerequisites](#prerequisites)
> - [Deployment through the Azure Portal](#deployment-through-the-azure-portal)
>   * [Step 1: Create a new Azure Static Web App](#step-1-create-a-new-azure-static-web-app)
>   * [Step 2: Configure the Static Web App](#step-2-configure-the-static-web-app)
>   * [Step 3: Review and deploy the Static Web App](#step-3-review-and-deploy-the-static-web-app)
>   * [Step 4: Access your Static Web App](#step-4-access-your-static-web-app)
> - [Deployment through the Azure SWA CLI](#deployment-through-the-azure-swa-cli)
> - [Conclusion](#conclusion)

## Prerequisites
Before we move forward in deploying our apps, we need the following:

- An Azure Account, which can be created [here](https://azure.microsoft.com/en-us/free/).
- A Github repository that contains the code to be deployed. You can find the repository used in this article [here]( https://github.com/MaryAyobami/university-site).

Once you are set, let's proceed!

## Deployment through the Azure Portal
### Step 1: Create a new Azure Static Web App

The first step is to create a new Azure Static Web App in the Azure portal. Follow these steps to create the app:

- Log in to the Azure portal (https://portal.azure.com/).
- Click on "Create a resource" in the top left corner of the dashboard.
- Search for "Static Web Apps" in the search bar.
- Click on "Static Web Apps (Preview)" from the list of results.
- Click on the "Create" button to create a new static web app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdh3cywiqd038ij2v8w2.jpg)

---
### Step 2: Configure the Static Web App
Go ahead to select a subscription, resource group (or create a new one), and give a name to your app as shown in the image below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mxoe3hjh7uyp31dkg6y.jpg)

Under _Deployment Details_ , Select the source where your code is located. For Github, you may have to authorize Azure to connect to your Github account if you have not done so before. 

Then, Select the repository where your web app code is hosted.
Select the branch that you want to deploy.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g74xpa6xbn44iobop1pb.PNG)

Choose the build command that will be used to build your web app. This could be a script in your package.json file or a custom command. Specify the output folder where the built files will be located.
Choose the deployment environment for your static web app. You can choose between a preview or production environment.
Click on the "Review + create" button to review your settings.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qzee1edfhts9w7ozy23.PNG)



---
### Step 3: Review and deploy the Static Web App

After configuring the static web app, you need to review the settings and deploy it. Follow these steps to review and deploy the app:

On the "Create a Static Web App" page, click on the "Review + create" button.
Review your settings and make any necessary changes. Click on the "Create" button to create and deploy your static web app.
Wait for the deployment to complete. You can view the deployment progress in the "Notifications" section.

---

### Step 4: Access your Static Web App

Once the deployment is complete, you will receive a notification. To access your web app, click on 'Go to Resource'.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b183kh881iftt37j8lg2.jpg)
From there, you can directly access your web app by clicking on the "Browse" button. Alternatively, you can copy the URL of your web app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qud17nrayhwkfho18u7c.jpg)

Step 4: Accessing your Static Web App

Once the deployment is complete, you will receive a notification. To access your web app, click on 'Go to Resource'.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b183kh881iftt37j8lg2.jpg)

From there, you can directly access your web app by clicking on the "Browse" button. Alternatively, you can copy the URL of your web app.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qud17nrayhwkfho18u7c.jpg)

And voila! Your web app is up and running.!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fikcj3uwjffuvg3qyy5.PNG)

---

## Deployment through the Azure SWA CLI
Another deployment option is the Azure Static Web Apps CLI, also known as SWA CLI. It is a local development tool for Azure Static Web Apps that can deploy your app to Azure Static Web Apps.

To use the SWA CLI, you need to install it first. You can install it globally using the following command:

```
npm install -g @azure/static-web-apps-cli
```
After a successful installation, navigate to the code you want to deploy and change the directory. To initialize a new static web app project, use the following command:
```
swa init
```
This command will prompt you to choose a configuration name for your app. After that, you will be shown an overview of the configuration settings. If the settings are correct, enter YES then proceed to deploying your app.

To deploy your app to Azure Static Web Apps, type the following command:

To deploy to Azure Static Web Apps, type:

```
swa deploy
```

---

## Conclusion
Azure Static Web Apps makes it easy to deploy web apps to a secure and scalable environment. By following the steps outlined in this article, you can deploy your web app to Azure Static Web Apps in just a few minutes.