gitlab-variable
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCI/CD Variable Skill
CI/CD变量管理技能
CI/CD variable management operations for GitLab using the CLI.
glab使用 CLI工具对GitLab进行CI/CD变量管理操作。
glabQuick Reference
快速参考
| Operation | Command | Risk |
|---|---|---|
| List variables | | - |
| Get variable | | - |
| Set variable | | ⚠️ |
| Update variable | | ⚠️ |
| Delete variable | | ⚠️⚠️ |
| Export variables | | - |
Risk Legend: - Safe | ⚠️ Caution | ⚠️⚠️ Warning | ⚠️⚠️⚠️ Danger
| 操作 | 命令 | 风险 |
|---|---|---|
| 列出变量 | | - |
| 获取变量 | | - |
| 设置变量 | | ⚠️ |
| 更新变量 | | ⚠️ |
| 删除变量 | | ⚠️⚠️ |
| 导出变量 | | - |
风险说明: - 安全 | ⚠️ 注意 | ⚠️⚠️ 警告 | ⚠️⚠️⚠️ 危险
When to Use This Skill
何时使用本技能
ALWAYS use when:
- User wants to manage CI/CD variables
- User mentions "variable", "secret", "env var", "CI variable", "environment variable"
- User wants to configure build/deployment settings
NEVER use when:
- User wants to run pipelines (use gitlab-ci)
- User wants to manage .env files locally (use file operations)
务必在以下场景使用:
- 用户需要管理CI/CD变量
- 用户提到“variable”、“secret”、“env var”、“CI variable”、“environment variable”(变量、密钥、环境变量等)
- 用户需要配置构建/部署设置
请勿在以下场景使用:
- 用户需要运行流水线(请使用gitlab-ci技能)
- 用户需要在本地管理.env文件(请使用文件操作技能)
Available Commands
可用命令
List Variables
列出变量
bash
glab variable list [options]Options:
| Flag | Description |
|---|---|
| List group-level variables |
| Results per page |
Examples:
bash
undefinedbash
glab variable list [options]选项:
| 参数 | 描述 |
|---|---|
| 列出组级变量 |
| 每页显示结果数 |
示例:
bash
undefinedList project variables
列出项目变量
glab variable list
glab variable list
List group variables
列出组变量
glab variable list -g mygroup
undefinedglab variable list -g mygroup
undefinedGet Variable
获取变量
bash
glab variable get <key> [options]Options:
| Flag | Description |
|---|---|
| Get from group level |
| Variable scope/environment |
Examples:
bash
undefinedbash
glab variable get <key> [options]选项:
| 参数 | 描述 |
|---|---|
| 从组级获取变量 |
| 变量作用域/环境 |
示例:
bash
undefinedGet variable value
获取变量值
glab variable get API_KEY
glab variable get API_KEY
Get scoped variable
获取带作用域的变量
glab variable get DATABASE_URL --scope=production
undefinedglab variable get DATABASE_URL --scope=production
undefinedSet Variable
设置变量
bash
glab variable set <key> <value> [options]Options:
| Flag | Description |
|---|---|
| Set at group level |
| Mask value in logs |
| Only available in protected branches |
| Value is raw (no expansion) |
| Variable scope/environment |
| Variable type: env_var, file |
Examples:
bash
undefinedbash
glab variable set <key> <value> [options]选项:
| 参数 | 描述 |
|---|---|
| 在组级设置变量 |
| 在日志中隐藏变量值 |
| 仅在受保护分支中可用 |
| 变量值为原始格式(不展开) |
| 变量作用域/环境 |
| 变量类型:env_var、file |
示例:
bash
undefinedSet simple variable
设置简单变量
glab variable set API_URL "https://api.example.com"
glab variable set API_URL "https://api.example.com"
Set masked secret
设置掩码密钥
glab variable set API_KEY "secret123" --masked
glab variable set API_KEY "secret123" --masked
Set protected variable (only on protected branches)
设置受保护变量(仅在受保护分支可用)
glab variable set DEPLOY_KEY "key123" --protected --masked
glab variable set DEPLOY_KEY "key123" --protected --masked
Set scoped variable for production
设置生产环境作用域的变量
glab variable set DATABASE_URL "postgres://prod..." --scope=production
glab variable set DATABASE_URL "postgres://prod..." --scope=production
Set file type variable
设置文件类型变量
glab variable set CONFIG_FILE "$(cat config.json)" --type=file
glab variable set CONFIG_FILE "$(cat config.json)" --type=file
Set group variable
设置组变量
glab variable set SHARED_SECRET "secret" -g mygroup --masked
undefinedglab variable set SHARED_SECRET "secret" -g mygroup --masked
undefinedUpdate Variable
更新变量
bash
glab variable update <key> <value> [options]Same options as . Updates existing variable.
setExamples:
bash
undefinedbash
glab variable update <key> <value> [options]选项与命令相同,用于更新现有变量。
set示例:
bash
undefinedUpdate variable value
更新变量值
glab variable update API_KEY "new-secret" --masked
glab variable update API_KEY "new-secret" --masked
Update and change scope
更新变量并修改作用域
glab variable update DATABASE_URL "new-url" --scope=staging
undefinedglab variable update DATABASE_URL "new-url" --scope=staging
undefinedDelete Variable
删除变量
bash
glab variable delete <key> [options]Options:
| Flag | Description |
|---|---|
| Delete from group level |
| Variable scope |
Warning: This permanently deletes the variable.
Examples:
bash
undefinedbash
glab variable delete <key> [options]选项:
| 参数 | 描述 |
|---|---|
| 从组级删除变量 |
| 变量作用域 |
警告: 此操作会永久删除变量。
示例:
bash
undefinedDelete variable
删除变量
glab variable delete OLD_API_KEY
glab variable delete OLD_API_KEY
Delete scoped variable
删除带作用域的变量
glab variable delete DATABASE_URL --scope=staging
undefinedglab variable delete DATABASE_URL --scope=staging
undefinedExport Variables
导出变量
bash
glab variable export [options]Export variables in dotenv format.
Examples:
bash
undefinedbash
glab variable export [options]以dotenv格式导出变量。
示例:
bash
undefinedExport to stdout
导出到标准输出
glab variable export
glab variable export
Export to file
导出到文件
glab variable export > .env.ci
glab variable export > .env.ci
Export and source
导出并加载
eval $(glab variable export)
undefinedeval $(glab variable export)
undefinedVariable Types
变量类型
| Type | Use Case |
|---|---|
| Environment variable (default) |
| Write value to file, expose path as variable |
| 类型 | 使用场景 |
|---|---|
| 环境变量(默认) |
| 将值写入文件,暴露文件路径作为变量 |
Variable Flags
变量参数
| Flag | Effect |
|---|---|
| Value is hidden in job logs |
| Only available on protected branches/tags |
| No variable expansion (use for JSON, etc.) |
| 参数 | 作用 |
|---|---|
| 在作业日志中隐藏变量值 |
| 仅在受保护分支/标签中可用 |
| 不进行变量展开(适用于JSON等格式) |
Common Workflows
常见工作流
Workflow 1: Set Up Deployment Variables
工作流1:配置部署变量
bash
undefinedbash
undefinedSet production secrets
设置生产环境密钥
glab variable set PROD_API_KEY "xxx" --protected --masked --scope=production
glab variable set PROD_DB_URL "postgres://..." --protected --masked --scope=production
glab variable set PROD_API_KEY "xxx" --protected --masked --scope=production
glab variable set PROD_DB_URL "postgres://..." --protected --masked --scope=production
Set staging secrets
设置预发布环境密钥
glab variable set STAGING_API_KEY "xxx" --masked --scope=staging
glab variable set STAGING_DB_URL "postgres://..." --masked --scope=staging
undefinedglab variable set STAGING_API_KEY "xxx" --masked --scope=staging
glab variable set STAGING_DB_URL "postgres://..." --masked --scope=staging
undefinedWorkflow 2: Rotate Secrets
工作流2:轮换密钥
bash
undefinedbash
undefined1. List current variables
1. 列出当前变量
glab variable list
glab variable list
2. Update the secret
2. 更新密钥
glab variable update API_KEY "new-secret-value" --masked
glab variable update API_KEY "new-secret-value" --masked
3. Trigger a new pipeline to use new secret
3. 触发新流水线以使用新密钥
glab ci run
undefinedglab ci run
undefinedWorkflow 3: Set Up Service Account
工作流3:配置服务账号
bash
undefinedbash
undefinedStore credentials as masked file
将凭据存储为掩码文件
glab variable set SERVICE_ACCOUNT_JSON "$(cat service-account.json)"
--type=file --protected --masked
--type=file --protected --masked
glab variable set SERVICE_ACCOUNT_JSON "$(cat service-account.json)"
--type=file --protected --masked
--type=file --protected --masked
In CI/CD, use $SERVICE_ACCOUNT_JSON as path to the credentials file
在CI/CD中,使用$SERVICE_ACCOUNT_JSON作为凭据文件的路径
undefinedundefinedWorkflow 4: Configure Multi-Environment
工作流4:配置多环境变量
bash
undefinedbash
undefinedProduction (protected + masked)
生产环境(受保护+掩码)
glab variable set DATABASE_URL "postgres://prod..." --scope=production --protected --masked
glab variable set API_KEY "prod-key" --scope=production --protected --masked
glab variable set DATABASE_URL "postgres://prod..." --scope=production --protected --masked
glab variable set API_KEY "prod-key" --scope=production --protected --masked
Staging
预发布环境
glab variable set DATABASE_URL "postgres://staging..." --scope=staging --masked
glab variable set API_KEY "staging-key" --scope=staging --masked
glab variable set DATABASE_URL "postgres://staging..." --scope=staging --masked
glab variable set API_KEY "staging-key" --scope=staging --masked
Development
开发环境
glab variable set DATABASE_URL "postgres://dev..." --scope=development
glab variable set API_KEY "dev-key" --scope=development
undefinedglab variable set DATABASE_URL "postgres://dev..." --scope=development
glab variable set API_KEY "dev-key" --scope=development
undefinedSecurity Best Practices
安全最佳实践
- Always mask secrets: Use for any sensitive values
--masked - Protect production secrets: Use for production credentials
--protected - Use scopes: Separate variables by environment
- Rotate regularly: Update secrets periodically
- Avoid logging: Never echo variable values in CI scripts
- Use file type for complex secrets: JSON, certificates, etc.
- 始终掩码密钥: 对任何敏感值使用参数
--masked - 保护生产环境密钥: 对生产凭据使用参数
--protected - 使用作用域: 按环境分离变量
- 定期轮换: 定期更新密钥
- 避免日志输出: 切勿在CI脚本中打印变量值
- 复杂密钥使用文件类型: 适用于JSON、证书等
Troubleshooting
故障排除
| Issue | Cause | Solution |
|---|---|---|
| Authentication failed | Invalid/expired token | Run |
| Variable not found | Wrong key or scope | Check with |
| Cannot see value | Variable is masked | Masked values cannot be retrieved |
| Permission denied | Not maintainer | Need maintainer+ role for variables |
| Value truncated | Special characters | Use |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 认证失败 | 令牌无效/过期 | 运行 |
| 变量未找到 | 密钥或作用域错误 | 使用 |
| 无法查看变量值 | 变量已掩码 | 掩码变量的值无法被检索 |
| 权限不足 | 非维护者角色 | 需要维护者及以上角色才能管理变量 |
| 值被截断 | 包含特殊字符 | 对复杂值使用 |
Related Documentation
相关文档
- Safeguards
- Quick Reference
- 安全防护措施
- 快速参考