Howto check site working in the Jenkins pipeline
Task: Check site working (response code = 200) in the Jenkins pipeline step
Implementation sh script:
url='https://scalan.com'
attempts=5
timeout=10
online=false
echo "Checking status of $url."
i=1
while [ "$i" -le "$attempts" ]; do
code=`curl -sL --connect-timeout 20 --max-time 30 -w "%{http_code}\\n" "$url" -o /dev/null`
echo "Found code $code for $url."
if [ "$code" = "200" ]; then
echo "Website $url is online."
online=true
break
else
echo "Website $url seems to be offline. Waiting $timeout seconds."
sleep $timeout
fi
i=$(( i + 1 ))
done
if $online; then
echo "Monitor finished, website is online."
exit 0
else
echo "Monitor failed, website seems to be down."
exit 1
fi
Jenkins piplene:
pipeline {
agent any
// environment { }
stages {
stage('checkout') {
steps {
sh '''
echo 'Checkout...'
# ...
'''
}
}
stage('build') {
steps {
sh '''
echo 'Build...'
# ...
'''
}
}
stage('update') {
steps {
sh '''
echo 'Update...'
# ...
'''
}
}
stage('wait') {
steps {
sh '''
echo 'Wait for update beanstalk application...'
sleep 2m
echo "Done..."
'''
}
}
stage('check') {
steps {
sh '''
echo 'Check site working...'
url='https://scalan.com'
attempts=5
timeout=10
online=false
echo "Checking status of $url."
i=1
while [ "$i" -le "$attempts" ]; do
code=`curl -sL --connect-timeout 20 --max-time 30 -w "%{http_code}\\n" "$url" -o /dev/null`
echo "Found code $code for $url."
if [ "$code" = "200" ]; then
echo "Website $url is online."
online=true
break
else
echo "Website $url seems to be offline. Waiting $timeout seconds."
sleep $timeout
fi
i=$(( i + 1 ))
done
echo "Done..."
if $online; then
echo "Monitor finished, website is online."
exit 0
else
echo "Monitor failed, website seems to be down."
exit 1
fi
'''
}
}
}
}
Done.
