bash 在 Jenkins“Execute Shell”中运行“exec”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20379202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:52:30  来源:igfitidea点击:

Running the "exec" command in Jenkins "Execute Shell"

c++linuxbashc++11jenkins

提问by ossys

I'm running Jenkins on a Linux host. I'm automating the build of a C++ application. In order to build the application I need to use the 4.7 version of g++ which includes support for c++11. In order to use this version of g++ I run the following command at a command prompt:

我在 Linux 主机上运行 Jenkins。我正在自动构建 C++ 应用程序。为了构建应用程序,我需要使用 4.7 版的 g++,其中包括对 c++11 的支持。为了使用这个版本的 g++,我在命令提示符下运行以下命令:

exec /usr/bin/scl enable devtoolset-1.1 bash

So I created a "Execute shell" build step and put the following commands, which properly builds the C++ application on the command prompt:

因此,我创建了一个“执行 shell”构建步骤并放置了以下命令,这些命令在命令提示符下正确构建了 C++ 应用程序:

exec /usr/bin/scl enable devtoolset-1.1 bash
libtoolize
autoreconf --force --install
./configure --prefix=/home/tomcat/.jenkins/workspace/project
make
make install
cd procs
./makem.sh /home/tomcat/.jenkins/workspace/project

The problem is that Jenkins will not run any of the commands after the "exec /usr/bin/scl enable devtoolset-1.1 bash" command, but instead just runs the "exec" command, terminates and marks the build as successful.

问题是 Jenkins 不会在“exec /usr/bin/scl enable devtoolset-1.1 bash”命令之后运行任何命令,而是只运行“exec”命令,终止并将构建标记为成功。

Any ideas on how I can re-structure the above so that Jenkins will run all the commands?

关于如何重新构建上述内容以便 Jenkins 运行所有命令的任何想法?

Thanks!

谢谢!

回答by Destroyica

At the begining of your "Execute shell" script, execute source /opt/rh/devtoolset-1.1/enableto enable the devtoolet "inside" of your shell.

在“执行 shell”脚本source /opt/rh/devtoolset-1.1/enable的开头,执行以启用 shell“内部”的 devtoolet。

Which gives:

这使:

source /opt/rh/devtoolset-1.1/enable
libtoolize
autoreconf --force --install
./configure --prefix=/home/tomcat/.jenkins/workspace/project
make
make install
cd procs
./makem.sh /home/tomcat/.jenkins/workspace/project

回答by Peter Schuetze

I needed to look up what sclactually does.

我需要查看scl实际做了什么。

Examples

scl enable example 'less --version'
    runs command 'less --version' in the environment with collection 'example' enabled
scl enable foo bar bash
    runs bash instance with foo and bar Software Collections enabled

So what you are doing is running a bash shell. I guess, that the bash shell returns immediately, since you are in non-interactive mode. execruns the the command within the shell without creating a new shell. That means if the newly opened bash ends it also ends your shell prematurely. I would suggest to put all your build steps into a bash script (e.g. run_my_build.sh) and call it in the following way.

所以你正在做的是运行一个 bash shell。我猜 bash shell 会立即返回,因为您处于非交互模式。exec在 shell 中运行命令而不创建新的 shell。这意味着如果新打开的 bash 结束,它也会提前结束你的 shell。我建议将所有构建步骤放入 bash 脚本(例如 run_my_build.sh)中,并按以下方式调用它。

exec /usr/bin/scl enable devtoolset-1.1 run_my_build.sh

回答by Mark Setchell

This kind of thing normally works in "find" commands, but may work here. Rather than running two, or three processes, you run one "sh" that executes multiple things, like this:

这种事情通常在“查找”命令中起作用,但在这里可能起作用。与其运行两个或三个进程,不如运行一个“sh”来执行多项操作,如下所示:

exec sh -c "thing1; thing2; thing3"

exec sh -c "thing1; thing2; thing3"

If you require each step to succeed before the next step, replace the semi-colons with double ampersands:

如果您要求每一步都在下一步之前成功,请将分号替换为双与号:

exec sh -c "thing1 && thing2 && thing3"

exec sh -c "thing1 && thing2 && thing3"

I have no idea which of your steps you wish to run together, so I am hoping you can adapt the concept to fit your needs.

我不知道您希望同时运行哪些步骤,因此我希望您可以调整该概念以满足您的需求。

Or you can put the whole lot into a script and exec that.

或者您可以将全部内容放入脚本中并执行。