gitlab ci脚本在哪个$期间?允许为非零 [英] gitlab ci scripts during which $? is allowed to be non-zero

查看:114
本文介绍了gitlab ci脚本在哪个$期间?允许为非零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们有一个shell脚本,该脚本将被用作设置环境变量以用于后续的构建过程或运行所构建的应用程序.

In our project we have a shell script which is to be sourced to set up environment variables for the subsequent build process or to run the built applications.

它包含一个块,用于检查已设置的变量并进行一些调整.

It contains a block which checks the already set variables and does some adjustment.

# part of setup.sh
for LIBRARY in "${LIBRARIES_WE_NEED[@]}"
do
  echo $LD_LIBRARY_PATH | \grep $LIBRARY > /dev/null
  if [ $? -ne 0 ]
  then
   echo Adding $LIBRARY
   LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBRARY
  else
   echo Not adding $LIBRARY
  fi
done

即它检查库路径是否已经在$LD_LIBRARY_PATH中,如果没有,则添加它. (为公平起见,这可以写成不同的(例如),但假设脚本应该实现不调用程序,检查$?然后做一件事或做另一件事就很难做到的事情.

i.e. it checks if a path to a library is already in $LD_LIBRARY_PATH and if not, adds it. (To be fair, this could be written differently (like here), but assume the script is supposed to achieve something which is very hard to do without calling a program, checking $? and then either doing one thing or doing another thing).

.gitlab-ci.yml然后包含

before_script:
  - yum install -y <various packages>
  - source setup.sh

,但是跑步者决定在$?不为零的那一刻(即,如果if语句决定向$LD_LIBRARY_PATH添加路径)立即停止之前脚本. 现在很好了,gitlab运行程序在脚本的每一行之后都检查$?,但是如果.gitlab-ci.yml中的行被视为原子行,那就太好了.

but the runner decides to stop the before script the very moment $? is non-zero, i.e. when the if-statement decides to add a path to $LD_LIBRARY_PATH. Now it is nice that the gitlab runner checks $? after each line of my script, but here it'd be great if the lines in .gitlab-ci.yml were considered atomic.

在源于.gitlab-ci.yml的脚本中是否有办法避免对$?进行中间检查?

Is there a way to avoid the intermediate checks of $? in a script that's sourced in .gitlab-ci.yml?

推荐答案

使用command_that_might_fail || true隐藏该命令的退出状态.

Use command_that_might_fail || true to mask the exit status of said command.

还请注意,您可以使用grep -q阻止输出:

Also note that you can use grep -q to prevent output:

echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY" || true

但是,这也会屏蔽您可能不需要的$?.如果要检查命令是否正确退出,则可以使用:

This will however also mask $? which you might not want. If you want to check if the command exits correct you might use:

if echo "$LD_LIBRARY_PATH" | grep -q "$LIBRARY"; then
  echo "Adding $LIBRARY"
else
  ...
fi

我怀疑gitlab-ci设置了-e,可以使用set +e禁用它:

I suspect that gitlab-ci sets -e which you can disabled with set +e:

set +e # Disable exit on error
for library in "${LIBRARIES_WE_NEED[@]}"; do
  ...
done
set -e # Enable exit on error

未来阅读:为什么双引号很重要

Future reading: Why double quotes matter and Pitfalls with set -e

这篇关于gitlab ci脚本在哪个$期间?允许为非零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆