1

I want to rerun, below shell script in the same project, once my build is i completed with errors, so I can rerun my failed test cases in testng-failed.xml.

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

But in Jenkins, post build section, I don't see an option to add Execute shell. Please help.

Post build plugin is available in maven

enter image description here

I installed post build plugin , but it's only show for maven projects and not for my free style project.

In free style project, No post steps option. No post step option in freestyle project

Sameera De Silva
  • 1,722
  • 1
  • 22
  • 41
  • 1
    Does this answer your question? [Execute Shell Script after post build in Jenkins](https://stackoverflow.com/questions/11160363/execute-shell-script-after-post-build-in-jenkins) – Noam Helmer Aug 11 '21 at 14:23
  • Is there are any straight forward way of doing it ? I'm using a shared Jenkins server with other teams so adding a plugin need an internal approval. – Sameera De Silva Aug 11 '21 at 14:39
  • Nope. You need that plugin to enable that functionality when using freestyle jobs. You can use a pipeline job instead which will enable this logic out of the box. – Noam Helmer Aug 11 '21 at 14:43
  • it's not usable in free style project , seems it's only for maven projects as I only see Post steps options in the Maven project only. – Sameera De Silva Aug 12 '21 at 04:40
  • What do you mean not usable? we are using it it in many freestyle jobs. If you cant use it just extract the post build logic to a separate job and call it from the post build of the original job - that way you will have full control. – Noam Helmer Aug 12 '21 at 07:19
  • Yes , it seems weird may be a version issue , did not see a post steps option in free style project but found that in maven project. Anyway, thanks a lot for your kind support. I did found a walkaround for this . Thank you. – Sameera De Silva Aug 12 '21 at 11:30

1 Answers1

0

I managed a walkaround to do this without any plugins and still run in the Jenkins free style project. Add two execute shells and in first shell enter the below shell commands.

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.

In the second shell command ,conditionally checks the testng-failed.xml is exists if so run it to run the failed test cases.

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

enter image description here

S0 even in the second time, my webdriver test case is failed, it's failed build mark as a failure.

[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
Sameera De Silva
  • 1,722
  • 1
  • 22
  • 41