'Jenkins No such property: dockerImage error when trying to deploy image to DockerHub

Hi I have set up a pipeline that pulls from Github when there's a commit and publishes an image to Dockerhub. My Pipeline script is as follows:

 pipeline {
  environment {
    registry = "momo979/purple-beard-team-2"
    registryCredential = 'dockerhub'
    dockerImage= ''
  }
  agent any
  stages {
    stage('Cloning Git') {
      steps {
        git 'https://github.com/Momo979/JenkinsTest.git'
      }
    }
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
    stage('Deploy Image') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
            dockerImage.push()
          }
        }
      }
    }
    stage('Remove Unused docker image') {
      steps{
        sh "docker rmi $registry:$BUILD_NUMBER"
      }
    }
  }
}

The image gets built ok but during the deploy phase I am getting the following message which causes everything to fail.

"groovy.lang.MissingPropertyException: No such property: 
dockerImage for class: groovy.lang.Binding at 
groovy.lang.Binding.getVariable(Binding.java:63)"
 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source