Jenkins 运行 构建失败时的 shell 命令作为对 运行 失败的 TestNG 计划的 post 构建操作

Jenkins run a shell command when build is failed as a post build action to run failed TestNG plan

我想在同一个项目的 shell 脚本下面重新运行,一旦我的构建完成但有错误,所以我可以在 testng-failed.xml.[= 中重新运行我失败的测试用例18=]

FILE=./target/surefire-reports/testng-failed.xml
if test -f "$FILE"; then
    echo "$FILE exists."
mvn clean test -Dhttp.proxyHost=proxy-dev.aws.skynet.com -Dhttp.proxyPort=8080 -Dproxy-dev.aws.skynet.com -Dhttps.proxyPort=8080 -Dbrowser="${Browser}" -Dbrowser_version="${browser_version}" -Dos_version="10" -Dos="Windows" -Dlocalrun="false"  -Dtestxml=FILE
else
   echo "$FILE is not there"
fi

但是在 Jenkins 的 post 构建部分,我没有看到添加执行 shell 的选项。请帮忙。

我安装了 post build 插件,但它只对 maven 项目显示,对我的 free style 项目不显示。

在自由风格项目中,没有 post 个步骤选项。

我在没有任何插件的情况下设法解决了这个问题,并且仍然 运行 在 Jenkins 自由风格项目中。添加两个执行 shells 并在第一个 shell 输入下面的 shell 命令。

export M2_HOME=/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3
export PATH=$PATH:$M2_HOME/bin
mvn --version
cd CommonPagesStorage
mvn clean install -DskipTests
cd ..
cd MultiWebdriverDemo
mvn clean test -Dmaven.test.failure.ignore=true -Dsurefire.suiteXmlFiles=TestSuites/${TestPlanName} 
echo "${TestPlanName}is ran"

#Dmaven.test.failure.ignore=true added this so even the script failed it doesn't mark as fail and mark it as unstable and continue.

在第二个 shell 命令中,有条件地检查 testng-failed.xml 是否存在 运行 它到 运行 失败的测试用例。

cd MultiWebdriverDemo
export M2_HOME=/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3
export PATH=$PATH:$M2_HOME/bin
mvn --version

#this to print current directory.
echo "the second Script executed from: ${PWD}"

if [ -e ./target/surefire-reports/testng-failed.xml ]
then
    echo "ok file found pass"
    mvn  test -Dmaven.test.failure.ignore=true -Dsurefire.suiteXmlFiles=target/surefire-reports/testng-failed.xml
else
    echo "file not found passed in first attempt ..."
    exit 0
    #exit 0 means forcefully mark it as passed

fi

if [ -e ./target/surefire-reports/testng-failed.xml ]
then
    echo "Rerun also failed exiting with buil state as failure "
    exit 1
 #exit 1 means forcefully mark it as failed
else
    echo "file not found rerun is passed marking the build as passed ..."
     exit 0
fi

S0 即使在第二次,我的 webdriver 测试用例也失败了,失败的构建标记为失败。

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.199 s
[INFO] Finished at: 2021-08-12T14:05:36Z
[INFO] ------------------------------------------------------------------------
+ [ -e ./target/surefire-reports/testng-failed.xml ]
+ echo Rerun also failed exiting with buil state as failure 
Rerun also failed exiting with buil state as failure 
+ exit 1
Build step 'Execute shell' marked build as failure
Archiving artifacts
TestNG Reports Processing: START
Looking for TestNG results report in workspace using pattern: **/testng-results.xml
Saving reports...
Processing '/var/jenkins_home/jobs/August-FreeStyle/builds/37/testng/testng-results.xml'
100.000000% of tests failed, which exceeded threshold of 0%. Marking build as UNSTABLE
TestNG Reports Processing: FINISH
Finished: FAILURE