Git setup

git config --global user.name "Your Name"
git config --global user.email "name@domain.example"
git config --global init.defaultBranch main
 

Disable Host Key Check in:
Manage Jenkins >> Security

Here is the Java program this demo builds. It generates and prints 100 random numbers to the console:

RandomNumberGenerator.java

import java.util.Random;
 
public class RandomNumberGenerator {
    public static void main(String[] args) {
        // Create an instance of Random
        Random random = new Random();
 
        // Generate and print 10 random numbers
        System.out.println("Generating 10 random numbers:");
        for (int i = 1; i <= 10; i++) {
            // Generate a random number (can customize range if needed)
            int randomNumber = random.nextInt();
            System.out.println("Number " + i + ": " + randomNumber);
        }
    }
}

Create a freestyle project

Now create a Jenkins project of type Freestyle project that pulls the code from GitHub and runs a simple build:

  1. Add git repository — In the job configuration, set Source Code Management → Git and paste the repository’s SSH clone URL (e.g. git@github.com:user/repo.git). Add the private SSH key in the Jenkins Credentials so it can authenticate with GitHub.
  2. Create a shell script — Add a Build Step of type Execute shell that compiles the Java source into bytecode:
    javac RandomNumberGenerator.java
  3. Run the compiled program — Add a second shell build step that executes the compiled class, which prints the 100 random numbers:
    java RandomNumberGenerator

When you trigger the build, Jenkins clones the repository, compiles the code, and runs it — confirming the full flow works end to end.

Use ant builder

Start by adding antinstallation on on tools
build.xml

<project>
 
    <target name="clean">
        <delete dir="build"/>
    </target>
 
    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>
 
    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/RandomNumberGenerator.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="RandomNumberGenerator"/>
            </manifest>
        </jar>
    </target>
 
    <target name="run">
        <java jar="build/jar/RandomNumberGenerator.jar" fork="true"/>
    </target>
 
</project>

Create ant jobs

ant compile
ant jar
ant run

Add simple tests

Mere compilation isn’t enough to prove the code behaves correctly, so the next step makes the program testable and adds unit tests that Jenkins runs on every build.

Change code to allow tests — Refactor RandomNumberGenerator by extracting the random-number generation into a separate, reusable method. Because this method takes a count parameter, tests can call it directly and verify its behaviour against a known input:

import java.util.Random;
 
public class RandomNumberGenerator {
    public static void main(String[] args) {
        // Create an instance of Random
        Random random = new Random();
 
        // Generate and print 100 random numbers
        System.out.println("Generating 10 random numbers:");
        for (int i = 1; i <= 10; i++) {
            // Generate a random number (can customize range if needed)
            int randomNumber = random.nextInt();
            System.out.println("Number " + i + ": " + randomNumber);
        }
    }
 
    // Method to generate an array of random numbers
    public static int[] generateRandomNumbers(int count) {
        Random random = new Random();
        int[] numbers = new int[count];
        for (int i = 0; i < count; i++) {
            numbers[i] = random.nextInt();
        }
        return numbers;
    }
}

Add some unit tests — Create a test class that calls generateRandomNumbers(...) and checks the result. Here it asserts that the returned array contains exactly as many numbers as requested, so a failure fails the Jenkins build:

public class RandomNumberGeneratorTest {
    public static void main(String[] args) {
        testRandomNumberArrayLength();
    }
 
    public static void testRandomNumberArrayLength() {
        int count = 10;
        int[] numbers = RandomNumberGenerator.generateRandomNumbers(count);
 
        // Using assert to validate the test
        assert numbers.length == count : "Test failed: Array length is incorrect.";
        System.out.println("Test passed: Array length is correct.");
    }
}

To have Jenkins run these tests automatically, add a build step that compiles and runs the test class:

javac RandomNumberGeneratorTest.java
java -ea RandomNumberGeneratorTest

The -ea flag enables Java assertions. Without it, the assert statement is silently ignored and the test would always pass, even when broken.

Using pipelines

pipeline {
    agent { label 'linux' }  // Force the whole pipeline to 'my-agent' node
    
    stages {
        stage('Build') {
            steps {
                echo 'Building on the specified agent'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing on the specified agent'
            }
        }
    }
}

Trigger by script

  1. Create a user token
  2. Configure Project to build by remote
  3. Add token
  4. Call a script:
$ curl -X POST -u "admin:11c8426374bb55519876695cf33ea72aec" http://18.175.56.58:8080/job/random-project-pipeline/build?token=4e85cb2eb8aee87b41656c0e6c60c0ce

Where:

  • 11c8426374bb55519876695cf33ea72aec -> User token
  • 4e85cb2eb8aee87b41656c0e6c60c0ce -> Shell Generated token date | md5sum

Trigger by git hooks

random-project.git/hooks/post-update

#!/usr/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

echo "Trigger build"
# exec git update-server-info
echo "Send trigger"
exec curl -X POST -u "admin:11c8426374bb55519876695cf33ea72aec" "http://18.175.56.58:8080/job/random-project-pipeline/build?token=4e85cb2eb8aee87b41656c0e6c60c0ce"
echo "Done!"