munHunger
munHunger

Reputation: 2999

Jenkins pipeline working in different folders

Alright so I am just learning about pipelines in Jenkins and I've ran into a small problem. It is building my war file in one directory but trying to build the docker image in another one, which will ofcourse fail.

so a shorthand log describes the problem quite well:

[Pipeline] stage
[Pipeline] { (build war)
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/Wunderbaren@2
[Pipeline] {

[Pipeline] stage
[Pipeline] { (build dockerimage)
[Pipeline] script
[Pipeline] {
[Pipeline] dir
Running in /root/.jenkins/workspace/Wunderbaren/backend
[Pipeline] {

the Jenkinsfile:

pipeline {
    agent any
    stages {
        stage('build war') {
            agent {
                docker { image 'gradle:latest' }
            }
            steps {
                sh 'gradle war -b backend/build.gradle'
            }
        }
        stage('build dockerimage') {
            steps {
                script {
                    dir('backend/') {
                        def image = docker.build("munhunger/wunderbaren")

                        docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
                            image.push("${env.BUILD_NUMBER}")
                            image.push("latest")
                        }
                    }
                }
            }
        }
    }
}

What I find odd is that I have a similar project with pretty much the exact same configuration. only differs in folder names and docker tag. And that seems to be working 100% of the times, so I feel quite lost on this one!

Upvotes: 1

Views: 8192

Answers (2)

Joel Anderson
Joel Anderson

Reputation: 535

From Jenkins Pipeline Documentation

The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional.

I believe this means the 'build war' stage will execute in a separate environment from the 'build docker image' stage. As far as similar syntax working in a different job, perhaps the same agent is defined for both stages?

Upvotes: 1

munHunger
munHunger

Reputation: 2999

Turns out you need to reuse the node:

stage('build war') {
    agent {
        docker { 
            image 'gradle:latest'
            reuseNode true
        }
    }
    steps {
        sh 'gradle war -b backend/build.gradle'
    }
}

From the documentation I found at https://go.cloudbees.com/docs/cloudbees-documentation/use/reference/pipeline/

reuseNode A boolean, false by default. If true, run the container in the node specified at the top-level of the Pipeline, in the same workspace, rather than on a new node entirely. This option is valid for docker and dockerfile, and only has an effect when used on an agent for an individual stage.

Upvotes: 6

Related Questions