Skip to content

Step 1 - Build & Push

In the first step of our project, we will create a simple pipeline that builds our Java application, creates a Docker image, and pushes it to JFrog Artifactory.

Jenkinsfile

Here is the Jenkinsfile for this step. Source code: 30-01-Jenkinsfile-docker-build-push-jfrog.

pipeline {
  agent any
  options {
    disableConcurrentBuilds()
    disableResume()
    buildDiscarder(logRotator(numToKeepStr: '10'))
    timeout(time: 1, unit: 'HOURS')
  }
  tools {
    maven 'maven-3.6.3' 
  }
  environment {
    DOCKER_REGISTRY = "vigneshsweekaran.jfrog.io"
    DOCKER_REPOSITORY = "docker-helloworld-local"
    IMAGE_NAME = "hello-world-java"
    IMAGE_TAG = "1.${BUILD_NUMBER}"
    DOCKER_CREDENTIAL_ID = "jfrog-credential"
  }
  stages {
    stage ('Build') {
      steps {
        sh 'mvn clean package'
      }
    }
    stage ('Docker Build') {
      steps {
        script {
          docker.build("${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}")
        }
      }
    }
    stage ('Docker Push') {
      steps {
        script {
          docker.withRegistry("https://${DOCKER_REGISTRY}", "${DOCKER_CREDENTIAL_ID}") {
            docker.image("${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${IMAGE_NAME}:${IMAGE_TAG}").push() 
          }           
        }
      }
    }
  }
  post {
    always {
      deleteDir()
    }
  }
}

Detailed Explanation

Environment Block

The environment block defines global variables accessible by all stages. - DOCKER_REGISTRY: The URL of your JFrog Artifactory registry. - IMAGE_TAG: A unique tag for each build (1.${BUILD_NUMBER}) to ensure traceability.

Stages

  • Build: Compiles the Java application using Maven.
  • Docker Build: Uses the Docker Pipeline plugin to build the image.
  • Docker Push: Authenticates with JFrog using jfrog-credential and pushes the tagged image.

Important Tips

Tip

Always use a unique tag (like $BUILD_NUMBER) for your docker images. Using latest makes it hard to rollback or know exactly what code is running.

Next Step: Deploy to Environments

Quick Quiz

Quick Quiz

#

Which Docker Pipeline plugin method is used to build a Docker image?

#

Which Docker Pipeline plugin method is used to authenticate with a Docker registry?

#

What does the docker.image("image:tag").push() method do?

📬 DevopsPilot Weekly — Learn DevOps, Cloud & Gen AI the simple way.
👉 Subscribe here