-1

Context

While running a test, which imports many scripts, an error is thrown in the script below. In particular, an array initialization: arr=() in Bash leads to an error on GitLab CI (in Alpine docker), whereas it runs fine (no errors), when running on Ubuntu 22.04.1.

Code

In particular, the error is thrown on the following bit of Bash script:

#!/bin/bash

# Structure:github_status
github_repo_exists_locally(){
    github_repo="$1"
    if test -d "$MIRROR_LOCATION/GitHub/$github_repo"; then
        echo "FOUND"
    else
        echo "NOTFOUND"
    fi
}
 
get_git_branches() {
    local -n arr=$1             # use nameref for indirection
    local company=$2
    local git_repository=$3
    arr=() # innitialise array with branches
    
    local theoutput=$(cd "$MIRROR_LOCATION/$company/$git_repository" && git branch --all)
    
    
    # Parse branches from branch list response
    while IFS= read -r line; do
        number_of_lines=$(echo "$theoutput" | wc -l)
        if [ "$number_of_lines" -eq 1 ]; then
            arr+=("${line:2}")
        # Only parse remote branches.
        elif [ "${line:0:17}" == "  remotes/origin/" ]; then
            
            # Remove the substring that identifies a remote branch to get the actual branch name up to the first space.
            # Assumes branch names can't contain spaces
            branch=$(get_rhs_of_line_till_character "${line:17}" " ")
            
            # Filter out the HEAD branch duplicate, by filtering on a substring that indicates the duplicate.
            if [ "${branch:0:10}" != "-> origin/" ]; then
                
                # Filter out git theoutput artifacts of that do not start with a letter or number.
                # Assumes branch names always start with a letter or number.
                # TODO: make this check silent.
                if grep '^[-0-9a-zA-Z]*$' <<<"${branch:0:1}" ;then 
                    
                    # Append the branch name to the array of branches
                    #echo "branch=$branch"
                    arr+=("$branch")
                fi          
            fi
        fi
    # List branches and feed them into a line by line parser
    done <<< "$theoutput"
}

Error message

./test.sh: src/helper/GitHub/helper_github_status.sh: line 56: syntax error: unexpected "(" (expecting "}") ERROR: Job failed: exit code 2

.GitLab-CI.yml

Several issues mentioned in the comments led to errors, because the docker ran an Alpine environment instead of Ubuntu. A combination of the comments led to a valid build status for a trivial bats repository, I am now testing it on the code in the question:

unit_test:
    stage: test
    script:
        - apk update && apk add bash
        - apk add git # Default docker image uses Alpine iso Ubuntu, hence apk.
        - chmod +x *.sh
        - ./install-bats-libs.sh
        - bash ./test.sh

Notes

  • I suspect it may be related to this question, yet I do not yet know how.
  • I manually verified that there is not an open { bracket that is lingering/unclosed, hence, I was wondering:

Question

Why does this error occur, and can I resolve it by changing the array declaration?

a.t.
  • 2,002
  • 3
  • 26
  • 66
  • 3
    *`bit of Bash script`* is a bit misleading when there's ~90 lines of code to debug; consider adding `set -x` at the top of the script to enable debug mode, run the script, review the debug output; if you're still unsure of the issue then update the question with the lines of code causing the issue and the related debug output – markp-fuso Oct 17 '22 at 14:51
  • 1
    Alpine doesn't ship with bash. Did you `apk add` it in your Dockerfile? Also, if you invoke the script like `sh script.sh`, then you need to explicitly invoke it like `bash script.sh`. In Ubuntu, `/bin/sh` symlinks to bash – glenn jackman Oct 17 '22 at 14:53
  • @markp-fuso Thank you, I've removed unused code, and will apply the `set -x` to review the debug output. – a.t. Oct 17 '22 at 15:00
  • @glennjackman thank you, I was not aware alpine does not ship with bash, mostly because it seemed to run the bash without saying "bash is a problem". I will look at GitLab yaml examples to see how to `apk add bash` to the docker container, or otherwise find a docker container that supports `apt` (and bash). – a.t. Oct 17 '22 at 15:02

1 Answers1

0

The errors were indeed caused by the docker running an Alpine environment instead of Ubuntu. A combination of the comments led to the following .gitlab-ci.yml, which resulted in a passing build status for the code above:

unit_test:
    stage: test
    script:
        - apk update && apk add bash
        - apk add git # Default docker image uses Alpine iso Ubuntu, hence apk.
        - chmod +x *.sh
        - ./install-bats-libs.sh
        - bash ./test.sh
a.t.
  • 2,002
  • 3
  • 26
  • 66