package.json
文件中的脚本 block :
"scripts": {
...
"test:schema": "./src/schemas/schema-test.sh"
}
.gitlab-ci.yml
文件内容:
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
文件内容:
#/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 错误信息:
...
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
CI 错误说
schema-test.Sh
文件的第 11 行有问题,但我没有看到问题。它们在 MacOS 环境下运行良好,我的猜测是 CI 的 Docker 镜像是 Linux,导致了一些兼容性问题。
或者只是“/”符号不是转义码的问题?我很困惑。
感谢大家的帮助!
============================================= ===
- 按照@chepner的说法进行修改,但是测试还是有问题
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
最佳答案
您的脚本不是使用 bash
执行的,所以不要使用 bash
特定的功能。假设您想将 foo.schema.json
更改为 foo.sample.json
,您可以改用
SAMPLE_FILE=samples/${SCHEMA_FILE%.schema.json}.sample.json
这会从 SCHEMA_FILE
的扩展中删除 .schema.json
,然后显式添加 .sample.json
。
关于linux - 如何在 gitlab ci :bad substitution 中修复 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59881610/