Deploying Streamlit App on Azure App Service (without Docker) using Azure DevOps

First create a web app on the Azure App Services. 

I recommend to use Python >= 3.10 to prevent any issues. (e.g. with Python 3.9 the app didn't load properly)

Because we don't use Docker, we select 'Code' for Publish and 'Linux' for Operating System.





Once we have the app, we're ready to deploy the app using Azure DevOps pipeline.

1. Archive the code into a zip

stages:
- stage: Build
  displayName: Build
  dependsOn: []
  jobs:
  - job: Build
    displayName: Build the function app
    steps:
    - task: UsePythonVersion@0
      displayName: "Setting python version to 3.10 as required by functions"
      inputs:
        versionSpec: '3.10'
        architecture: 'x64'

    - task: ArchiveFiles@2
      displayName: "Archive files"
      inputs:
        rootFolderOrFile: "$(System.DefaultWorkingDirectory)"
        includeRootFolder: false
        archiveFile: "$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip"
    
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(System.DefaultWorkingDirectory)/build$(Build.BuildId).zip'
        artifactName: 'MyStreamlitApp'


2. Deploy by enabling remote build.

We can enable remote build by first setting some environment variables (app settings).

By setting  SCM_DO_BUILD_DURING_DEPLOYMENT and ENABLE_ORYX_BUILD to true (or 1) in app settings, we build the app remotely. This runs pip install -r requirements.txt by default. 

If you have any commands or script to learn before or after the build, you can set PRE_BUILD_COMMAND or POST_BUILD_COMMAND in the app settings.


- stage: Deploy_MyStreamlitApp
  dependsOn: ${{ parameters.depends_on }}
  jobs:
  - job: Release_MyStreamlitApp
    steps:
    - task: DownloadBuildArtifacts@0
      inputs:
        buildType: 'current'
        downloadType: 'single'
        artifactName: 'MyStreamlitApp'
        downloadPath: '$(System.ArtifactsDirectory)'

    - task: AzureAppServiceSettings@1
      displayName: Set App Settings
      inputs:
        azureSubscription: ${{ parameters.service_connection }}
        appName: MyStreamlitApp
        appSettings: |
          [
            {
              "name": "ENABLE_ORYX_BUILD",
              "value": 1
            },
            {
              "name": "SCM_DO_BUILD_DURING_DEPLOYMENT",
              "value": 1
            },
            {
              "name": "POST_BUILD_COMMAND",
              "value": "pip install ."
            }
          ]
        connectionStrings: |
          [
            {
              "name": "API_KEY",
              "value": "$(api-key)",
              "type": "Custom",
              "slotSetting": false
            }
          ]


    - task: AzureWebApp@1
      displayName: Deploy Web App
      inputs:
        azureSubscription: ${{ parameters.service_connection }}
        appType: webAppLinux
        appName: ${{ parameters.app_name}}
        package: $(System.ArtifactsDirectory)/**/*.zip
        startUpCommand: 'python -m streamlit run Home.py --server.port 8000 --server.address 0.0.0.0'
        deploymentMethod: 'zipDeploy'

After that, we can actually deploy the app using AzureWebApp@1 task. 

We should also set the startUpCommand to run the Streamlit app.

If you didn't change the port and host in the streamlit configuriation toml file, don't forget to set in the startup command with port 8000 and host 0.0.0.0.

In order to test if the streamlit can be run properly, you can first test with the command "python -m streamlit hello --server.port 8000 --server.address 0.0.0.0".

Comments