How to start/stop Appium server in Jenkins Pipeline

Aphicha Intaragumhaeng
3 min readNov 5, 2019

Did you ever want to manage your Appium server programmatically but unfortunately the CLI doesn’t have a built-in command to manage it directly then how can it integrate with the other CI/CD system like Jenkins

Especially with Jenkins Pipeline where everything is defined as code in your Jenkinsfile, not much plugin magic and you still want to keep that advantages of maintaining a reproducible pipeline codebase

This article will focus more on Jenkins’s declarative pipeline style but the idea and concept is still the same

Problem

Appium server sometimes it does not stop when you try to close it, server and port is still hanging forever and the Appium CLI has no build-in command to stop the server which makes it harder to manage programmatically

Imagine that you want to manage it programmatically with the automation process in your CI/CD pipeline such as Jenkins it could be a really painful story

appium
or
appium & (run as background process)

The example command to start Appium server that can be stopped only when you terminate it but sometimes it not stops

Solution

I’ve searched for the answer on StackOverflow for a long time and none of them answer directly to my question

So far what seems to work is that you have to kill the process of the server manually in the shell with the specific process ID

To make it simply work with the pipeline, we could have a short version of the command

kill $(lsof -t -i :4723) [In Shell]
kill \$(lsof -t -i :${APPIUM_PORT}) [In Jenkinsfile]

Where the APPIUM_PORT is your Appium port, the default port is 4723
lsof command should work in Unix-like systems e.g. MacOS, Linux

What is lsof command ?

lsof is a command meaning “list open files”, which is used in many Unix-like systems to report a list of all open files and the processes that opened them

By running this command it should return an ID of the process running on that specific port to use for the kill signal

More lsof resources

How to implement in Jenkinsfile

To implement in the pipeline Add this step at the end of your Jenkinsfile

post{
always{
...
echo "Stop appium server"
sh "kill \$(lsof -t -i :${APPIUM_PORT})"
}
success{
...
}
failure{
...
}
cleanup{
...
}
}

It should kill the hanging Appium server process and you can start the new Appium server again with the same port!

Conclusion

So it’s not completely impossible to manage start/stop for the Appium just by adding one line of code

If you have to test multiple devices at the same time you can manage start/stop the Appium allocate with the difference port programmatically for each environment within the pipeline code and to make sure your resources are not hanging forever

Example of the full Jenkinsfile

pipeline {    agent any    environment {
APPIUM_PORT= 5555
}
stages {
stage('Build') {
steps {
echo "Building.."
}
}
stage('Test') {
steps {
echo "Testing.."
sh "appium --port ${APPIUM_PORT}"
...
}
}
stage('Deploy') {
steps {
echo "Deploying...."
}
}
post {
always{
...
echo "Stop appium server"
sh "kill \$(lsof -t -i :${APPIUM_PORT})"
}
success{
...
}
failure{
...
}
cleanup{
...
}
}
}
}

Your life with Jenkins pipeline and mobile devices automate testing will now be happy and glory again

Hope it helps

--

--