Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active January 16, 2024 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deraen/7011933 to your computer and use it in GitHub Desktop.
Save Deraen/7011933 to your computer and use it in GitHub Desktop.
Grunt build script for CI (Jenkins)
# Create build-directory in project and place this package.json in that dir
{
"name": "build",
"version": "0.0.0",
"private": true,
"description": "Grunt-cli & Bower",
"dependencies": {
"grunt-cli": "0.1.11",
"bower": "1.2.8"
},
"engines": {
"node": "~0.10.20"
}
}
#!/usr/bin/env bash
# Idea for npm/bower cache from: https://github.com/heroku/heroku-buildpack-nodejs/blob/diet/bin/compile
# Exit with error if any command fails
set -e
# Tells bower to use non-interactive mode
CI=true
echo "Building UI project using Grunt"
echo "Retrieve latest Node"
cd build
if [[ ! -d "jq" ]]; then
# Download jq - json processor
mkdir jq
cd jq
curl http://stedolan.github.io/jq/download/linux64/jq -s -o jq
chmod +x jq
cd ..
fi
PATH="$(pwd)/jq:$PATH"
semver_range=$(cat package.json | jq -r .engines.node)
# Old curl versions don't have support for --data-urlencoded
semver_urlencoded=$(perl -MURI::Escape -e 'print uri_escape($semver_range);' "$2")
node_version=$(curl --silent --get -d "range=${semver_range}" https://semver.io/node/resolve)
echo "Using node version: $node_version (semver: $semver_range)"
if [[ ! -d "node-v$node_version-linux-x64" ]]; then
rm -fr node-v*-linux-x64
# Selected node version doesn't exist
node_url="http://s3pository.heroku.com/node/v$node_version/node-v$node_version-linux-x64.tar.gz"
# extract into ./node-v<version>-linux-x64
curl $node_url -s -o - | tar xzf - -C .
fi
PATH="$(pwd)/node-v${node_version}-linux-x64/bin:$PATH"
# Install bower & grunt into "global" npm prefix
echo "Install build tools"
npm --no-color install
npm prune
PATH="$(pwd)/node_modules/.bin:$PATH"
cd ..
cd ui
set +e
echo "Installing node dependencies"
npm --no-color install
if [[ $? -ne 0 ]]; then
echo "First try with npm failed. Clearing and trying again"
rm -rf node_modules/
set -e
npm --no-color install
fi
set -e
# Don't exit if bower fails the first time
set +e
echo "Installing bower dependencies"
bower --no-color install
if [[ $? -ne 0 ]]; then
echo "First try with bower failed. Clearing and trying again"
rm -r app/components/
# Exit if bower fails again
set -e
bower --no-color install
fi
set -e
echo "Prune unnecessary deps"
npm --no-color prune
bower --no-color prune
echo "Running UI unit tests"
grunt --no-color env:test mochaTest:unit
echo "Running grunt build"
grunt --no-color build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment