Skip to main content

Deploying Python to Azure App Service

Learn how to deploy a Python project to Azure App Service as part of your continuous deployment (CD) workflows.

์ด ๋ฌธ์„œ์˜ ๋‚ด์šฉ

Prerequisites

Before creating your GitHub Actions workflow, you will first need to complete the following setup steps:

  1. Azure App Service ์š”๊ธˆ์ œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด Azure CLI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ App Service ์š”๊ธˆ์ œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    Bash
    az appservice plan create \
       --resource-group MY_RESOURCE_GROUP \
       --name MY_APP_SERVICE_PLAN \
       --is-linux
    

    ์œ„์˜ ๋ช…๋ น์—์„œ MY_RESOURCE_GROUP์„ ๊ธฐ์กด Azure ๋ฆฌ์†Œ์Šค ๊ทธ๋ฃน์œผ๋กœ ๋ฐ”๊พธ๊ณ  MY_APP_SERVICE_PLAN์„ App Service ์š”๊ธˆ์ œ์˜ ์ƒˆ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.

    Azure CLI ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Azure ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

    • ์ธ์ฆ์€ Azure CLI๋กœ ๋กœ๊ทธ์ธ์„ ์ฐธ์กฐํ•˜์„ธ์š”.
    • ์ƒˆ ๋ฆฌ์†Œ์Šค ๊ทธ๋ฃน์„ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ az group์„ ์ฐธ์กฐํ•˜์„ธ์š”.
  2. Create a web app.

    For example, you can use the Azure CLI to create an Azure App Service web app with a Python runtime:

    Bash
    az webapp create \
        --name MY_WEBAPP_NAME \
        --plan MY_APP_SERVICE_PLAN \
        --resource-group MY_RESOURCE_GROUP \
        --runtime "python|3.8"
    

    In the command above, replace the parameters with your own values, where MY_WEBAPP_NAME is a new name for the web app.

  3. Azure ๊ฒŒ์‹œ ํ”„๋กœํ•„์„ ๊ตฌ์„ฑํ•˜๊ณ  AZURE_WEBAPP_PUBLISH_PROFILE ๋น„๋ฐ€์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

    ๊ฒŒ์‹œ ํ”„๋กœํ•„์„ ์‚ฌ์šฉํ•˜์—ฌ Azure ๋ฐฐํฌ ์ž๊ฒฉ ์ฆ๋ช…์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Azure ์„ค๋ช…์„œ์˜ ๋ฐฐํฌ ์ž๊ฒฉ ์ฆ๋ช… ์ƒ์„ฑ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

    GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๊ฒŒ์‹œ ํ”„๋กœํ•„์˜ ์ฝ˜ํ…์ธ ๊ฐ€ ํฌํ•จ๋œ AZURE_WEBAPP_PUBLISH_PROFILE์ด๋ผ๋Š” ๋น„๋ฐ€์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋น„๋ฐ€์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์˜ ์ž์„ธํ•œ ๋‚ด์šฉ์€ Using secrets in GitHub Actions์„(๋ฅผ) ์ฐธ์กฐํ•˜์„ธ์š”.

  4. Add an app setting called SCM_DO_BUILD_DURING_DEPLOYMENT and set the value to 1.

  5. Optionally, configure a deployment environment. ํ™˜๊ฒฝ์€ ์ผ๋ฐ˜์ ์ธ ๋ฐฐํฌ ๋Œ€์ƒ(์˜ˆ: production, staging ๋˜๋Š” development)์„ ์„ค๋ช…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. GitHub Actions ์›Œํฌํ”Œ๋กœ๊ฐ€ ํ™˜๊ฒฝ์— ๋ฐฐํฌ๋˜๋ฉด ํ™˜๊ฒฝ์ด ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๊ธฐ๋ณธ ํŽ˜์ด์ง€์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์Šน์ธ์„ ์š”๊ตฌํ•˜๊ฑฐ๋‚˜ ์›Œํฌํ”Œ๋กœ, ์‚ฌ์šฉ์ž ์ง€์ • ๋ฐฐํฌ ๋ณดํ˜ธ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒŒ์ดํŠธ ๋ฐฐํฌ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ„๊ธฐ๋ฅผ ์ œํ•œํ•˜๊ฑฐ๋‚˜ ๋น„๋ฐ€์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ฐฐํฌ ํ™˜๊ฒฝ ๊ด€๋ฆฌ์„(๋ฅผ) ์ฐธ์กฐํ•˜์„ธ์š”.

Creating the workflow

Once you've completed the prerequisites, you can proceed with creating the workflow.

The following example workflow demonstrates how to build and deploy a Python project to Azure App Service when there is a push to the main branch.

Ensure that you set AZURE_WEBAPP_NAME in the workflow env key to the name of the web app you created. If you use a version of Python other than 3.8, change PYTHON_VERSION to the version that you use.

๋ฐฐํฌ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•œ ๊ฒฝ์šฐ ํ™˜๊ฒฝ์˜ ์ด๋ฆ„์œผ๋กœ environment ๊ฐ’์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ environment ํ‚ค๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

YAML
# ์ด ์›Œํฌํ”Œ๋กœ๋Š” GitHub์—์„œ ์ธ์ฆ๋˜์ง€ ์•Š์€ ์ž‘์—…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
# ์ž‘์—…์€ ํƒ€์‚ฌ์—์„œ ์ œ๊ณตํ•˜๋ฉฐ
# ๋ณ„๋„์˜ ์„œ๋น„์Šค ์•ฝ๊ด€, ๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ, ์ง€์› ์„ค๋ช…์„œ์—์„œ ๊ทœ์ •๋ฉ๋‹ˆ๋‹ค.
# ์ฐธ์กฐํ•˜์„ธ์š”.

# ์ปค๋ฐ‹ SHA์— ์ž‘์—…์„ ๊ณ ์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
# ์ตœ์‹  ๋ฒ„์ „์„ ์–ป์œผ๋ ค๋ฉด SHA๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
# ํƒœ๊ทธ ๋˜๋Š” ๋ถ„๊ธฐ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ฒฝ๊ณ  ์—†์ด ์ž‘์—…์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

name: Build and deploy Python app to Azure Web App

env:
  AZURE_WEBAPP_NAME: MY_WEBAPP_NAME   # set this to your application's name
  PYTHON_VERSION: '3.8'               # set this to the Python version to use

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python version
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate

      - name: Set up dependency caching for faster installs
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install dependencies
        run: pip install -r requirements.txt

      # Optional: Add a step to run tests here (PyTest, Django test suites, etc.)

      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v3
        with:
          name: python-app
          path: |
            .
            !venv/
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: python-app
          path: .

      - name: 'Deploy to Azure Web App'
        id: deploy-to-webapp
        uses: azure/webapps-deploy@85270a1854658d167ab239bce43949edb336fa7c
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}

Further reading