Search code examples
bashawkgitlab-ci-runner

List Gitlab runners by name only


I am trying to get a list of only the names of gitlab runners.

So the output of gitlab-runner list 2>&1 is:

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

Note that the names of the runners can contain spaces in them.

So I've tried the following:

gitlab-runner list 2>&1 | awk -F'Executor' '{if(NR>1)print $1}' which gives me pretty much what I want (except with trailing spaces that I'll need to remove).

default_runner
test runner

However, if I change the field separator to Executor= in hopes of making it more explicit, it no longer works. It returns the entire line.

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

I've tried escaping it with Executor\= to no avail. How can I include the equal sign in my split?

Edit:

It works if I take one of the lines and echo it into awk

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

Another thing to note is that, for whatever reason, gitlab-runner list prints to stderr. That is why I redirect to stdout before I pipe to awk. Maybe I'm not redirecting properly? But that doesn't really make sense since awk picks it up without equal sign.


Solution

  • The reason why you can't split on Executor= when piping from the gitlab-runner command is because Executor= string is not present! At least not it that form - gitlab-runner adds some ANSI color codes (ESC[0;m is used to reset all attributes):

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

    To prove it, try running:

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

    There's an 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.


    Note that in both POSIX and GNU awk, -F accepts an ERE expression. So, Executor= should work in general (nothing special about =). To deal with spaces, you can use <space>*Executor=:

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