You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CLI创建可参数化的GitLab个人访问令牌并解决引号转义问题

报错根因

你当前的写法中$gitlab_username展开后得到的root没有被单引号包裹,Rails执行代码时会将其识别为未定义的变量而非字符串,因此抛出对应报错。你只需要在变量外侧补全单引号,匹配你之前硬编码'root'的写法即可。

修正后命令

output="$(sudo docker exec -i 5303124d7b87 bash -c "gitlab-rails runner \"token = User.find_by_username('$gitlab_username').personal_access_tokens.create(scopes: [:read_user, :read_repository], name: 'Automation-token'); token.set_token('token-string-here123'); token.save! \"")"

补充说明

如果你的用户名包含特殊字符、空格,建议先对变量内容做转义处理,避免执行逻辑异常,常规用户名场景以上写法可直接运行。

内容的提问来源于stack exchange,提问作者a.t.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 21:57:03