【问题标题】:How do I fix bash in gitlab ci :bad substitution如何在 gitlab ci 中修复 bash:错误替换
【发布时间】:2020-01-23 15:09:49
【问题描述】:

package.json 文件中的脚本块:

"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

【问题讨论】:

  • 我在这个语句中也没有看到错误,但我会使用set -x 运行命令,或者至少在计算 SAMPLE_FILE 之前执行echo,以查看架构文件?顺便说一句,对于文件名包含空格的情况,我会在if 内用双引号引起变量,或者使用[[....]] 而不是[ ... ]
  • 您的脚本未使用 bash 执行:bash 错误消息将显示为 bad substitution,而不是 Bad substitution。该脚本很可能使用sh ... 执行而不考虑您的shebang,并且您计算机上的sh 链接到其他一些POSIX shell(可能是dash)。
  • 顺便说一句,shebang 缺少!。如果系统未明确运行sh ...,则将其更正为#!/usr/bin/env bash 可能会解决问题。
  • 感谢您的提醒。我会试一试。 @user1934428
  • 我在shellcheck也看到了这个问题,我会更正它并再次测试它。 @chepner

标签: linux bash shell gitlab-ci gitlab-ci-runner


【解决方案1】:

您的脚本不是用bash 执行的,所以不要使用bash 特有的功能。假设你想把foo.schema.json改成foo.sample.json,你可以改用

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

这会从SCHEMA_FILE 的扩展中删除.schema.json,然后明确添加.sample.json

【讨论】:

  • 还是不行,我试着把#/usr/bin/env bash改成#!/usr/bin/env bash
  • 什么不起作用?这至少应该修复了错误的替换错误。 (或者你的意思是你的原始代码仍然不起作用?正如我所说的我的第一条评论,如果 CI 通过显式调用 sh 来运行你的脚本,甚至不会使用 shebang。)
  • - 我通过将 #/usr/bin/env bash 更改为 #! /usr/bin/env bash 或使用 SAMPLE_FILE=samples/${SCHEMA_FILE%.schema.json}.sample.json 来测试这一点,将解决错误替换问题。 - 但是报了一个新的错误:command not found: ajv。 - 我在问题的最后更新了它。还是谢谢你
  • 这是一个完全独立的问题。我认为这个问题已经解决。
猜你喜欢
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2014-01-04
相关资源
最近更新 更多