CI / Secrets and trust
Deploy to Azure with OIDC
Your deploy job needs Azure credentials, and a client secret that you save as a CI secret works for months. With a federated credential, the job trades a 5-minute token from the forge with Azure, and the forge holds no Azure secret.
2 min read
You need:
- Write access to the repository on TanStack Source, and a workflow. Your first workflow shows how to write one.
- Admin access to an Azure app registration or a user-assigned managed identity.
1. Add a federated credential
Add a federated credential to the app registration or the managed identity, for another issuer:
- Issuer:
https://source.tanstack.com/oidc - Subject:
repo:acme/app:ref:refs/heads/main. Azure matches the whole subject exactly. - Audience:
api://AzureADTokenExchange
Note
The issuer is the address of the site, then /oidc. On another deployment of TanStack Source, use its address.
2. Ask for the token in the job
Add permissions: { idToken: true } to the deploy job. Ask for the token, write it to a file, and run your deploy with AZURE_FEDERATED_TOKEN_FILE, AZURE_CLIENT_ID and AZURE_TENANT_ID:
import { writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { defineWorkflow, push, setup } from '@tanstack-source/ci'
const CLIENT_ID = '00000000-0000-0000-0000-000000000000'
const TENANT_ID = '00000000-0000-0000-0000-000000000000'
export default defineWorkflow('deploy', { on: [push({ branches: ['main'] })] }).job('deploy', {
permissions: { idToken: true },
egress: ['registry.npmjs.org', 'login.microsoftonline.com', 'management.azure.com'],
steps: [
setup.pnpm(),
{ name: 'Install', run: 'pnpm install --frozen-lockfile' },
{
name: 'Deploy',
do: async (ctx) => {
const token = await ctx.idToken({ audience: 'api://AzureADTokenExchange' })
const file = join(tmpdir(), 'azure-federated-token')
await writeFile(file, token, { mode: 0o600 })
await ctx.sh`AZURE_FEDERATED_TOKEN_FILE=${file} AZURE_CLIENT_ID=${CLIENT_ID} AZURE_TENANT_ID=${TENANT_ID} pnpm run deploy`
},
},
],
})
- Replace
CLIENT_IDandTENANT_IDwith the ids of your app registration or managed identity. pnpm run deployis your own deploy script. The Azure Identity libraries readAZURE_FEDERATED_TOKEN_FILE,AZURE_CLIENT_IDandAZURE_TENANT_ID.egresslistslogin.microsoftonline.comand the hosts of the Azure services that you deploy to, likemanagement.azure.com.
3. Push to main
git add .source/workflows/deploy.ts
git commit -m "ci: deploy to Azure with OIDC"
git push origin main
Done
Open the run on the CI tab. The Deploy step passes, with the output of your deploy script in its log. The job deployed to Azure, and the forge holds no Azure secret.
If the step fails, read When ctx.idToken() fails.