Deploy your Streamlit app using GitLab and Azure App Service

Here's a step-by-step guide to deploy your Streamlit app using GitLab and Azure App Service:


Step 1: Prepare Your Streamlit App

Ensure your project has the following structure:

bash
my_streamlit_app/ │ ├── app.py # Your main Streamlit script ├── requirements.txt # List of required Python packages └── .gitlab-ci.yml # GitLab CI/CD pipeline config (will create below)

Step 2: Create requirements.txt

List all the required libraries, including Streamlit:

txt
streamlit pandas scikit-learn # Add other dependencies

You can create it with:

bash
pip freeze > requirements.txt

Step 3: Create .gitlab-ci.yml

This config file defines how GitLab will deploy your app:

yaml
image: python:3.9 stages: - deploy deploy_to_azure: stage: deploy script: - pip install -r requirements.txt - echo "Deploying to Azure..." - az login --service-principal -u $AZURE_USERNAME -p $AZURE_PASSWORD --tenant $AZURE_TENANT - az webapp up --name $AZURE_APP_NAME --resource-group $AZURE_RESOURCE_GROUP --location $AZURE_LOCATION --sku F1 only: - main

Replace variables like $AZURE_USERNAME, $AZURE_APP_NAME with actual values or define them as GitLab CI/CD secrets.


Step 4: Push Your Code to GitLab

Commit and push the project to your GitLab repository:

bash
git init git add . git commit -m "Initial commit" git remote add origin <your-gitlab-repo-url> git push -u origin main

Step 5: Set Up Secrets in GitLab

Go to your GitLab repo → Settings → CI/CD → Variables, and add the following:

  • AZURE_USERNAME

  • AZURE_PASSWORD

  • AZURE_TENANT

  • AZURE_APP_NAME

  • AZURE_RESOURCE_GROUP

  • AZURE_LOCATION


Step 6: Configure Azure App Service

  1. Go to Azure Portal

  2. Create a Web App resource.

  3. Select a runtime (Python 3.9).

  4. Choose deployment via GitHub or External CI/CD if asked.

  5. Add a startup command in configuration:

    arduino
    streamlit run app.py --server.port 8000 --server.enableCORS false

Step 7: Trigger the CI/CD Pipeline

Once pushed, GitLab will automatically run the pipeline and deploy your app to Azure.

No comments

Theme images by tjasam. Powered by Blogger.