The Simplest Way to Make GCP Secret Manager and Gitlab Work Like They Should
You pair gcp secret manager and gitlab so build jobs fetch cloud secrets on demand. This setup removes hard-coded keys from your source code and runner settings. Teams use GitLab to run tests, build apps, and ship changes to Google Cloud. Those build jobs need database passwords, API tokens, and cloud keys to finish work. Storing those secrets inside GitLab project settings creates two separate places to update keys. Fetching secrets straight from Google Cloud keeps every secret in one central
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 pair gcp secret manager and gitlab so build jobs fetch cloud secrets on demand. This setup removes hard-coded keys from your source code and runner settings.
Teams use GitLab to run tests, build apps, and ship changes to Google Cloud. Those build jobs need database passwords, API tokens, and cloud keys to finish work. Storing those secrets inside GitLab project settings creates two separate places to update keys. Fetching secrets straight from Google Cloud keeps every secret in one central store.
How gcp secret manager and gitlab pass tokens and data
GitLab and Google Cloud trust each other through OpenID Connect, also called OIDC. When a GitLab job starts, the runner creates a short-lived identity token. This token states which group, project, and branch started the current build job.
The runner sends this identity token to the Google Cloud workload identity pool. Google Cloud checks the token signature against the public keys from GitLab. Next, Google Cloud checks the token claims against your pool attribute rules.
If the claims match your rules, Google Cloud returns a short-lived cloud token. The GitLab job uses that cloud token to ask for specific secret values. Secret Manager checks the role bound to that identity and sends the secret back. When the job ends, the token expires and leaves no static key behind.
Common failure modes and how to scope roles
Many teams still paste static service account JSON keys into GitLab variables. Those keys do not expire on their own and often hold broad cloud roles. If a developer prints job variables in a build log, anyone can steal the key.
Another common mistake is trusting an entire GitLab instance in your cloud identity pool. If you skip project and branch checks, any repo can claim your cloud role. A test branch could read production secrets and change live databases during a test run.
GCP Secret Manager + GitLab CI Security: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
To fix this, scope each workload identity attribute mapping to one project path. Require a protected branch claim before a job can read production secrets. Give each service account permission to read only the single secret it needs. Never grant project-wide secret reader roles to a shared build service account.
Keeping build logs and runner hosts clean
Fetching a secret safely is only the first half of the job. Once the secret reaches the GitLab runner, you must keep it out of logs. Turn off debug trace output in jobs that handle raw passwords or keys.
Shared runners also pose a risk when multiple projects share the same virtual machine. A job might write a secret to a disk folder and forget to delete it. The next job on that machine could read that left-over file from the disk.
Use isolated ephemeral runners for jobs that touch production data or cloud systems. Wipe the workspace folder after every run and keep secrets in memory variables. Also turn on data access audit logs in Google Cloud for Secret Manager. Those logs show which GitLab job read each secret and when the request happened. Check these logs often to spot odd secret reads from unexpected build pipelines.
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
Why use Workload Identity Federation instead of service account keys in GitLab?
Service account keys stay valid until someone deletes them by hand. Workload Identity Federation uses short-lived tokens that expire soon after the job starts. This stops leaked build variables from giving attackers long-term cloud access.
How do I stop feature branches from reading production secrets?
Set an attribute condition in your Google Cloud workload identity pool. Check that the GitLab token claim matches your main branch or protected tag. Google Cloud will block token requests from all other branches right away.
Can a GitLab job write new secret versions back to Google Cloud?
Yes, if you grant the secret version adder role to that job identity. Most teams only allow secret reads during standard build and deploy runs. Keep write roles restricted to dedicated key rotation jobs with strict approval rules.
Related guides
In this post