SED命令无法在GitLab Runner上正确执行

发布于 2025-02-11 20:06:09 字数 896 浏览 1 评论 0原文

我有一个脚本需要运行NPM运行测试并捕获测试覆盖范围值 因此,在这里,我试图捕获值36.95

并以Gitlab脚本输出,我打算将其添加为GitLab作业。看来跑步者是GNU。如果我在本地上执行以下脚本,它可以正常工作,就像返回36.95一样。 但是,如果我在gitlab上运行它,它将返回npm运行测试的值。如何使其与我使用的跑步者兼容。

RES=$(npm test -- --coverage --watchAll=false)
TOTAL=$(sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/' <<< $RES)
echo "Current test coverage is : " $TOTAL

我认为这里是弦乐(&lt;&lt;&lt;)与跑步者不兼容,这就是为什么它可以在我的本地机器上工作,但在gitlab上也不适用,也许我的脚本也有一些问题。 gitlab pipleline config

test:
  stage: test
  image: node:16.13.1
  before_script:
    - npm i
    - npx node -v
    - npx npm -v
  script:
    - echo "running test coverage"
    - bash test.sh

test.sh包含顶部提到的脚本

I have a script which needs to run npm run test and capture test coverage coverage value
enter image description hereso here I am trying to capture the value 36.95

and output it in gitlab script which I am planning to add as a gitlab job. It seems that the runner is gnu. If I execute the below script on my local it works fine as in returns 36.95.
However if I run it on gitlab it returns the value of the npm run test . How can I make it compattible with the runner I am using.

RES=$(npm test -- --coverage --watchAll=false)
TOTAL=$(sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/' <<< $RES)
echo "Current test coverage is : " $TOTAL

I think here-string(<<<) is not compatible with the runner which is why it works on my local machine but not on gitlab maybe there is some issue with my script too.
Gitlab pipleline config

test:
  stage: test
  image: node:16.13.1
  before_script:
    - npm i
    - npx node -v
    - npx npm -v
  script:
    - echo "running test coverage"
    - bash test.sh

test.sh contains the script mentioned at the top

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2025-02-18 20:06:09

您可以直接将sed管道

total=$(npm test -- --coverage --watchAll=false | sed -En '/All files/s/^[^|]*\| +([[:digit:].]+).*$/\1/p')
echo "Current test coverage is :  $total"

You could just pipe directly to sed

total=$(npm test -- --coverage --watchAll=false | sed -En '/All files/s/^[^|]*\| +([[:digit:].]+).*$/\1/p')
echo "Current test coverage is :  $total"
橘虞初梦 2025-02-18 20:06:09

您可以尝试 &lt;&lt;&lt;&lt;&lt;&let code>的替代

TOTAL=$(echo "${RES}"|grep "All files"|sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/')

方案您要sed

You can try the alternative to <<<

TOTAL=$(echo "${RES}"|grep "All files"|sed -E 's/^.* All files \| ([^ ]+) \| .*$/\1/')

Meaning you isolate the line you want to sed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文