0

i have a dockerfile which is built with an ARG (name of the git branch). I want to run 2 if statements which test the name of the git branch: So if it is the test branch then it will run "npm run test", if it is master branch then "npm run build". I've tried this but it does not work :

RUN if [ "$GIT_BRANCH" = "test" ] ; then CMD npm test ; fi

RUN if [ "$GIT_BRANCH" = "master" ] ; then CMD npm build ; fi

It works with echo but not with npm commands. why ?

thank you

YoussHark
  • 558
  • 1
  • 9
  • 26
  • 1
    Possible duplicate of [RUN inside a conditional statement in Dockerfile](https://stackoverflow.com/questions/51518087/run-inside-a-conditional-statement-in-dockerfile) – Matias Fuentes Apr 07 '19 at 21:04
  • In what way does it not work?? – larsks Apr 07 '19 at 21:32
  • @larsks it says /bin/sh: 1: CMD: not found The command '/bin/sh -c if [ "$GIT_BRANCH" = test" ] ; then CMD npm test ; fi' returned a non-zero code: 127 – YoussHark Apr 07 '19 at 21:56

1 Answers1

1

in case someone has the same problem one day : if you want to run an npm command inside if statements within a dockerfile, the key is to write:

RUN if [ "$GIT_BRANCH" = "test" ] ; then  /usr/bin/npm test --force ; fi

RUN if [ "$GIT_BRANCH" = "master" ] ; then /usr/bin/npm build --force ; fi

And it will work. The --force is essential otherwise it will launch a git error.

YoussHark
  • 558
  • 1
  • 9
  • 26