DEV Community

Cover image for Build. Test. Deploy. Repeat – CI/CD with Jenkins, Docker & GitLab
Amaan Hasan
Amaan Hasan

Posted on

Build. Test. Deploy. Repeat – CI/CD with Jenkins, Docker & GitLab

Tired of pushing code and praying it magically works in production?
Welcome to the world of CI/CD, where Jenkins, Docker, and GitLab team up to make your deployments less... explosive.

In this post, I’ll show you how I built a fully automated pipeline that pulls code, builds it, tests it, containers it, and then teleports it to a remote server using good old SSH.

No DevOps degree required — if you can follow a recipe, you can follow this. Let's turn your chaotic deployments into smooth, auto-triggered perfection.

Tech Stack:
Jenkins (Dockerized)
GitLab (Source repository)
Docker & Docker Compose
Maven
Custom Remote Host (Fedora-based with Docker and SSH)

Project Structure:
.
├── Jenkinsfile
├── java-app/
│ ├── src/
│ └── pom.xml
└── jenkins/
├── build/
│ ├── mvn.sh
│ └── build.sh
├── test/
│ └── mvn.sh
├── push/
│ └── push.sh
└── deploy/
├── deploy.sh
└── publish

Docker Containers Involved:
jenkins: Main Jenkins server container
remote-host: Acts as a deployment server (custom Fedora container with SSH & Docker)
gitlab: GitLab server container hosting the code repository

Jenkinsfile Breakdown:

pipeline {

agent any

environment {
    PASS = credentials('Dokcer-registry')
}

stages {
    stage('Build') {
        steps {
            sh '''
                bash ./jenkins/build/mvn.sh
                bash ./jenkins/build/build.sh
            '''
        }
        post {
            success {
                archiveArtifacts artifacts: 'java-app/target/*.jar', fingerprint: true
            }
        }
    }

    stage('Test') {
        steps {
            sh 'bash ./jenkins/test/mvn.sh'
        }
        post {
            always {
                junit 'java-app/target/surefire-reports/*.xml'
            }
        }
    }

    stage('Push') {
        steps {
            sh 'bash ./jenkins/push/push.sh'
        }
    }

    stage('Deploy') {
        steps {
            sh 'bash ./jenkins/deploy/deploy.sh'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

}

Scripts Summary:
build/mvn.sh: Spins up a Maven container, runs the build, and copies the resulting .jar.
test/mvn.sh: Runs tests inside a Maven container.
build/build.sh: Builds a Docker image using docker-compose.
deploy/deploy.sh: Uses scp to send scripts and credentials to remote-host and triggers remote deployment using ssh.

GitLab → Jenkins Auto Trigger Setup:

  1. Install the GitLab plugin in Jenkins.
  2. Go to Job → Configure.
  3. Check Build when a change is pushed to GitLab.
  4. Copy the provided Webhook URL.
  5. Generate a secret token in Jenkins.
  6. In GitLab: Go to Settings → Webhooks. Paste the webhook URL. Add the secret token. Click Save. Make a push to GitLab and... Jenkins triggers the build!

Conclusion:
This setup gives you a modular, scalable, and containerized CI/CD pipeline, ideal for teams working with Java, Docker, and GitLab. You can easily extend this pipeline with more stages like security scans, staging deployments, or Slack notifications.

Screenshots:

  1. Pushing the changes

  2. Changes pushed in gitlab

  3. Pipeline started and got completed.

  4. Build successful

Top comments (1)

Collapse
 
tentinqu profile image
Tentin Quarantino

This was a very good and informative read. Thanks and keep em coming!