I have some steps in my jenkins pipeline that build and image and deploy that image to the swarm.
It's an image for an API service, so I want to run apitester tool with configuration files on the server where the image has been deployed.
It would probably be a script on the server that would be trigger with SSH from the jenkins-agent in the pipeline that what have something looks something like this:
#!/bin/bash
docker run --net=host -v "$(pwd)":/apiconf docker.registry.com:5000/apitester:1.0.0 apitester -credentials /apiconf/credentials.txt -scenario /apiconf/api.yml -strict-mode;
echo status: $?
Important is the last command that return a 1 if the command before passed OK, exited without error. echo status: $?
I would like to save the output of that command as a an environment variable in the Jenkinsfile pipeline which I would then use as a conditional in a next step.
Essentially if OUTPUT is 1 then it would run a slacksend function ELSE it would put back the old image before the pipeline run.
So my question is what is the best way to get that usable environment variable from those set of commands?
CodePudding user response:
You can simply use returnStatus: true
in a shell step to get the status of the execution. Read more here.
def status = sh( script: 'docker run --net=host -v "$(pwd)":/apiconf docker.registry.com:5000/apitester:1.0.0 apitester -credentials /apiconf/credentials.txt -scenario /apiconf/api.yml -strict-mode', returnStatus: true )
echo "${status}"
if(status == 0){
echo "Success"
}