我有这样的一行:
sed -i 's/"host: TND_HOST"/"host: process.env.TND_HOST"/g' services/management/tnd.js
上面的选项会导致 linting 错误:
This GitLab CI configuration is invalid: (<unknown>): mapping values are not allowed in this context at line [...]
其他不起作用的选项是:
This GitLab CI configuration is invalid: (<unknown>): mapping values are not allowed in this context at line [...]
有什么办法可以克服这个问题并将其保留为一句台词吗?
pat*_*pat 34
由于您使用两种类型的引号,因此将命令放入 yaml 模板中可能是最简单的方法。这样你就不需要逃避任何事情:
stages:
  - lint
.sed_template: &sed_template |
 sed -i 's/"host: TND_HOST"/"host: process.env.TND_HOST"/g' services/management/tnd.js
some_job:
  image: someImage:latest
  stage: lint
  except:
    - master
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - frontend/node_modules/
  script:
    - echo "firstLine"
    - *sed_template
    - echo "lastLine"
它不再是一个单行代码,但我想它是最干净的选项,因为它使命令本身保持可读性。另一种选择是使用折叠样式,将其缩小一点:
stages:
  - lint
some_job:
  image: someImage:latest
  stage: lint
  except:
    - master
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - frontend/node_modules/
  script:
    - echo "firstLine"
    - >
        sed -i 's/"host: TND_HOST"/"host: process.env.TND_HOST"/g' services/management/tnd.js
    - echo "lastLine" 
Val*_*Shi 13
您不必正式转义冒号。请参阅下面的示例。
一种方法是将命令的参数(用单引号括起来的冒号)放入变量中。您可以在参数字符串中使用变量。
然后eval使用如下变量的“字符串化”命令(该示例用于curl触发外部部署,但应该适用于每个命令):
deploy_job:
  variables:
    DEPLOY_CURL_COMMAND: 'curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $DO_APP_PLATFORM_API_TOKEN" https://api.digitalocean.com/v2/apps/$DO_APP_PLATFORM_STAGE_FRONT_APP_ID/deployments'
  script:
    - echo "Stage Deploy to DigitalOcean App Platform"
    - echo $DEPLOY_CURL_COMMAND
    - 'eval "$DEPLOY_CURL_COMMAND"'
还有一些其他方法可以避免: (冒号后跟空格)问题:
  script:
    # 1. Note how the single quotes eliminate colon+space issue.
    - 'MESSAGE="Tests finished with status: ${CI_JOB_STATUS}"'
    # 2. Here pipe operator allows using colon+space on the next line.
    - |
      curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" "$SOME_URL"
    # 3. Here we refrain from using pipe operator and instead wrap the entire command in sigle quotes as in 1. 
    # Note that I replaced single with double quotes on HTTP header.
    - 'curl -X POST -H "Content-type: application/json" --data "{\"text\":\"$MESSAGE\"}" "$SOME_URL"'
脚本中的所有引号、破折号和空格都要非常精确。YAML 非常无情。使用 GitLab 管道编辑器和 linter检查语法。实际上,首先在编辑器中开发管道要快得多。
如果您使用项目/存储库级别变量,请不要忘记在protected设置变量并在不受保护的分支(这是分支默认状态)中使用它时取消选中复选框(默认情况下选中)。
| 归档时间: | 
 | 
| 查看次数: | 22397 次 | 
| 最近记录: |