wheeler
wheeler

Reputation: 3241

Jenkins not running bash script correctly

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

Upvotes: 0

Views: 1996

Answers (1)

Alex O
Alex O

Reputation: 8164

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.

Upvotes: 1

Related Questions