متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 150Cloud · Azure

How to deploy to Azure from GitHub Actions with OIDC

The old way to deploy to Azure from CI: a service principal client secret stored in GitHub — it expires, leaks and must be rotated. With OIDC (workload identity federation) GitHub gives the job a signed token, Azure checks it ("repo X, environment production") and returns a short-lived access token. No secret exists. This how-to sets up a user-assigned managed identity, the federated credential, the role, and the workflow.

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

150.1

The quick answer

az identity create -g rg-cicd -n id-github-shop
az identity federated-credential create -g rg-cicd --identity-name id-github-shop -n gh-production --issuer https://token.actions.githubusercontent.com --subject repo:ORG/shop:environment:production --audiences api://AzureADTokenExchange
az role assignment create --assignee "$(az identity show -g rg-cicd -n id-github-shop --query principalId -o tsv)" --role Contributor --scope /subscriptions/SUB_ID/resourceGroups/rg-shop-prod
.github/workflows/deploy.yml (part)
permissions:
  id-token: write
  contents: read
jobs:
  deploy:
    environment: production
    runs-on: ubuntu-24.04
    steps:
      - uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      - run: az webapp deploy -g rg-shop-prod -n app-shop-prod --src-path app.zip --type zip

الأوامر في هذا الدرس

150.2

Step by step

  1. The identity — a user-assigned managed identity (or an app registration). Note its client ID and your tenant ID and subscription ID — these are not secrets; store them as GitHub variables.
  2. The federated credential — Azure trusts exactly this subject:
SubjectTrusts
repo:ORG/shop:environment:productionJobs with environment: production (best for deploys).
repo:ORG/shop:ref:refs/heads/mainJobs on the main branch.
repo:ORG/shop:pull_requestPull request jobs (read-only identities only).
  1. Least privilege — give the role only on the resource group it deploys to (quick answer), not the whole subscription; Contributor or a narrower built-in/custom role.
  2. The workflow (quick answer) needs permissions: id-token: write to ask GitHub for the OIDC token, and the same environment as in the subject.
  3. Protect the environment in GitHub (required reviewers, main only) — then only approved runs on main can get the Azure token.
  4. Terraform uses the same login — set ARM_USE_OIDC=true with ARM_CLIENT_ID, ARM_TENANT_ID, ARM_SUBSCRIPTION_ID in the job.
  5. Remove the old secret — delete the AZURE_CREDENTIALS secret and the client secret in Entra ID once OIDC works.

150.3

Check that it worked

The azure/login step logs in without any secret, az account show in the job shows the right subscription, the deploy works — and a run from another branch or without the environment fails to log in (the subject does not match).

الأوامر في هذا الدرس

150.4

Common problems

ProblemCauseFix
AADSTS70021: No matching federated identity record foundSubject differs (branch vs environment, org/repo name case).Copy the exact subject; the error shows the presented subject.
Unable to get ACTIONS_ID_TOKEN_REQUEST_URLid-token: write permission missing.Add it to the workflow/job permissions.
Login works, deploy gets AuthorizationFailedNo role on that scope, or role assignment not yet active.Assign the role on the right resource group; wait a few minutes.
Works for main, not for tags/releasesSubject only for the branch.Add a federated credential for that ref or use an environment.
Fork PRs try to deployWorkflow triggered by pull_request with deploy steps.Deploy only on push to main with the environment.
Old client secret still existsNot cleaned up.Delete it — it is still a way in.

150.5

Go further

1What does Azure check before it gives a GitHub job an access token?

The federated credential's issuer, audience and subject (repo, branch or environment) must match the job's OIDC token.

2Which workflow permission is needed for OIDC?

id-token: write.

كل الأوامر في هذا الفصل