0

I am trying to execute a build script from Jenkins that builds and deploys a website (the script is multipurpose, but it is executed by running bash ./scripts/deploy.sh -ssh):

#!/usr/bin/env bash

cd ..
rm -rf _site/
if [ "$1" = "-ssh" ]; then
    git clone [email protected]:RIT-EVT/RIT-EVT.github.io.git --branch master --depth 1 _site
else
    git clone https://github.com/RIT-EVT/RIT-EVT.github.io.git --branch master --depth 1 _site
fi

LIVE_VERSION_BUILD=`cat _site/version`

LIVE_VERSION=${LIVE_VERSION_BUILD%.*}
LIVE_BUILD=${LIVE_VERSION_BUILD##*.}
PACKAGE_VERSION=`sed -nE 's/^\s*"version": "(.*?)",$/\1/p' package.json`

if [[ "$LIVE_VERSION" == "$PACKAGE_VERSION" ]]; then
    ((LIVE_BUILD++))
else
    LIVE_VERSION=${PACKAGE_VERSION}
    LIVE_BUILD=0
fi

rm -rf _site/*

jekyll build
echo "$LIVE_VERSION.$LIVE_BUILD" > _site/version

cd _site/
git add -A
git commit -m "v$LIVE_VERSION.$LIVE_BUILD $(date)"
git push
cd ..

The issue I have is bash does not execute the script as expected (tested against running the script on my computer with the same version of bash). I keep receiving the following output, and then the script just fails (I had to work around the above issue by using HTTPS and entering the username and password in the git repo url: https://username:[email protected]/...):

...
Cloning into '_site'...
++ cat _site/version
+ LIVE_VERSION_BUILD=0.0.9.0
+ LIVE_VERSION=0.0.9
+ LIVE_BUILD=0
++ sed -nE 's/^\s*"version": "(.*?)",$/\1/p' ./package.json
+ PACKAGE_VERSION=0.0.9
+ [[ 0.0.9 == \0\.\0\.\9 ]]
+ (( LIVE_BUILD++ ))
Build step 'Execute shell' marked build as failure
Finished: FAILURE

1 Answer 1

1

The issue is probably caused by running the script with set -e enabled:

  • Your command (( LIVE_BUILD++ )) will evaluate to 0 if LIVE_BUILD equals 0
  • The (( [...] )) operator returns status 1 for that case.
  • And if set -e is active, this will make the shell abort immediately.

Solution:

  • add set +e in the beginning of your script, or
  • better: use LIVE_BUILD=$(( LIVE_BUILD + 1)) -- it is more readable and will not fail.
Sign up to request clarification or add additional context in comments.

2 Comments

I see. So [[ 0.0.9 == \0\.\0\.\9 ]] is not unusual behavior? I would expect it to look like [[ 0.0.9 == 0.0.9 ]]. It looks like it's wrong, but if I change around the script to use set +e and change (( LIVE_BUILD++ )) to what you suggested, it looks like it's running as expected.
No, it is not unusual (I suppose the backslashes are added in debug output in order to underline the fact that the dots are not considered to be part of a regular expression).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.