Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"]
like image 977
Toni Nurmi Avatar asked Mar 08 '23 14:03

Toni Nurmi


1 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'
        }
  }
}
like image 166
Laszlo Fogas Avatar answered Mar 10 '23 10:03

Laszlo Fogas