Jenkins: check connectivity

2023-09-23 1 min read jenkins Brialius

Jenkinsfile to check connectivity to a host and port.

node {
    stage('Check connectivity') {
        def nameValid = "${HOST}" ==~ /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/
        def portValid = "${PORT}" ==~ /^\d+$/
        if (!nameValid) {
            error "Invalid host: ${HOST}"
        }
        if (!portValid) {
            error "Invalid port: ${PORT}"
        }
        currentBuild.description = "${HOST}:${PORT}"
        sh "nc -zv -w5 ${HOST} ${PORT}"
    }
}