Home > OS >  fail jenkins ssh script in case a port is unreachable
fail jenkins ssh script in case a port is unreachable

Time:02-02

I am trying to make a final check on my jenkins script, in case a port is available I want the script (build) to be a success, and in case its not available to mark it as failure. How can I achieve this easily?

this is the final check

$ nc -zv 10.10.10.24 3306
Connection to localhost 3306 port [tcp/mysql] succeeded!

CodePudding user response:

You could use catchError() to explicitly set build results:

catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
  sh 'nc -zv 10.10.10.24 3306'
}

See: https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/

But from what I understand from your question, this may already happen by just executing the sh step. If it fails, the pipeline will also fail in this stage / step (If you don't catch / handle errors in another way).

Edit: Maybe you also want to set a timeout for nc like:

nc -zv 10.10.10.24 3306 -w 60
  •  Tags:  
  • Related