仅按名称列出 Gitlab 运行器

List Gitlab runners by name only

我正在尝试获取仅包含 gitlab 运行者姓名的列表。

所以gitlab-runner list 2>&1的输出是:

Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml
default_runner                                      Executor=shell Token=251cda361f983e612b27381e2f73ad URL=http://10.6.20.230
test runner                                         Executor=shell Token=86ab70918fc87c8a8d3a57c21457fb URL=http://10.6.20.230

请注意,参赛者的名字中可以包含空格。

所以我尝试了以下方法:

gitlab-runner list 2>&1 | awk -F'Executor' '{if(NR>1)print }' 这给了我几乎我想要的东西(除了我需要删除的尾随空格)。

default_runner
test runner

但是,如果我将字段分隔符更改为 Executor= 以使其更明确,它就不再有效了。它 returns 整行。

$ gitlab-runner list 2>&1 | awk -F'Executor=' '{if(NR>1)print }'
default_runner                                      Executor=shell Token=251cda361f983e612b27381e2f73ad URL=http://10.6.20.230
test runner                                         Executor=shell Token=86ab70918fc87c8a8d3a57c21457fb URL=http://10.6.20.230

我试过用 Executor\= 转义它,但无济于事。如何在拆分中包含等号?

编辑:

如果我使用其中一行并将其回显到 awk 中,它就会工作

$ echo "test runner                                         Executor=shell Token=86ab70918fc87c8a8d3a57c21457fb URL=http://10.6.20.230" | awk -F'Executor=' '{print }'
test runner

另一件需要注意的事情是,无论出于何种原因,gitlab-runner list 都会打印到 stderr。这就是我在通过管道传输到 awk 之前重定向到 stdout 的原因。也许我没有正确重定向?但这并没有真正意义,因为 awk 在没有等号的情况下选择它。

当从 gitlab-runner 命令进行管道传输时无法在 Executor= 上拆分的原因是 因为 Executor= 字符串不存在 !至少不是那种形式 - gitlab-runner 添加了一些 ANSI color codesESC[0;m 用于重置所有属性):

$ gitlab-runner list 2>&1 | cat -A
default_runner                        ^[[0;m  Executor^[[0;m=shell Token=86ab70918fc87c...
#                                     ^^^^^^          ^^^^^^      <--  ANSI color codes

要证明这一点,请尝试 运行:

$ gitlab-runner list 2>&1 | awk -F 'Executor\x1b\[0;m=' '{print }'
default_runner

有一个open proposal to add an option to disable color output in gitlab-runner, but it has been opened for 10 months already, without much community support. Until they decide to add that option, if you wish, you can strip ANSI color codes, for example like this or this


请注意,在 POSIX and GNU awk 中,-F 都接受 ERE 表达式。所以,Executor= 应该可以正常工作(= 没什么特别的)。要处理空格,可以使用 <space>*Executor=:

$ cmd | awk -F ' *Executor=' 'NR>1 {print }' | cat -A
default_runner$
test runner$