jenkins pipeline script to build and push artifacts into repository

Jenkins pipeline script to build Java application and push artifacts into repository

Last Updated on February 18, 2023 by cscontents

Introduction

In this article we will discuss on building one sample Java application and pushing the build artifacts into JFrog artifactory using Jenkins pipeline script (declarative).

Prerequisite

Before starting we assume you have knowledge on –

  • Jenkins pipeline as a script
  • Java application build using maven
  • Git, GitHub
  • Repository like JFrog artifactory, nexus etc.

If you want to learn Jenkins, Docker, Kubernetes etc., then I would recommend to check out the below courses from KodeKloud which offers one of the best learning courses in the DevOps world.

Jenkins Course URL: https://kodekloud.com/courses/jenkins/?aff=827846 

Docker Learning Path: https://kodekloud.com/learning-path-docker/?aff=827846

Kubernetes with Hands-on Course URL: https://kodekloud.com/courses/kubernetes-for-the-absolute-beginners-hands-on/?aff=827846

Note: The above links are affiliate links, if you enroll this course using the above link, then it would help us to get some monetary benefit from KodeKloud. It won’t cost you anything.

Tools/Technology used

Below are the tools/technology which we used –

  • Sample application based on Java
  • Source code management tool – Git
  • Source code repository – GitHub
  • Build tool – Maven
  • Pipeline making tool – Jenkins
  • To store the build artifacts – JFrog artifactory

Block diagram of pipeline

jenkins pipeline block diagram

 

Jenkins Pipeline Script

Below are some prerequisite Jenkins configuration for the pipeline to run.

  • We need to provide the java path in Jenkins “Global Tool Configuration” (Manage Jenkins → Global Tool Configuration)

java home configuration in jenkins pipeline

 

In your case JAVA_HOME path might be different, provide the correct path from your Jenkins machine.

 

  • We need to provide the maven path in Jenkins “Global Tool Configuration” (Manage Jenkins → Global Tool Configuration)

maven home configuration in jenkins pipeline

In your case MAVEN_HOME path might be different, provide the correct path from your Jenkins machine.

 

  • To configure JFrog artifactory server (or JFrog cloud URL) details with Jenkins we installed “JFrog” plugin in Jenkins (Manage Jenkins → Configure System)

JFrog configuration in jenkins pipeline

 

Below is the Jenkins pipeline script (declarative).

pipeline {
    agent any
    tools {
        // In Jenkins, we have configured maven as “LocalMVN”, in your case you can any such name
        maven "LocalMVN"
    }
    stages {
        stage('Checkout') {
            steps {
               withCredentials([string(credentialsId: 'GitHub_Token', variable: 'github_token')]) {
                checkout([$class: 'GitSCM',
                          branches: [[name: '*/master']],
                                extensions: [[$class: 'CleanCheckout']],
                                userRemoteConfigs: [[url: 'https://' + env.github_token + '@' + 'github.com/sk617/simple-java-maven-app.git']]
                       ])
               }
            }
        }
        stage('Build') {
            steps {
                sh "mvn -Dmaven.test.failure.ignore=true clean package"
            }
        }
        stage('Push artifacts into artifactory') {
            steps {
              rtUpload (
                serverId: 'my-artifactory',
                spec: '''{
                      "files": [
                        {
                          "pattern": "*.war",
                           "target": "example-repo-local/build-files/"
                        }
                    ]
                }'''
              )
          }
        }
    }
}

In the above pipeline,

Stage: Checkout

In this stage source code will be fetched from GitHub.

If your GitHub repo is a public repository, then no need to provide GitHub credential in the pipeline. In the above pipeline we have provided “github_token” to fetch private repositories, but if you configure your pipeline as shown in the above pipeline it will work for both private repo and public repo.

Stage: Build

In this stage source code will be built using maven tool. Output of this build will be a war file which we will push into JFrog artifactory. You can use any are repository like Nexus etc. where the build artifacts will be pushed.

Stage: Push artifacts into artifactory

The artifacts which are built in previous stage those will be pushed into JFrog artifactory in this stage.

 

Thank you.

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.