GitHub Actions中三元运算符的等效写法及run-name条件配置
GitHub Actions run-name 条件逻辑的正确写法
你遇到的问题是YAML语法解析导致的——未加引号的字符串里包含冒号时,YAML会将其误判为键值对的分隔符,从而使配置失效。以下是两种可行的正确写法:
方法一:用引号包裹整个run-name值
使用单引号或双引号将整个run-name的内容包裹,让YAML将其识别为完整字符串,内部的表达式符号就不会触发语法错误:
单引号写法(注意内部单引号需转义为两个单引号)
run-name: 'Something Running ${{ (inputs.cache-retain == ''true'') ? ''(with forced cache-retain)'' : '''' }}'
双引号写法
run-name: "Something Running ${{ (inputs.cache-retain == 'true') ? '(with forced cache-retain)' : '' }}"
方法二:使用GitHub Actions的if函数拼接
也可以用if函数替代三元运算符,写法更贴合Actions的函数风格:
run-name: ${{ concat('Something Running ', if(inputs.cache-retain == 'true', '(with forced cache-retain)', '')) }}
这两种写法都能实现需求:当inputs.cache-retain为'true'时,run-name显示Something running (with forced cache-retain);否则显示Something running。
内容的提问来源于stack exchange,提问作者dankoiDev
相关产品推荐
相关产品推荐

