3

package.json script block in file:

"scripts": {
  ...
  "test:schema": "./src/schemas/schema-test.sh"
}

.gitlab-ci.yml file contents:

image: node:12

stages:
  - lint
  - test
  # - build

.yarn_install:
  before_script:
    - yarn config set @private:registry https://npm.private.io
    - echo "//npm.private.io/:_authToken=${NPM_TOKEN}" > ~/.npmrc
    - yarn install
    - export PATH="./node_modules/.bin:${PATH}"

prettier:
  stage: lint
  script:
    - yarn config set @private:registry https://npm.private.io
    - echo "//npm.private.io/:_authToken=${NPM_TOKEN}" > ~/.npmrc
    - yarn add prettier
    - yarn lint

schema test:
  stage: test
  script:
    - yarn test:schema

variables:
  GIT_DEPTH: 10

schema-test.sh file contents:

#/usr/bin/env bash

# Test all file ends with schema.json via ajv

CURRENT_DIR=`dirname "$0"`

cd $CURRENT_DIR

for SCHEMA_FILE in *.schema.json
do
    SAMPLE_FILE=samples/${SCHEMA_FILE/schema/sample}
    echo Schema file: $SCHEMA_FILE
    if [ -f $SAMPLE_FILE ]
    then
        echo Found sample file: $SAMPLE_FILE
        npx ajv -s $SCHEMA_FILE -d $SAMPLE_FILE
    else
        echo "*NO* sample file found for $SCHEMA_FILE"
    fi
done

Gitlab CI error message:

...
51 $ export PATH="./node_modules/.bin:${PATH}"
52 $ yarn test:schema
53 yarn run v1.21.1
54 $ ./src/schemas/schema-test.sh
55 ./src/schemas/schema-test.sh: 11: ./src/schemas/schema-test.sh: Bad substitution
56 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
57 error Command failed with exit code 2.
61 ERROR: Job failed: command terminated with exit code 1
  • The CI error said there was a problem with line 11 of the schema-test.Sh file, but I didn't see the problem.

  • They work well in a MacOS environment, and my guess is that CI's Docker image is Linux, causing some compatibility issues.

  • Or is it just a matter of the ' / ' symbol not being Escape Code? I'm confused.

Thank you for all of your help!

==================================================

  • According to @chepner said to make changes, but the test is still a problem
23 $ yarn test:schema
24 yarn run v1.21.1
25 $ ./src/schemas/schema-test.sh
26 Schema file: dev-assistant.schema.json
27 Found sample file: samples/dev-assistant.sample.json
28 npx: installed 6 in 1.124s
29 command not found: ajv
30 Schema file: form.schema.json
31 *NO* sample file found for form.schema.json
32 Schema file: news.schema.json
33 *NO* sample file found for news.schema.json
34 Schema file: repos.schema.json
35 Found sample file: samples/repos.sample.json
36 npx: installed 6 in 0.911s
37 command not found: ajv
38 Schema file: team-members.schema.json
39 Found sample file: samples/team-members.sample.json
40 npx: installed 6 in 0.902s
41 command not found: ajv
42 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
43 error Command failed with exit code 1.
47 ERROR: Job failed: command terminated with exit code 1
liby
  • 679
  • 6
  • 13
  • I don't see an error in this statement either, but I would run the command using `set -x`, or at least do the `echo` before calculating SAMPLE_FILE, to see what is the value of SCHEMA_FILE? BTW, for the case the file name contains white space, I would either double-quote the variable inside the `if` or use `[[....]]` instead of `[ ... ]`. – user1934428 Jan 23 '20 at 15:34
  • Your script isn't being executed with `bash`: a `bash` error message would read `bad substitution`, not `Bad substitution`. The script is likely being executed using `sh ...` without regard to your shebang, and `sh` on your machine is linked to some other POSIX shell (likely `dash`). – chepner Jan 23 '20 at 15:54
  • The shebang, by the way, is missing the `!`. Correcting that to `#!/usr/bin/env bash` may resolve the issue if the system isn't running `sh ...` explicitly. – chepner Jan 23 '20 at 15:59
  • Thanks for the heads-up. I'll give it a try. @user1934428 – liby Jan 23 '20 at 16:02
  • I also saw this problem in [shellcheck](https://www.shellcheck.net/) and I will correct it and test it again. @chepner – liby Jan 23 '20 at 16:05

1 Answers1

2

Your script isn't executed with bash, so don't use bash-specific features. Assuming you want to change foo.schema.json to foo.sample.json, you can use instead

SAMPLE_FILE=samples/${SCHEMA_FILE%.schema.json}.sample.json

This removes .schema.json from the expansion of SCHEMA_FILE, then adds .sample.json back explicitly.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • It still doesn't work, I tried to change `#/usr/bin/env bash` to `#!/usr/bin/env bash` – liby Jan 23 '20 at 16:20
  • What doesn't work? This should have fixed the bad substitution error at least. (Or do you mean your original code still doesn't work? As I said my first comment, the shebang isn't even used if the CI is running your script by calling `sh` explicitly.) – chepner Jan 23 '20 at 16:22
  • - I tested this by changing `#/usr/bin/env bash` to `#! /usr/bin/env bash` or using `SAMPLE_FILE=samples/${SCHEMA_FILE%.schema.json}.sample.json` will solve the bad substitution. - But a new error was reported: `command not found: ajv`. - I updated it at the end of the question. Thank you anyway – liby Jan 23 '20 at 16:30
  • That's an *entirely* separate problem. I consider this question resolved. – chepner Jan 23 '20 at 16:41