/ maint / ci_log_span_fns.sh
ci_log_span_fns.sh
 1  # Intentionally no shebang here - this script should be sourced.
 2  
 3  # Provides functions for adding sections to gitlab CI output.
 4  # See <https://docs.gitlab.com/ee/ci/jobs/job_logs.html#custom-collapsible-sections>
 5  #
 6  # Some undocumented behaviors discovered experimentally:
 7  # * Section titles must be unique, but aren't displayed in the rendered UI.
 8  #   Only the section *descriptions* are displayed.
 9  # * Sections can be nested, which explains why the section-end string has to specify
10  #   the title of the section that they are ending.
11  # * Likewise, yes, sections without a section-end string don't work properly in
12  #   the UI.
13  #
14  # For ease of use we don't expose the section-nesting functionality in the API
15  # below.  In return:
16  # * `section_end` doesn't need to specify which section it's
17  #    ending.
18  # * `section_start` automatically ends the current section.
19  # * We automatically generate section-titles (which aren't displayed).
20  
21  # Tell shellcheck the expected shell:
22  # shellcheck shell=bash
23  
24  # Track the current section
25  current_section_number=0
26  current_section_title=""
27  current_section_description=""
28  current_section_start_time=""
29  
30  # starts a section (first ending the current one, if any)
31  function section_start () {
32    local section_description="${1}"
33  
34    # If we're already in a section, end it.
35    if [ -n "$current_section_title" ]; then
36      section_end
37    fi
38  
39    local now
40    now=$(date +%s)
41  
42    ((current_section_number+=1))
43    current_section_title="section-$current_section_number"
44    current_section_description="$section_description"
45    current_section_start_time="$now"
46  
47    # gitlab magic string to start the section
48    echo -e "section_start:$now:${current_section_title}[collapsed=true]\r\e[0K${current_section_description}"
49  }
50  
51  # ends the current section.
52  function section_end () {
53    local now
54    now=$(date +%s)
55  
56    # While gitlab shows the duration of collapsed sections,
57    # it doesn't properly collapse sections whose start has "scrolled off".
58    # So show the duration here too, with a nice grep'able prefix.
59    echo "SECTION-END: '$current_section_description' finished in $((now-current_section_start_time)) seconds"
60  
61    # gitlab magic string to end the section
62    echo -e "section_end:$now:${current_section_title}\r\e[0K"
63  
64    # clear section vars
65    current_section_title=""
66    current_section_description=""
67    current_section_start_time=""
68  }