Let me start with a brief description of the use case I want to implement.
mvn clean install
. I can also do a mvn package
to create an executable jar, which I can use in a Docker image.docker-compose up
to launch my environment. This is fine, but it is an asynchronous step: I don't know how much time it will take to have all services completely started an accepting requests.docker-compose down
to tear down the test environment.waitUntil
step, but I am looking for some guidance...waitUntil
step?Should I implement a simple shell script that issues an HTTP request to one of my app REST endpoints in a loop and that completes as soon as the app sends a response? Should I invoke this script in the
waitUntil
step?
Yes, that is one legitimate approach.
You could also simply fold this into the script which runs your tests. To keep the Pipeline script short and to the point, and make it easier to test pieces of logic in isolation, store that script in SCM. Assuming it is in Bash (but Python or whatever would work fine):
while :
do
if curl http://endpoint/
then
echo Up and running
break
else
echo Still waiting
fi
done
make test
and then your Pipeline script could read something like
node {
stage 'build'
checkout scm
sh 'mvn clean install'
stage 'test'
sh 'docker-compose up'
try {
sh './run-tests-when-ready'
finally {
sh 'docker-compose down'
}
}
Of course the call to docker-compose
could even be put into such an external script if you like. Tricky to do the cleanup reliably in Bash (can use trap EXIT '…'
), easier in a real language.