gitlab ci scripts during which $? is allowed to be non-zero

pseyfert picture pseyfert · Sep 13, 2016 · Viewed 9.3k times · Source

In our project we have a shell script which is to be sourced to set up environment variables for the subsequent build process or to run the built applications.

It contains a block which checks the already set variables and does some adjustment.

# part of setup.sh
for LIBRARY in "${LIBRARIES_WE_NEED[@]}"
do
  echo $LD_LIBRARY_PATH | \grep $LIBRARY > /dev/null
  if [ $? -ne 0 ]
  then
   echo Adding $LIBRARY
   LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBRARY
  else
   echo Not adding $LIBRARY
  fi
done

i.e. it checks if a path to a library is already in $LD_LIBRARY_PATH and if not, adds it. (To be fair, this could be written differently (like here), but assume the script is supposed to achieve something which is very hard to do without calling a program, checking $? and then either doing one thing or doing another thing).

The .gitlab-ci.yml then contains

before_script:
  - yum install -y <various packages>
  - source setup.sh

but the runner decides to stop the before script the very moment $? is non-zero, i.e. when the if-statement decides to add a path to $LD_LIBRARY_PATH. Now it is nice that the gitlab runner checks $? after each line of my script, but here it'd be great if the lines in .gitlab-ci.yml were considered atomic.

Is there a way to avoid the intermediate checks of $? in a script that's sourced in .gitlab-ci.yml?

Answer

andlrc picture andlrc · Sep 13, 2016

Use command_that_might_fail || true to mask the exit status of said command.

Also note that you can use grep -q to prevent output:

echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY" || true

This will however also mask $? which you might not want. If you want to check if the command exits correct you might use:

if echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY"; then
  echo "Adding $LIBRARY"
else
  ...
fi

I suspect that gitlab-ci sets -e which you can disabled with set +e:

set +e # Disable exit on error
for library in "${LIBRARIES_WE_NEED[@]}"; do
  ...
done
set -e # Enable exit on error

Future reading: Why double quotes matter and Pitfalls with set -e