表达式
本文档是 AtomGit Action 表达式的完整参考,包含字面量、运算符、状态函数、字符串函数和表达式使用示例。
AtomGit Action 使用 ${{ expression }} 语法在工作流中编写表达式。表达式可在 if 条件、变量赋值、步骤参数等位置使用。
3.1 字面量
| 类型 | 语法 | 示例 |
|---|---|---|
| 布尔值 | true / false | ${{ true }} |
| null | null | ${{ null }} |
| 数字 | 整数或浮点数 | ${{ 42 }}, ${{ 3.14 }} |
| 字符串 | 单引号包裹 | ${{ 'hello' }} |
3.2 运算符
| 运算符 | 说明 | 示例 |
|---|---|---|
== | 等于 | ${{ atomgit.ref == 'refs/heads/main' }} |
!= | 不等于 | ${{ atomgit.event_name != 'schedule' }} |
> | 大于 | ${{ matrix.version > 12 }} |
< | 小于 | ${{ matrix.version < 14 }} |
>= | 大于等于 | ${{ strategy.job-total >= 3 }} |
<= | 小于等于 | ${{ inputs.count <= 10 }} |
运算符优先级(从高到低): `` →
!→<,>,<=,>=→==,!=→&&→||
3.3 函数
| 函数 | 说明 | 示例 |
|---|---|---|
always | 无论前置步骤结果如何始终返回 true | if: ${{ always() }} |
contains(search, item) | 判断 search 是否包含 item | ${{ contains(atomgit.ref, 'release') }} |
startsWith(search, prefix) | 判断 search 是 否以 prefix 开头 | ${{ startsWith(atomgit.ref, 'refs/tags/') }} |
endsWith(search, suffix) | 判断 search 是否以 suffix 结尾 | ${{ endsWith(atomgit.ref_name, '.rc') }} |
format(template, args...) | 格式化字符串,0/1... 为占位符 | ${{ format('Hello {0}, {1}!', name, role) }} |
substring(str, start, len) | 截取子字符串 | ${{ substring(atomgit.sha, 0, 7) }} |
replace(str, old, new) | 替换字符串 | ${{ replace(atomgit.ref, 'refs/heads/', '') }} |
函数详细说明:
- always:在任何情况下返回
true,用于确保步骤始终执行(如清理步骤)。配合post使用。 - contains(search, item):字符串搜索时为子串匹配;数组/对象搜索时判断是否存在该元素。
- startsWith/endsWith:纯字符串操作,区分大小写。
- format:使用
{0},{1}, ... 作为占位符,参数依次替换。
3.4 表达式示例
steps:
- name: 失败或取消时仍执行清理
if: ${{ always() }}
run: echo "Cleanup resources"
- name: 标签推送时构建
if: ${{ startsWith(atomgit.ref, 'refs/tags/') }}
run: echo "Build release"
- name: 使用 format 拼接字符串
env:
IMAGE_TAG: ${{ format('{0}:{1}', 'myimage', atomgit.sha) }}
run: echo $IMAGE_TAG