CI / Secrets and trust
Deploy to Google Cloud with OIDC
Your deploy job needs Google Cloud credentials, and a key that you save as a CI secret works for months. With workload identity federation, the job trades a 5-minute token from the forge with Google Cloud, and the forge holds no Google Cloud key.
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 your Google Cloud project.
1. Add the forge as an OIDC provider
Create a workload identity pool, with an OIDC provider in it. Set the issuer to https://source.tanstack.com/oidc.
Note
The issuer is the address of the site, then /oidc. On another deployment of TanStack Source, use its address.
2. Map the subject, and limit who gets in
Map google.subject to assertion.sub. Add an attribute condition, like:
assertion.sub == 'repo:acme/app:ref:refs/heads/main' && assertion.trust == 'protected'
Google Cloud can match any claim of the token. The claims are in The claims of a token.
3. Set the audience
Set an allowed audience on the provider. The job asks for the same audience in step 4.
4. Write the token to a file in the job
Add permissions: { idToken: true } to the deploy job, and write the token to a file before the deploy:
import { writeFile } from 'node:fs/promises'
import { defineWorkflow, push, setup } from '@tanstack-source/ci'
const AUDIENCE = '<the allowed audience of the provider>'
export default defineWorkflow('deploy', { on: [push({ branches: ['main'] })] }).job('deploy', {
permissions: { idToken: true },
egress: ['registry.npmjs.org', 'sts.googleapis.com', 'storage.googleapis.com'],
steps: [
setup.pnpm(),
{ name: 'Install', run: 'pnpm install --frozen-lockfile' },
{
name: 'Deploy',
do: async (ctx) => {
const token = await ctx.idToken({ audience: AUDIENCE })
await writeFile('/tmp/gcp-oidc-token', token, { mode: 0o600 })
await ctx.sh`pnpm run deploy`
},
},
],
})
egresslistssts.googleapis.comand the hosts of the Google APIs that you deploy to, likestorage.googleapis.com.pnpm run deployis your own deploy script.
5. Make a credential configuration for the token file
Make a credential configuration that reads the token from that file: gcloud iam workload-identity-pools create-cred-config with --credential-source-file /tmp/gcp-oidc-token. Your deploy script uses this configuration.
Done
Push the workflow to main, and open the run on the CI tab. The job gets Google Cloud credentials for the token, and the forge holds no Google Cloud key.
If the step fails, read When ctx.idToken() fails.