0

Using the OpenShift oc new-app command, I have built a container image. Instead of pushing the image to a local container registry, I want to push the generated image to a private registry. As I am using Jenkins for CI/CD, I want to automate the process of generating the image and pushing to the private registry.

I am able to achieve the generation part. But struck with pushing the image to a private registry through Jenkinsfile. Any pointers on how to achieve this is appreciated.

Vidyasagar Machupalli
  • 2,737
  • 1
  • 19
  • 29

1 Answers1

0

This Jenkins Building Docker Image and Sending to Registry article discusses how this might be done.

pipeline {
  environment {
    registry = "gustavoapolinario/docker-test"
    registryCredential = 'dockerhub'
    dockerImage = ''
  }
  agent any
  tools {nodejs "node" }
  stages {
    stage('Cloning Git') {
      steps {
        git 'https://github.com/gustavoapolinario/node-todo-frontend'
      }
    }
    stage('Build') {
       steps {
         sh 'npm install'
       }
    }
    stage('Test') {
      steps {
        sh 'npm test'
      }
    }
    stage('Building image') {
      steps{
        script {
          dockerImage = 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"
      }
    }
  }
}
Nick
  • 1,834
  • 20
  • 32
  • Already tried this but no luck as I see "Docker command not found" error in the Jenkins logs. I have the Docker pipeline plugin already installed on the Jenkins server – Vidyasagar Machupalli Sep 26 '19 at 13:58