React app build failed but job success

I currently setting up the gitlab-ci.yml file for my react project. I don’t know why but if the project failed to compile (With any type of error …), the gitlab job still success. We can see in the job details in gitlab

 Creating an optimized production build...
 Failed to compile.
 ./src/pages/page500/Page500.js
   Line 26:81:  Unexpected use of 'history'  no-restricted-globals
 Search for the keywords to learn more about each error.
Running after_script
00:01
Saving cache
00:01
Uploading artifacts for successful job
00:02
 Job succeeded

Please find below the gitlab-ci.yml file :

stages:
    - build_test
    - build_production
    - deploy_test
    - deploy_production

build_test:
    image: node:12.17.0
    stage: build_test
    tags:
        - npm-build
    script:
        - unset CI
        - npm install
        - npm run build:development
    only:
        - development
    artifacts:
        when: on_success
        name: settlement-build-development
        paths:
            - build
        expire_in: 1 hour

build_production:
    image: node:12.17.0
    stage: build_production
    tags:
        - npm-build
    script:
        - unset CI
        - npm install
        - npm run build:production
    only:
        - master
    artifacts:
        when: on_success
        name: settlement-build-production
        paths:
            - build
        expire_in: 1 hour

deploy_test:
    image: alpine
    stage: deploy_test
    tags:
        - deploy
    script:
        - apk add --no-cache rsync openssh
        - mkdir -p ~/.ssh
        - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa
        - chmod 600 ~/.ssh/id_dsa
        - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
        - rsync -rav --delete ./build/ -e 'ssh -p 2222' user@XX.XXX.XX.XX:/var/www/website.com/
    only:
        - development
        
deploy_production:
    image: alpine
    stage: deploy_production
    tags:
        - deploy
    script:
        - apk add --no-cache rsync openssh
        - mkdir -p ~/.ssh
        - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa
        - chmod 600 ~/.ssh/id_dsa
        - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
        - ls -lah ./build/
        - rsync -rav --delete ./build/ -e 'ssh -p 2222' user@XX.XXX.XX.XX:/var/www/website.com/
    only:
        - master

And please find the script part of packages.json :

 "scripts": {
    "start": "react-scripts start",
    "build": "react-app-env --env-file=.env.${BUILD_ENV} build",
    "build:development": "BUILD_ENV=development npm run build",
    "build:production": "BUILD_ENV=production npm run build",
    "test": "react-scripts test",
    "test:cov": "npm test -- --coverage --watchAll=false",
    "test:debug": "react-scripts --inspect-brk test --runInBand",
    "eject": "react-scripts eject"
  },

Someone already have this problem with gitlab ?

THanks in advance

To fix your problem you will need to make your build fails explicitly!

To do so, you will need to just add || exit 1 if your build fails like that:

"build:production": "BUILD_ENV=production npm run build || exit 1",

This will make the CI stop and exit 1;