IEIT by Elias
Connexion

Tutoriel : déployer sur Azure sans secret depuis une pipeline

Intermédiaire Tutoriel 2 min de lecture Mis à jour le 21 novembre 2026

Configurer la fédération d'identités de charge de travail entre GitHub Actions ou GitLab CI et Entra ID, pour ne plus stocker aucun secret.

Le principe

La plateforme CI émet, pour chaque job, un jeton signé (OIDC) qui décrit le dépôt, la branche ou l'environnement. Entra ID fait confiance à cet émetteur pour une identité précise et échange le jeton contre un jeton d'accès Azure valable quelques minutes. Aucun secret n'est stocké nulle part.

1. Créer l'identité

bash
az ad app create --display-name "cicd-infra-prod"
APP_ID=$(az ad app list --display-name "cicd-infra-prod" --query "[0].appId" -o tsv)
az ad sp create --id $APP_ID

Une identité managée affectée par l'utilisateur fonctionne aussi et évite de gérer une inscription d'application.

2. Ajouter l'information d'identification fédérée

Pour GitHub Actions, limitée à l'environnement production du dépôt :

github.json
{
  "name": "github-infra-production",
  "issuer": "https://token.actions.githubusercontent.com",
  "subject": "repo:ewyb/infra:environment:production",
  "audiences": ["api://AzureADTokenExchange"]
}

Pour GitLab, limitée à la branche main du projet :

gitlab.json
{
  "name": "gitlab-infra-main",
  "issuer": "https://gitlab.com",
  "subject": "project_path:ewyb/infra:ref_type:branch:ref:main",
  "audiences": ["api://AzureADTokenExchange"]
}
bash
az ad app federated-credential create --id $APP_ID --parameters github.json

3. Donner les droits

bash
SP_ID=$(az ad sp show --id $APP_ID --query id -o tsv)
az role assignment create --assignee-object-id $SP_ID --assignee-principal-type ServicePrincipal \
  --role Contributor --scope /subscriptions/<id>/resourceGroups/rg-app-prod-frc

Le moins possible, à l'étendue la plus étroite possible.

4. Utiliser dans GitHub Actions

yaml
permissions:
  id-token: write
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - 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 group show -n rg-app-prod-frc

5. Utiliser dans GitLab CI

yaml
deploy:
  image: mcr.microsoft.com/azure-cli
  id_tokens:
    AZURE_JWT:
      aud: api://AzureADTokenExchange
  script:
    - az login --service-principal -u "$AZURE_CLIENT_ID" -t "$AZURE_TENANT_ID" --federated-token "$AZURE_JWT"
    - az account set --subscription "$AZURE_SUBSCRIPTION_ID"
    - az group show -n rg-app-prod-frc
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Pour Terraform, les variables ARM_USE_OIDC=true, ARM_OIDC_TOKEN, ARM_CLIENT_ID, ARM_TENANT_ID et ARM_SUBSCRIPTION_ID suffisent au provider azurerm.

Dépanner

ErreurCause probable
AADSTS70021: No matching federated identity record foundLe subject ne correspond pas exactement (branche, environnement, nom du dépôt)
AADSTS700016Mauvais identifiant d'application ou de tenant
Authentification réussie mais AuthorizationFailedRôle manquant ou mauvaise étendue
Tu as terminé ce guide ? Connecte-toi pour suivre ta progression