How to Integrate GitLab CI and Spanner Safely
You link gitlab ci and spanner to run schema changes and tests during builds. Your build jobs log in to Google Cloud and send queries to the database. Teams use this setup to push schema updates before new app code goes live. A safe setup stops leaks of cloud keys and limits what each job can change. You need clear rules for identity, roles, and logs across both tools. How gitlab ci and spanner share identity and data A build runner starts a job when a developer pushes code to a branch. That
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You link gitlab ci and spanner to run schema changes and tests during builds. Your build jobs log in to Google Cloud and send queries to the database.
Teams use this setup to push schema updates before new app code goes live. A safe setup stops leaks of cloud keys and limits what each job can change. You need clear rules for identity, roles, and logs across both tools.
How gitlab ci and spanner share identity and data
A build runner starts a job when a developer pushes code to a branch. That runner needs a valid cloud identity to talk to the database instance. The safest path uses workload identity federation instead of static service account keys.
GitLab issues a short token for each build job that runs. Google Cloud checks this token and trades it for a temporary cloud access token. The runner then uses that cloud token to reach the database API.
Data flows over encrypted cloud network paths between the runner and the database. Schema tools read the current tables and apply new migration files during the job. Test jobs write sample rows and read them back to check app logic. When the job ends, the temporary token expires on its own.
Common failure modes and how to scope roles
Many teams store static JSON keys inside project variables to save setup time. Static keys do not expire and can leak in build logs or forks. If a bad actor gets that key, they can read or delete production data.
Shared service accounts cause another big risk for engineering teams. When all pipelines share one cloud account, every branch gets the same high power. A test branch could run a migration against the live production database by mistake.
GitLab CI Security + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Broad cloud roles also create risk during daily builds. Giving a runner project owner rights lets it touch every database in the cloud project. You should scope roles down to the exact database instance and action needed. Give feature branches rights to read and write only on a test database. Give only the main branch rights to alter schemas on the live database.
Unlogged queries make it hard to pass security audits or fix broken builds. Cloud logs show that a service account ran a query, not which job did it. Teams must tie every database statement back to a specific pipeline run and commit.
Network and data controls for build pipelines
Public runners run on shared machines outside your private cloud network. Using public runners to reach production databases opens your network too wide. Run private runners inside your own cloud network to keep database traffic internal.
Even in test databases, real customer data can slip into test snapshots. If a test log prints query results, sensitive fields might show up in plain text. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. This keeps sensitive strings out of build logs while tests still run fine.
Govern statements and responses across databases and APIs
Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.
First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.
Frequently asked questions
How do I connect GitLab runners to Spanner without static keys?
Set up workload identity federation between your GitLab group and your Google Cloud project. The runner trades its job token for a short cloud token during each run.
How should I separate test and production database roles in CI?
Create two distinct cloud service accounts for your test and production databases. Map your main branch to the production account and feature branches to the test account.
Can I restrict which GitLab branches can run schema migrations?
Yes, use protected branches in GitLab and attribute conditions in Google Cloud. Only tokens from protected branches will get permission to assume the migration role.
Related guides
In this post