jenkins-common.sh 2.27 KB
Newer Older
1 2
#!/usr/bin/env bash

3 4
set -e

5 6
source $HOME/jenkins_env

7
NODE_ENV_DIR=$HOME/nenv
8
NODE_VERSION=6.11.1
9 10 11

NODE_INSTALL_COMMAND="nodeenv --node=$NODE_VERSION --prebuilt $NODE_ENV_DIR --force"

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# Clear the mongo database
# Note that this prevents us from running jobs in parallel on a single worker.
mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'

# Ensure we have fetched origin/master
# Some of the reporting tools compare the checked out branch to origin/master;
# depending on how the GitHub plugin refspec is configured, this may
# not already be fetched.
git fetch origin master:refs/remotes/origin/master

# Reset the jenkins worker's virtualenv back to the
# state it was in when the instance was spun up.
if [ -e $HOME/edx-venv_clean.tar.gz ]; then
    rm -rf $HOME/edx-venv
    tar -C $HOME -xf $HOME/edx-venv_clean.tar.gz
fi

# Activate the Python virtualenv
source $HOME/edx-venv/bin/activate
31

32
# add the node packages dir to PATH
33
PATH=$PATH:node_modules/.bin
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
echo "setting up nodeenv"
pip install nodeenv
# Ensure we are starting with a clean node env directory
rm -rf $NODE_ENV_DIR

# Occasionally, the command to install node hangs. We need to catch that and retry.
# Note that this will retry even if the command itself fails.
WAIT_COUNT=0
until timeout 30s $NODE_INSTALL_COMMAND || [ $WAIT_COUNT -eq 2 ]; do
    echo "re-trying to install node version..."
    sleep 2
    WAIT_COUNT=$((WAIT_COUNT+1))
done

# If we tried the max number of times, we need to quit.
if [ $WAIT_COUNT -eq 2 ]; then
    echo "Node environment installation command was not successful. Exiting."
    exit 1
fi

source $NODE_ENV_DIR/bin/activate
echo "done setting up nodeenv"
echo "node version is `node --version`"
echo "npm version is `npm --version`"

# TODO: Provide a cached node_modules/ directory for faster/smaller installs

62 63
# Manage the npm cache on Jenkins.
# (In this case, remove it. That ensures from run-to-run, it is a clean npm environment)
64
echo "--> Cleaning npm cache"
65
npm cache clean
66 67 68 69 70

# Log any paver or ansible command timing
TIMESTAMP=$(date +%s)
export PAVER_TIMER_LOG="test_root/log/timing.paver.$TIMESTAMP.log"
export ANSIBLE_TIMER_LOG="test_root/log/timing.ansible.$TIMESTAMP.log"
71 72

echo "This node is `curl http://169.254.169.254/latest/meta-data/hostname`"