DEV Community

Visual Studio Learn
Visual Studio Learn

Posted on Originally published at visualstudiolearn.blogspot.com

CI/CD for a FastAPI App — GitHub Actions to Azure App Service

Direct answer: Put one workflow file in .github/workflows/. Run your tests on every pull_request, and deploy to Azure App Service only on a push to main, after the tests pass, logging in with OIDC so no Azure password is ever stored in GitHub. That is the whole pipeline — this post builds it step by step.

Versions used (November 2026): Python 3.12.3, FastAPI 0.115.6, pytest 8.3.4, actions/checkout@v4, actions/setup-python@v5, azure/login@v2, azure/webapps-deploy@v3.

What you will build

The pipeline map: open a pull request, test gate, merge to main, OIDC login, deploy job, live on Azure

Two jobs, one gate between them. The test job runs on every pull request and holds no secrets. The deploy job runs only when code lands on main and only after the test job passes.

Environment setup (prerequisites first)

  1. A GitHub account and a repository for the app (public or private — the pipeline is identical).
  2. An Azure account. The free account includes an F1 Free App Service plan; the spending limit stays ON. If any screen asks you to switch to pay-as-you-go, stop — this tutorial never needs it.
  3. Python 3.12 on your own machine for the local run.

The app (kept tiny on purpose)

import os
from fastapi import FastAPI

app = FastAPI(title="CI/CD Demo API")

@app.get("/")
def root():
    return {"app": "CI/CD Demo API", "message": "Hello from FastAPI",
            "environment": os.getenv("APP_ENVIRONMENT", "local"),
            "version": os.getenv("APP_VERSION", "dev")}

@app.get("/health")
def health():
    return {"status": "ok"}

@app.get("/greet/{name}")
def greet(name: str):
    return {"greeting": f"Hello, {name}!"}
Enter fullscreen mode Exit fullscreen mode

Pins in requirements.txt: fastapi==0.115.6, uvicorn==0.34.0, gunicorn==23.0.0, uvicorn-worker==0.2.0. Dev pins (requirements-dev.txt): pytest==8.3.4, httpx==0.28.1. Three tests in tests/test_main.py check the three routes. Run them locally first — the exact commands the pipeline will run:

pip install -r requirements.txt -r requirements-dev.txt
pytest -v
# 3 passed in 0.35s (measured on the demo machine)
Enter fullscreen mode Exit fullscreen mode

pytest run on the demo machine: all three tests pass

The app running locally: root, health and greet endpoints answering with environment local

Step 1 — The workflow triggers: pull_request, never pull_request_target

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
Enter fullscreen mode Exit fullscreen mode

The workflow triggers in ci-cd.yml: pull_request and push to main, highlighted

The December 2 change: pull_request_target runs in the base repository's context, with secrets and a write token, even for fork code — the pattern behind "pwn request" attacks. GitHub's changelog of November 2, 2026 states that from December 2, 2026 it is blocked by default in public repositories unless an Actions event policy explicitly allows it (brownouts ran November 9 and 16). Private/internal repositories and GitHub Enterprise Server are not affected. Plain pull_request runs the proposed code with no secrets — exactly right for tests. This pipeline never uses pull_request_target.

Step 2 — The test job (the gate)

  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install -r requirements-dev.txt
      - name: Run tests
        run: pytest -v
Enter fullscreen mode Exit fullscreen mode

The test job in ci-cd.yml with the pytest step highlighted

A failing test puts a red cross on the pull request. The job holds no Azure secrets, so untrusted code has nothing to steal.

Step 3 — Create the Azure Web App and the OIDC login (one-time)

  1. Portal → Create a resource → Web App: Python 3.12, Linux, F1 Free plan, a globally unique name.
  2. On the app: Configuration → General settings → Startup Command — paste this exact command (it also ships as startup.txt in the companion code) and Save: gunicorn --bind=0.0.0.0:8000 --timeout 600 --workers 2 -k uvicorn_worker.UvicornWorker main:app --access-logfile '-' --error-logfile '-'
  3. Create a user-assigned managed identity (or app registration) with a federated credential trusting your GitHub repository, and the Website Contributor role on the app — Microsoft Learn's "Deploy by using GitHub Actions" page walks this; OIDC is its recommended authentication because the token is short-lived.
  4. In GitHub: Settings → Secrets and variables → Actions → New repository secret, three times: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_SUBSCRIPTION_ID. These are identifiers, not passwords. Also add a variable AZURE_WEBAPP_NAME with your app's name. Never paste a publish profile or password into a workflow file.

The demo Web App in the Azure portal: Status Running, Python 3.12 runtime, F1 Free plan

The demo Web App created for this post (Azure portal, November 17, 2026): Status "Running", Python 3.12 on Linux, F1 Free plan. The subscription ID is blanked out — never share yours.

Step 4 — The deploy job (push to main only, after tests)

  deploy:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs: test
    runs-on: ubuntu-latest
    environment: production
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - name: Install dependencies (for the package)
        run: pip install -r requirements.txt -t .
      - name: Log in to Azure with OIDC (no stored password)
        uses: azure/login@v2
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
      - name: Deploy to Azure App Service
        uses: azure/webapps-deploy@v3
        with:
          app-name: ${{ vars.AZURE_WEBAPP_NAME }}
          package: .
Enter fullscreen mode Exit fullscreen mode

The deploy job in ci-cd.yml with the azure/login and webapps-deploy steps highlighted

The pip install -r requirements.txt -t . step installs the app's dependencies into the package folder itself, so the zip Azure receives already contains everything the app needs to start. permissions: id-token: write on the deploy job is what lets GitHub mint the OIDC token that azure/login exchanges — without it the login fails every time; the test job deliberately does not have it.

needs: test is the gate in code: no green tests, no deploy. environment: production lets you add a required reviewer in GitHub's environment settings if you want a human tap before production changes.

App Service deployment log: Deployment Successful

The deployed app answering live: root, health and greet endpoints with environment production

Proof run for this post (November 17, 2026): the exact app package this workflow ships was deployed to the demo Web App, the build finished with "Deployment Successful", and the three endpoints were verified live — note "environment":"production", which comes from the app setting saved in Step 3. The demo resource group was deleted right after these captures were taken.

A short, honest note on Agentic Workflows

GitHub Agentic Workflows became generally available on October 28, 2026: you describe a repository task in a Markdown file, gh aw compile turns it into a normal Actions workflow, and an AI agent runs it — issue triage and CI-failure investigation are the starter uses. Its guardrails are the part worth copying: read-only access by default and actions only through approved "safe outputs". The same posture exists for coding agents run unattended — for example Claude Code's permissions.denyInsteadOfPrompting (added in v2.1.319): a call that would have waited for your approval is denied and the run continues, instead of being silently approved. Keep agents away from your deploy job until they have earned it.

Errors you may hit

  • Deploy "succeeds" but you see the Azure welcome page: the startup command is empty, so FastAPI never started. Set the gunicorn startup command (Step 3) and restart the app.
  • azure/login fails: one of the three identifiers is wrong, or the federated credential names a different repository/branch. The failure happens before anything deploys — the safe direction.
  • Tests pass locally, fail in Actions: an unpinned dependency moved. Pin everything, exactly as in the companion code.
  • A workflow using pull_request_target stops running in a public repo after December 2, 2026: that is the new default block, not a bug in your YAML. Move test work to plain pull_request, or have an admin create an event policy allowing the specific workflow.

Get the code

The companion code for this post — app, tests, workflow, startup command and README — ships in the public Visual Studio Learn code repository: the topic-13-cicd-fastapi-azure folder. Copy the folder's contents to the root of your own repository, so the workflow lands at .github/workflows/ in your repo — that placement is what Step 1 assumes.

Sources

  • GitHub Changelog, Nov 2, 2026 — pull_request_target blocked by default December 2.
  • GitHub Changelog, Oct 28, 2026 — GitHub Agentic Workflows generally available.
  • Microsoft Learn — Deploy by using GitHub Actions (Azure App Service): OIDC recommended, azure/login@v2, azure/webapps-deploy@v3, Website Contributor.
  • Claude Code changelog v2.1.319 — permissions.denyInsteadOfPrompting.

Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.

Originally published on Visual Studio Learn. Watch the build: CI/CD for a FastAPI App — GitHub Actions to Azure

Top comments (0)