执行gcloud清理策略命令触发AttributeError错误求助
解决
gcloud artifacts repositories set-cleanup-policies命令的AttributeError错误 问题根源
你的清理策略JSON文件结构不符合GCP Artifact Registry的规范:mostRecentVersions字段必须嵌套在condition对象下,而非作为顶层字段存在。当CLI解析错误的结构时,会将本应是字典类型的字段误判为字符串,从而触发'str' object has no attribute 'get'的崩溃错误。
修正后的策略文件
将policy.json内容替换为以下符合规范的结构:
{ "name": "keep-latest-version", "action": {"type": "Keep"}, "condition": { "mostRecentVersions": { "packageNamePrefixes": ["users"], "keepCount": 1 } } }
执行步骤
- 保存修正后的
policy.json文件 - 重新运行原命令:
gcloud artifacts repositories set-cleanup-policies my-repo --policy=policy.json
可选测试方式
若怀疑文件路径或读取问题,可直接通过命令行传入JSON字符串执行:
gcloud artifacts repositories set-cleanup-policies my-repo --policy='{"name": "keep-latest-version", "action": {"type": "Keep"}, "condition": {"mostRecentVersions": {"packageNamePrefixes": ["users"], "keepCount": 1}}}'
内容的提问来源于stack exchange,提问作者Tom3652
相关产品推荐
相关产品推荐

