Issue
I am setting up an CI Pipelin by means of Jenkins.
I created the following Jenkinsfile:
pipeline {
agent none
stages {
stage('Build and push image') {
agent {
label 'man'
}
steps {
sh 'docker image build -t login:stage .'
sh 'docker tag login:stage 192.168.66.201:5000/login:stage'
sh 'docker push 192.168.66.201:5000/login:stage'
}
}
stage('Create Service') {
agent {
label 'dev'
}
steps {
sh 'docker service create --name loginService -p 40001:40001 --replicas=3 192.168.66.201:5000/login:stage'
}
}
}
}
I know, I could use the components of docker (docker.withRegistry) but I like to do it manually in first attempt to exactly knew what happens.
But my question is how to check in the "Create Service"-Stage, if a docker service already exists. If he exists I like to execute an update statement.
You've got any idea? Using google or jenkins docs did not really helped me..
Solution
One way for doing this:
docker service ls | grep <service-name> && echo service-exists
If the service with name exists, the echo will execute. Replace the echo with the update command you intend to use.
Answered By - yamenk
Answer Checked By - David Goodson (JavaFixing Volunteer)