bhreinb
bhreinb

Reputation: 181

Docker Entrypoint Script To Support One or Two or Both Commands (namely npm config & npm run)

I run tests with testcafe & cucumberjs using docker which is built on top of nodejs. The test run are initiated via npm scripts. I detail the workflow below...

1) Git pull pre built docker image

2) Optionally set a 'config' parameter in the package.json file for example...npm config set packageJson:task-profile auto-regression-chrome

3) Execute the runner script i.e. npm run e2e-test

my shell script is able to cater for 3 only but I'd to evolve so that it caters for 2) & 3). My shell script at present looks like this

#!/bin/sh
set -e

if [ "$1" == 'npm' ] && [ "$2" == 'run' ]; then
    shift 2

    XVFB_SCREEN_WIDTH=${SCREEN_WIDTH-1280}
    XVFB_SCREEN_HEIGHT=${SCREEN_HEIGHT-720}

    dbus-daemon --session --fork
    Xvfb :1 -screen 0 "${XVFB_SCREEN_WIDTH}x${XVFB_SCREEN_HEIGHT}x24" >/dev/null 2>&1 &
    export DISPLAY=:1.0
    fluxbox >/dev/null 2>&1 &
    exec npm run "$@"
fi


# otherwise, just run whatever command was passed on
exec "$@"

Any ideas on how to evolve the script so to cater for 2) & 3). Typically I clone the image & execute the runner command but it would be nice to have a shell script that allows me to set some npm config first and execute npm run.

More details re intention:

Looking at https://github.com/bhreinb/SYSTAC#running-tests-on-docker

I do the following when running tests on docker

1) Build the docker image

docker build --no-cache -t bhreinb/systac .

2) Run the tests on docker and throw away the image...

docker run --rm -it -v $PWD/e2e/:/opt/systac/e2e bhreinb/systac npm run e2e-test

The limitation is the config needs to be setup in source control before running the tests. I want to be able to dynamically update the config, run the tests and throw away the image using the one command i.e.

docker run --rm -it -v $PWD/e2e/:/opt/SYSTAC/e2e -v $PWD/reports/:/opt/SYSTAC/reports/ bhreinb/SYSTAC npm config set packageJson:task-profile auto-regression-chrome && npm run e2e-test

hope that makes sense?

Upvotes: 2

Views: 771

Answers (1)

tripleee
tripleee

Reputation: 189427

Here's a tentative quick hack to allow you to pass in configuration parameters with -v options.

#!/bin/sh
set -e

# Quick and dirty option processing
while [ $1 = '-v' ]; do
    case $2 in
      *=*)
        option=${2%=*}
        npm config set "$option" "${2#$option}"
        shift 2;;
      *)
        echo "$0: Fatal error: argument to -v must contain =" >&2
        exit 127;;
    esac
done

# Aside; use = not == inside [ ... ]    
if [ "$1" = 'npm' ] && [ "$2" = 'run' ]; then
    shift 2

    XVFB_SCREEN_WIDTH=${SCREEN_WIDTH-1280}
    XVFB_SCREEN_HEIGHT=${SCREEN_HEIGHT-720}

    dbus-daemon --session --fork
    Xvfb :1 -screen 0 "${XVFB_SCREEN_WIDTH}x${XVFB_SCREEN_HEIGHT}x24" >/dev/null 2>&1 &
    export DISPLAY=:1.0
    fluxbox >/dev/null 2>&1 &
    exec npm run "$@"
fi

exec "$@"

Usage:

docker run --rm -it \
   -v $PWD/e2e/:/opt/SYSTAC/e2e \
   -v $PWD/reports/:/opt/SYSTAC/reports/ \
  bhreinb/SYSTAC \
   scriptname -v foo=bar \
      -v packageJson:task-profile=auto-regression-chrome \
      -v baz=quux \
     npm run gargleblaster --demo --crash --splinters

Upvotes: 2

Related Questions