💾Thananjhayan
← Back to notes

A Minimal 3-Stage Jenkins Pipeline

October 19, 2025

Three stages are enough to take a repo from git to published images.

Stage mapping

StageName in JenkinsfileWhat it does
1Pull Git Repositorygit branch:'main', url:'<repo url>'
2Build Docker Imagesdocker build backend2 + frontend2 images
3Push to Docker Hubdocker login then docker push both images

docker login lives inside the Push stage, so it doesn't need a stage of its own.

Steps

  1. Jenkins → New Item → name → Pipeline → OK.
  2. Pipeline section → Pipeline script from SCM → Git → your repo URL → branch main → Script Path Jenkinsfile. Save. (Or paste the Jenkinsfile directly using Pipeline script.)
  3. Add Docker Hub creds (ID dockerhub-creds) under Manage Jenkins → Credentials.
  4. Edit the placeholders: YOUR_DOCKERHUB_USERNAME, YOUR_APP_NAME, and the repo URL.
  5. Build Now → all 3 stages go green → verify the images on Docker Hub.

Final Jenkinsfile (3 stages)

pipeline {
    agent any
    environment {
        DOCKERHUB_USER  = 'YOUR_DOCKERHUB_USERNAME'
        APP_NAME        = 'YOUR_APP_NAME'
        DOCKERHUB_CREDS = credentials('dockerhub-creds')
    }
    stages {
        stage('Pull Git Repository') {
            steps { git branch: 'main', url: 'https://github.com/YOUR_USERNAME/YOUR_REPO.git' }
        }
        stage('Build Docker Images') {
            steps {
                sh 'docker build -f backend2/DOCKERFILE  -t $DOCKERHUB_USER/${APP_NAME}-backend:latest  ./backend2'
                sh 'docker build -f frontend2/DOCKERFILE -t $DOCKERHUB_USER/${APP_NAME}-frontend:latest ./frontend2'
            }
        }
        stage('Push to Docker Hub') {
            steps {
                sh 'echo $DOCKERHUB_CREDS_PSW | docker login -u $DOCKERHUB_CREDS_USR --password-stdin'
                sh 'docker push $DOCKERHUB_USER/${APP_NAME}-backend:latest'
                sh 'docker push $DOCKERHUB_USER/${APP_NAME}-frontend:latest'
            }
        }
    }
    post { always { sh 'docker logout || true' } }
}

End-to-end flow

Install/run Jenkins → install plugins → add Docker Hub credentials → New Item → Pipeline → Pipeline from SCM (your repo, Jenkinsfile) → Build Now → Jenkins checks out the code → builds backend & frontend images → docker login → pushes both images to Docker Hub → verify on Docker Hub.

Related: [[jenkins-jenkinsfile-explained]], [[docker-multi-container-app]].

💬 Comments