This example demonstrates how to use SecretZero to manage GitLab CI/CD variables.
For project access token generation with project: auto, see examples/gitlab-project-token.yml.
version: '1.0'
variables:
gitlab_project: mygroup/myproject
gitlab_group: mygroup
environment_scope: production
providers:
gitlab:
kind: gitlab
auth:
kind: token
config:
token: ${GITLAB_TOKEN}
# Optional: custom GitLab instance URL
# url: https://gitlab.mycompany.com
secrets:
# API Key for external service
- name: api_key
kind: random_string
config:
length: 32
charset: alphanumeric
targets:
# Store in local .env for development
- provider: local
kind: file
config:
path: .env
format: dotenv
# Store as GitLab project variable
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
masked: true # Hide in job logs
protected: false # Available to all branches
environment_scope: ${environment_scope}
variable_type: env_var
# Database credentials
- name: database_password
kind: random_password
config:
length: 32
special: true
exclude_characters: '"@/\`'
targets:
# Store as protected GitLab variable (only available on protected branches)
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
masked: true
protected: true
environment_scope: production
# Shared secret across multiple projects
- name: shared_secret
kind: random_string
config:
length: 64
targets:
# Store as group-level variable
- provider: gitlab
kind: gitlab_variable
config:
group: ${gitlab_group}
masked: true
protected: false
environment_scope: "*" # All environments
# File-type variable for configuration
- name: service_account_key
kind: static
config:
default: |
{
"type": "service_account",
"project_id": "my-project"
}
targets:
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
variable_type: file # Creates a file in the CI job
masked: false # JSON cannot be masked-
Generate a GitLab Personal Access Token:
- Go to GitLab → User Settings → Access Tokens
- Create a token with the following scopes:
api(required for reading/writing variables)
-
Export the token as an environment variable:
export GITLAB_TOKEN=glpat-your_token_here -
Test provider connectivity:
secretzero test -f examples/gitlab-cicd.yml -
Sync secrets (dry-run first):
secretzero sync -f examples/gitlab-cicd.yml --dry-run
-
Sync secrets for real:
secretzero sync -f examples/gitlab-cicd.yml
- Project Variables: Variables scoped to a specific project
- Group Variables: Variables shared across all projects in a group
- Environment Scoping: Restrict variables to specific environments
- Protected Variables: Only available on protected branches/tags
- Masked Variables: Hidden in job logs
- File Variables: Variables written to temporary files (useful for credentials)
Once synced, your variables are available in .gitlab-ci.yml:
deploy:
stage: deploy
environment:
name: production
script:
- echo "Using API_KEY: $API_KEY"
- echo "Database password: $DATABASE_PASSWORD"
# File variables are available as paths
- cat $SERVICE_ACCOUNT_KEY
only:
- mainStandard environment variables accessible via $VARIABLE_NAME:
config:
variable_type: env_varVariables written to temporary files, useful for credentials:
config:
variable_type: file # Creates file at path in $VARIABLE_NAMERestrict variables to specific environments:
config:
environment_scope: production # Only in production
# environment_scope: staging # Only in staging
# environment_scope: "*" # All environments (default)Only available on protected branches and tags:
config:
protected: true # Only on protected branchesHidden in job logs (must meet regex pattern):
config:
masked: true # Hidden in logsNote: Masked variables must:
- Be at least 8 characters
- Not have spaces
- Not have special regex characters
Scoped to a single project:
targets:
- provider: gitlab
kind: gitlab_variable
config:
project: mygroup/myprojectAvailable to all projects in a group:
targets:
- provider: gitlab
kind: gitlab_variable
config:
group: mygroupManage secrets for different environments:
secrets:
- name: api_key
kind: random_string
config:
length: 32
targets:
# Development environment
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
environment_scope: development
protected: false
# Staging environment
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
environment_scope: staging
protected: true
# Production environment
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
environment_scope: production
protected: true- Use masked variables for all sensitive data
- Use protected variables for production secrets
- Scope variables to environments to prevent accidental exposure
- Use group variables for shared secrets across projects
- Rotate secrets regularly using SecretZero
- Audit variable access through GitLab's audit events
- Never commit the GITLAB_TOKEN to your repository
- Verify your GITLAB_TOKEN is valid and not expired
- Ensure your token has the
apiscope - For self-hosted GitLab, ensure the
urlis correct
- Project variables require at least Maintainer role
- Group variables require at least Owner role
- Check your access level in project/group settings
- Verify the environment scope matches your job environment
- Protected variables only work on protected branches
- Check project/group settings → CI/CD → Variables
- Ensure the value is at least 8 characters
- Remove spaces or special characters
- Consider using file variables for complex formats