How can I use Jar produced by mvn package in another Jenkins declarative pipeline stage?

Viewed 4118

I'm trying to build CI/CD pipeline with Jenkins for Maven project. I can't seem to find any decent examples on how to use .jar file produced by mvn package in another Jenkins declarative pipeline stage. I need the jar file to make an docker-image before uploading it to docker-registry. Here's my relevant parts of jenkinsfile:

pipeline {
  agent none
  stages{
    stage('Build Jar'){
        agent {
          docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
          }
        }
        steps {
            sh 'mvn package'
            stash includes: 'target/*.jar', name: 'targetfiles'
        }
    }
    stage('Deploy') {
        agent {
            node {
                label 'DockerDefault'
            }
         }

      steps {
            script{
                def image = docker.build("image-name:test", ' .')
            }
      }
    }
  }
}

Dockerfile:

#install OS
FROM centos
#install java
RUN yum install -y java
#make directory structure to store temporary files
RUN mkdir -p /store
#put jar into container
ADD target/AdWordsProducer-1.0-SNAPSHOT-shaded.jar adwordsproducer.jar
#run jar
ENTRYPOINT ["java", "-jar", "/adwordsproducer.jar"]

EDIT 1----------------------------------------------------------------------:

Laszlos answer did the trick, as I noticed that my .jar file was under different name than dockerfile assumed. Here is my working final jenkinsfile:

pipeline {
  agent none
  stages{
    stage('Build Jar'){
        agent {
          docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
          }
        }
        steps {
            sh 'mvn package'
            stash includes: 'target/*.jar', name: 'targetfiles'
        }
    }
    stage('Deploy') {
        agent {
            node {
                label 'DockerDefault'
            }
         }

      steps {
            script{
                unstash 'targetfiles'
                sh 'ls -l -R'
                def image = docker.build("image-name:test", ' .')
            }
      }
    }
  }
}

And modified dockerfile:

#install OS
FROM centos
#install java
RUN yum install -y java
#make directory structure to store temporary files
RUN mkdir -p /store
#put jar into container
#ADD target/AdWordsProducer-1.0-SNAPSHOT-shaded.jar adwordsproducer.jar
ADD target/AdWordsProducer-1.0-SNAPSHOT.jar adwordsproducer.jar
#run jar
ENTRYPOINT ["java", "-jar", "/adwordsproducer.jar"]
2 Answers

You are stashing the jar files under the name 'targetfiles'. This is good.

What I think is missing is to pop from the stash, try adding the unstash line as shown bellow. The files will be in the exact same location as when you stashed them. Under "target/" in your case.

stage('Deploy') {
  steps {
        script{
            unstash 'targetfiles'
            sh 'docker build image-name:test'
        }
  }
}

You can use the same dockerfile for all stages and just install docker cmd on it and mount the docker.sock to it.

Add this to the Dockerfile:

RUN apt-get update && \
    apt-get install -y apt-transport-https ca-certificates curl software-properties-common && \
    curl -fsSL https://download.docker.com/linux/centos/gpg | apt-key add - && \
    add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/centos trusty stable" && \
    apt-get update && \
    apt-get install -y docker-ce

And run set the Jenkinsfile like this:

pipeline {
    agent {
       docker {
         image 'maven:3-alpine'
         args '-u root -v /root/.m2:/root/.m2 -v /var/run/docker.sock:/var/run/docker.sock'
       }
    }
    stage('Build Jar'){
        steps {
            sh 'mvn package'
            stash includes: 'target/*.jar', name: 'targetfiles'
        }
    }
    stage('Deploy') {

      steps {
            script{
                sh 'docker build image-name:test'
            }
      }
    }
}
Related