Saturday, October 29, 2022

[SOLVED] Bash: initialising an array arr=(), throws error in GitLab CI, expected }

Issue

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?


Solution

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


Answered By - a.t.
Answer Checked By - Willingham (WPSolving Volunteer)