I've added new logic to my Jenkinsfile that in a specific stage I change agents and run commands such as sh.
Problem:
I wanted to change agents and run commands there. However, it seems like a race condition where if the pipeline's agent runs a docker (in the pipeline declarative block), and in one of the stages you change agent, the agent doesn't run shell commands, it just hangs and stays stuck.
Please notice that I want my docker and label to be on the pipeline declaration block and not in specific stages (using agent none), since I want all stages but the last stage to run in this specific agent and specific docker.
Is there a bug in this script? I made a miniature script that reproduces this "bug", you can run this on your own jenkins to try it out for yourself (add docker-credentials to passwords in jenkins), it seems like a bug with jenkins:
#!/usr/bin/env groovy
pipeline
{
agent
{
docker
{
alwaysPull true
image 'ubuntu:xenial'
registryCredentialsId 'docker-credentials'
registryUrl 'https://index.docker.io/v1/'
label 'master'
}
}
stages
{
stage("Initialize")
{
steps
{
script
{
println("Initializing")
}
}
}
stage("First agent stage")
{
steps
{
script
{
sh(script: "ls", label: "Working shell scripts")
}
}
}
stage("Second agent stage")
{
agent
{
label 'master'
}
steps
{
script
{
sh(script: "ls", label: "Hung shell scripts")
}
}
}
}
}
Notice if you change from this:
agent
{
docker
{
alwaysPull true
image 'ubuntu:xenial'
registryCredentialsId 'docker-credentials'
registryUrl 'https://index.docker.io/v1/'
label 'master'
}
}
to this:
agent
{
label 'master'
}
It starts working again...