هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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-prodpermissions:
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 zipStep by step
- 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.
- The federated credential — Azure trusts exactly this subject:
| Subject | Trusts |
|---|---|
repo:ORG/shop:environment:production | Jobs with environment: production (best for deploys). |
repo:ORG/shop:ref:refs/heads/main | Jobs on the main branch. |
repo:ORG/shop:pull_request | Pull request jobs (read-only identities only). |
- Least privilege — give the role only on the resource group it deploys to (quick answer), not the whole subscription;
Contributoror a narrower built-in/custom role. - The workflow (quick answer) needs
permissions: id-token: writeto ask GitHub for the OIDC token, and the sameenvironmentas in the subject. - Protect the environment in GitHub (required reviewers,
mainonly) — then only approved runs onmaincan get the Azure token. - Terraform uses the same login — set
ARM_USE_OIDC=truewithARM_CLIENT_ID,ARM_TENANT_ID,ARM_SUBSCRIPTION_IDin the job. - Remove the old secret — delete the
AZURE_CREDENTIALSsecret and the client secret in Entra ID once OIDC works.
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).
Common problems
| Problem | Cause | Fix |
|---|---|---|
AADSTS70021: No matching federated identity record found | Subject 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_URL | id-token: write permission missing. | Add it to the workflow/job permissions. |
Login works, deploy gets AuthorizationFailed | No 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/releases | Subject only for the branch. | Add a federated credential for that ref or use an environment. |
| Fork PRs try to deploy | Workflow triggered by pull_request with deploy steps. | Deploy only on push to main with the environment. |
| Old client secret still exists | Not cleaned up. | Delete it — it is still a way in. |
Go further
- Command pages: az, gh, terraform.
- Next how-tos: Use secrets safely in GitHub Actions, Log in to Azure and choose a subscription, Plan and apply Terraform safely.
- Learn it: GitHub Actions — OIDC to Azure, Azure setup, Terraform from Actions, Short-lived credentials.
- Full guide: Azure CLI. Cloud logins in DevOps: OIDC.
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.