0

I have a declare pipeline, shown as follow:

pipeline{
    agent any
    stages{
        stage("Pull Souce Code"){
            steps{
                    checkout(...)  
                }
            }
        stage("Build and Push image"){
            steps{
                script{
                    docker.withRegistry(...){
                        def image = docker.build(...)
                        image.push()
                    }
                }
            }
        }
    }
}

The pipeline is running on the Jenkins Master(I don't build any jenkins slave). When I run this task concurrently, sometimes the Dockerfile referenced does not match the GitLab project.

I notice the current pipeline have 2 Jenkins workspace: /var/lib/jenkins/workspace/job_name and /var/lib/jenkins/workspace/job_name@2 Their Git files don't match their Dockerfile files.

I have to disable concurrent build now. What should I do?

Bryan Chen
  • 139
  • 1
  • 13
  • Maybe specify a unique workspace is a solution:[customWorkspace "${env.BUILD_NUMBER}"](https://stackoverflow.com/questions/61866110/jenkins-concurrent-builds-on-docker-slaves) – Bryan Chen Jun 14 '22 at 06:18
  • sounds like checkout and docker build run on different workspaces, can you check that in pipeline log? ("Running in [workspace] on [node]") – criztovyl Jun 19 '22 at 10:20
  • I notice that [workspace] cycle use `/var/lib/jenkins/workspace/job_name` and `/var/lib/jenkins/workspace/job_name@2` when I start more than 2 jobs: `jobid 1` is `workspace` and `jobid 2` is `workspace@2`, `jobid 3` is `workspace` and `jobid 4` is `workspace@2`. maybe something get wrong between `jobid1` and `jobid3` – Bryan Chen Jun 22 '22 at 02:36
  • which image tag are you using for push? – criztovyl Jun 22 '22 at 05:09
  • image tag is generated based on parameters. It's not about tag, because dockerfile content is not match gitlab project. I'll transfer Jenkins Slave to K8S, that should solve this issue. – Bryan Chen Jun 23 '22 at 09:32
  • really strange problem. how did you check dockerfile and git do not match? – criztovyl Jun 23 '22 at 12:02
  • Do you mean I maybe wrong during checking dockerfile and git? It's been too long to double check. So are you saying Jenkins doesn't have this problem with concurrency, even between `jobid1` and `jobid3`? – Bryan Chen Jun 27 '22 at 06:12
  • I think calling it `jobidX` is confusing, i would rather call it `workspaceidX`, that makes it more clear: Comparing between them would be across workspaces, which most likely does not make sense. Thus I would say within the same workspace you should not have concurrency problems (unless somehow checkout and image build run in different workspaces). The state across workspaces will be different, but that is not a problem, it's just a leftover and will not be re-used (the checkout will at least overwrite the code managed in SCM). – criztovyl Jun 27 '22 at 06:40
  • How did you roughly check, what was the problem you ran into? If you for example push to latest tag, it will not always be the "latest" because the "older" job finishes later. How is your SCM configured in Jenkins? Is it maybe configured to run for multiple branches? – criztovyl Jun 27 '22 at 06:44

0 Answers0