Skip to content

Commit dd3411e

Browse files
committed
Fail build if there are non-zero exit codes in build scripts
Uses `set -eo` to stop a build if any part of the `build-production` script returns a non-zero code, even if it's part of a series of pipes. This can be turned off for particular parts of the scripts if necessary, but it seems like a sensible default.
1 parent e7a2705 commit dd3411e

5 files changed

Lines changed: 18 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,4 @@ Use the mu-script `setup-ide` to install these without running in development mo
307307
Changes to dependencies in `package.json` will be picked up and an updated `package-lock.json` will be copied into the mounted sources provided `package-lock.json` is enabled. Local packages are installed through the `package-lock.json` and the template's dependencies are merged in. The `package-lock.json` thus only contains your own dependencies and should not conflict with future upgrades in the template.
308308

309309
### Custom build commands
310-
To execute custom bash statements during the image build (e.g. to install aditional system libraries), provide an `on-build.sh` script in the root of your service. It will be automatically picked up and executed by the Docker build.
310+
To execute custom bash statements during the image build (e.g. to install aditional system libraries), provide an `on-build.sh` script in the root of your service. It will be automatically picked up and executed by the Docker build. Make sure to return a non-zero return code in case of failure, in order to fail the build. For example using `set -eo` to exit on any non-zero statuses or if any part of a pipe fails.

build-production.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# so we can warn at runtime in case developers accidentally mount
77
# sources without setting the development environment variable.
88

9+
# Fail build on error
10+
set -eo
11+
912
source ./helpers.sh
1013

1114
# Copy sources from /app to where they can be built

npm-install-dependencies.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
3+
# Fail build on error
4+
set -eo
5+
26
source ./helpers.sh
37

48
environment=$1

transpile-sources.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
3+
# Fail build on error
4+
set -eo
5+
26
source ./helpers.sh
37

48
####

validate-package-json.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#!/bin/bash
2+
3+
# Fail build on error
4+
set -eo
5+
26
cd /usr/src/app/
37
node ./validate-package-json.js
48
cd /usr/src/app/app/
59

610
## Ensure package.json has module
711
if [ -f /usr/src/app/app/package.json ]
812
then
9-
cat /usr/src/app/app/package.json | jq -e ".type" > /dev/null
10-
if [ $? -ne 0 ]
13+
PACKAGE_TYPE=`cat /usr/src/app/app/package.json | jq -r ".type"`
14+
if [[ "$PACKAGE_TYPE" -eq "null" ]]
1115
then
1216
echo '[WARNING] Adding "type": "module" to your package.json.'
1317
echo 'To remove this warning, add "type": "module" at the same level as "name" in your package.json'
1418
sed -i '0,/{/s/{/{\n "type": "module",/' /usr/src/app/app/package.json
1519
else
16-
PACKAGE_TYPE=`cat /usr/src/app/app/package.json | jq -r ".type"`
1720
if [[ "$PACKAGE_TYPE" -ne "module" ]]
1821
then
1922
echo '[WARNING] DIFFERENT TYPE THAN "module" IN package.json; CONTINUING WITH UNSPECIFIED BEHAVIOUR'

0 commit comments

Comments
 (0)