Tutorial: Deploy GitLab repositories on Azure Static Web Apps
Azure Static Web Apps has flexible deployment options that allow to work with various providers. In this article, you deploy a web application hosted in GitLab to Azure Static Web Apps.
In this tutorial, you learn to:
- Import a repository to GitLab
- Create a static web app
- Configure the GitLab repo to deploy to Azure Static Web Apps
Prerequisites
- GitLab account
- Azure account
- If you don't have an Azure subscription, create a free trial account.
Create a repository
This article uses a GitHub repository as the source to import code into a GitLab repository.
Sign in to your GitLab account and go to https://gitlab.com/projects/new#import_project
Select Repo by URL.
In the Git repository URL box, enter the repository URL for your choice of framework.
In the Project slug box, enter my-first-static-web-app.
Select Create project and wait a moment while your repository is set up.
Create a static web app
Now that the repository is created, you can create a static web app from the Azure portal.
Go to the Azure portal.
Select Create a Resource.
Search for Static Web Apps.
Select Static Web Apps.
Select Create.
In the Basics section, begin by configuring your new app.
Setting Value Azure subscription Select your Azure subscription. Resource Group Select the Create new link and enter static-web-apps-gitlab. Name Enter my-first-static-web-app. Plan type Select Free. Region for Azure Functions API and staging environments Select the region closest to you. Source Select Other. Select Review + create.
Select Create.
Select Go to resource.
Select Manage deployment token.
Copy the deployment token value and set it aside in an editor for later use.
Select Close on the Manage deployment token window.
Create the pipeline task in GitLab
Next you add a workflow task responsible for building and deploying your site as you make changes.
Add deployment token
- Go to the repository in GitLab.
- Select Settings.
- Select CI/CD.
- Next to the Variables section, select Expand.
- Select Add variable.
- In the Key box, enter DEPLOYMENT_TOKEN.
- In the Value box, paste in the deployment token value you set aside in a previous step.
- Select Add variable.
Add file
Select the Repository menu option.
Select Files.
Ensure the main branch is selected in the branch drop down at the top.
Press the plus sign drop down and select New file.
Create a new file named
.gitlab-ci.yml
at the root of the repository. (Make sure the file extension is.yml
.)Enter the following YAML into the file.
variables: API_TOKEN: $DEPLOYMENT_TOKEN APP_PATH: '$CI_PROJECT_DIR/src' deploy: stage: deploy image: registry.gitlab.com/static-web-apps/azure-static-web-apps-deploy script: - echo "App deployed successfully."
The following configuration properties are used in the .gitlab-ci.yml file to configure your static web app.
The
$CI_PROJECT_DIR
variable maps to the repository's root folder location during the build process.Property Description Example Required APP_PATH
Location of your application code. Enter $CI_PROJECT_DIR/
if your application source code is at the root of the repository, or$CI_PROJECT_DIR/app
if your application code is in a folder namedapp
.Yes API_PATH
Location of your Azure Functions code. Enter $CI_PROJECT_DIR/api
if your app code is in a folder namedapi
.No OUTPUT_PATH
Location of the build output folder relative to the APP_PATH
.If your application source code is located at $CI_PROJECT_DIR/app
, and the build script outputs files to the$CI_PROJECT_DIR/app/build
folder, then set$CI_PROJECT_DIR/app/build
as theOUTPUT_PATH
value.No API_TOKEN
API token for deployment. API_TOKEN: $DEPLOYMENT_TOKEN
Yes Select Commit changes.
Select the CI/CD then Pipelines menu items to view the progress of your deployment.
Once the deployment is complete, you can view your website.
View the website
There are two aspects to deploying a static app. The first step creates the underlying Azure resources that make up your app. The second is a GitLab workflow that builds and publishes your application.
Before you can go to your new static site, the deployment build must first finish running.
The Static Web Apps overview window displays a series of links that help you interact with your web app.
- Return to your static web app in the Azure portal.
- Go to the Overview window.
- Select the link under the URL label. Your website loads in a new tab.
Clean up resources
If you're not going to continue to use this application, you can delete the Azure Static Web Apps instance and all the associated services by removing the resource group.
- Select the static-web-apps-gitlab resource group from the Overview section.
- Select Delete resource group at the top of the resource group Overview.
- Enter the resource group name static-web-apps-gitlab in the Are you sure you want to delete "static-web-apps-gitlab"? confirmation dialog.
- Select Delete.
The process to delete the resource group may take a few minutes to complete.
Next steps
Feedback
Submit and view feedback for