azuredevopssecurityservice-principal

Stop Using Your Own PAT: Commit to Azure DevOps with a Service Principal

PATs are personal and long-lived secrets that stop working the moment you leave the organisation. Here's how to replace them with a Service Principal.

November 7, 20252 min read

If you’ve ever tried committing to an Azure DevOps repo from a pipeline or a Fabric notebook, you probably ended up pasting your own PAT somewhere. It works, but it’s not safe: PATs are personal and long-lived secrets, and stop working the moment you leave the organization.

So here’s a better way:** use a Service Principal (SPN) to get a short-lived Azure AD token** and reuse that as your Git credential.

Step 1: Add the spn to your Azure Devops project

Before you can push commits with an SPN, you need to give it access to your Azure DevOps organization.

  1. Go to Organization Settings → Users
  2. Click Add users
  3. Enter your SPN’s name
  4. Assign it the Basic access level (enough for Git operations)
  5. Add it to the project you want to be able to commit to

Step 2: Get the token

Azure DevOps has a specific resource ID you can target when requesting tokens:

499b84ac-1321-427f-aa17-267ca6975798

That’s the Azure DevOps app ID in Microsoft Entra. When you request a token for 499b84ac-1321-427f-aa17-267ca6975798/.default, you’ll get a bearer token that works for Git HTTPS.

Here’s some example Python code:

import os
from azure.identity import ClientSecretCredential

cred = ClientSecretCredential(
    tenant_id=os.environ["AZURE_TENANT_ID"],
    client_id=os.environ["AZURE_CLIENT_ID"],
    client_secret=os.environ["AZURE_CLIENT_SECRET"]
)

token = cred.get_token("499b84ac-1321-427f-aa17-267ca6975798/.default").token
os.environ["AZDO_TOKEN"] = token

Step 3: Commit with Git

Now that you have a valid token, you can just use it as your Git password.

git config --global user.email "spn@example.com"
git config --global user.name "spn-service-account"
git config --global push.autoSetupRemote true

rm -rf repo-docs
git clone "https://anything:${AZDO_TOKEN}@dev.azure.com/org/project/_git/repo-docs"
cd repo-docs

# checkout target branch
git checkout -B a-sample-branch origin/a-sample-branch

# do some changes

# commit and push
git add index.html
git commit -m "Update docs from pipeline"
git push

Et voila, a safer and better way!