简体   繁体   English

Jenkins中的Shell脚本

[英]Shell Script in Jenkins

I am runnning shell script in a jenkins pipeline step. 我正在詹金斯管道步骤中运行shell脚本。 Script is doing maven build and some other stuff. 脚本正在执行Maven构建和其他一些工作。 I want pipeline to fail if maven build fails. 如果maven构建失败,我希望管道失败。 I am not sure how to do that. 我不确定该怎么做。

This is my script 这是我的剧本

#!/usr/bin/env bash
echo "Acceptance Test"
cd .. && cd $PWD/transactions-ui
npm run acceptance-start && cd .. &&  cd $PWD/transactions-test/ && mvn verify -Dserenity.reports=email -Dwebdriver.driver=chrome  -Dwebdriver.provided.mydriver=starter.util.RemoteChromeDriver ; cd .. ;  cd $PWD/transactions-ui/ ; npm run acceptance-stop
echo "Test completed"

Following is my jenkins file 以下是我的詹金斯档案

dir("${workspace}/transactions-test")
                {
                    sh "${workspace}/transactions-test/run.sh"
                }

It depends on how you define failure. 这取决于您如何定义失败。 If the exit code of your run.sh script is different than 0, then the build will fail. 如果您的run.sh脚本的退出代码不同于0,则构建将失败。

Add set -e in your bash to exit if any command returns a non-zero status 如果有任何命令返回非零状态,则在bash中添加set -e退出

#!/usr/bin/env bash
set -e
echo "Acceptance Test"
...

Bash set command for reference. Bash set命令供参考。

If set -e can't work for you, you can examine key cmd result as following: 如果set -e对您不起作用,则可以按以下方法检查关键的cmd结果:

#!/usr/bin/env bash
echo "Acceptance Test"
cd ..
cd $PWD/transactions-ui
npm run acceptance-start

if [[ $? -ne 0 ]]; then
  echo "npm run acceptance-start failed"
  exit 1
fi

cd .. 
cd $PWD/transactions-test/
mvn verify \
  -Dserenity.reports=email \
  -Dwebdriver.driver=chrome \
  -Dwebdriver.provided.mydriver=starter.util.RemoteChromeDriver

if [[ $? -ne 0 ]]; then
  echo "mvn verify failed"
  exit 1
fi

cd .. 
cd $PWD/transactions-ui/
npm run acceptance-stop

if [[ $? -ne 0 ]]; then
  echo "npm run acceptance-stop failed"
  exit 1
fi

echo "Test completed"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM