Implementation of CI/CD in Java application (Linux) Using Shell and Docker Executor on GitLab

Last Updated : 8 Apr, 2026

GitLab Runner supports multiple executors for CI/CD, with Shell and Docker being the most commonly used. These executors are selected based on system requirements and available resources. In Java applications on Linux, CI/CD pipelines can be implemented using Bash scripts for build and testing.

  • Shell Executor: Runs jobs directly on the local machine where GitLab Runner is installed. It requires manually installing all necessary software on the system.
  • Docker Executor: Runs jobs inside containers using Docker images, eliminating the need for manual setup. However, it may be restricted in some organizations due to security policies.

Implementation of Java on Shell Executor

Software Requirements

  • Git: Required to manage and push code to GitLab. It is a version control system used to track changes in files.
  • JDK (Java Development Kit): Required to compile Java code and generate JAR files. Install a compatible version (e.g., OpenJDK 8).
  • Apache Ant: A build automation tool used to compile the project and generate the JAR file using build.xml.

Path Configuration

After installing the required software, ensure their paths are correctly set:

export GIT=/usr/bin/git
export JAVA=/usr/bin/java
export ANT=/usr/bin/ant

You can verify installation using:

which git
which java
which ant

GitLab Runner Set Up

Step 1. Download GitLab Runner on Linux Machine 

sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

Step 2. Give it permissions to execute using the following command:

sudo chmod +x /usr/local/bin/gitlab-runner

Step 3. Create a GitLab CI use the following command:

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

Step 4. Install and run as a service using the following command:

sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner

Step Start GitLab Runner using the following command:

sudo gitlab-runner start

Step 6. Stop the GitLab Runner before registering the repository using the following command:

sudo gitlab-runner stop

Step 7. Once GitLab Runner is successfully stopped enter the following command in the terminal for repository registration.

sudo gitlab-runner register

Step 8. When you do repository registration with GitLab Runner, the below questions have to answer.

  • Enter your GitLab instance URL: It can be different with each organization and the format will be like http://gitlab.example.com     
  • Path: Go to GitLab Account -> Select repository which you want to register with runner -> Settings -> CI/CD -> Expand Runner
  • Enter the gitlab-ci token for this runner: It will be a unique token of each project which will need while registration and it can be found      
  • Path: Go to GitLab Account -> Select repository which you want to register with runner -> Settings -> CI/CD -> Expand Runner
  • Enter the gitlab-ci description for this runner: Put Runner name(any name), which will help you to remember that which runner' is running
  • Enter the gitlab-ci tags for this runner: It is optional if you want to start GitLab runner when a specific tag is available in yml file.
  • Enter the executor: There will be a list of several executors, and type shell(as GitLab Runner will run our system)

Step 9. After successful registration, start the GitLab Runner using the following command

sudo gitlab-runner start

To verify that GitLab Runner has registered the respective repository and the runner has been started. Go to GitLab Account -> Select repository which you want to register with runner -> Settings -> CI/CD -> Expand Runner, There will be a green color circle will be available, and displaying message will be Runners activated for this project.

Note: If the circle is gray, it means the runner has not started and starts again.

Linux GitLab Runner Commands

CommandDescription
sudo gitlab-runner registerRegister the project with GitLab Runner 
sudo gitlab-runner registerStart the runner 
sudo gitlab-runner stopStop the runner
sudo gitlab-runner statusTo know the status of gitlab-runner 
sudo gitlab-runner unregister --name  test-runner  Unregister the Runner of a project and replace the test-runner with your runner name and this name can be found inside the config.toml file (where your gitlab-runner ) available.
sudo gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3nRemove Runner by URL and token 
sudo gitlab-runner unregister --all-runnersUnregister All Runners
sudo gitlab-runner restartThis command stops and then starts the GitLab Runner service
sudo gitlab-runner uninstallThis command stops and uninstalls the GitLab Runner from being run as a service
sudo gitlab-runner execTo see a list of available executors, run
sudo gitlab-runner --helpCheck a recent list of commands by executing
sudo gitlab-runner run --helpCan see the name of the environment variable
sudo gitlab-runner --debugTo run a command in debug mode
sudo gitlab-runner exec shellTo see a list of all available options for the shell executor, run

  .gitlab-ci.yml_shell Executor: Below is the content of .gitlab-ci.yml on shell executor mode. However, change it if needed based on requirements.

stages:
 - build
 - execute

build:
 stage: build
 script:  
   - ant -f build.xml
 
 artifacts:
   paths:
   - abc.jar

execute:
 stage: execute
 script:  
  - pwd  
  - cd scripts
  - chmod -R 777 *
  - pwd  
  - ./run.sh

Implementation of Java on Docker Executor

There is no need to install any software manually, everything will be taken from the docker container. However, you can install the required software inputting the name in yml file and also can export the path. To run the GitLab runner on docker executor mode, Go to GitLab Runner Set-Up(above), and select docker instead of shell.

Example: .gitlab-ci.yml_ Docker Executor:  

image: ubuntu:latest

stages:
 - build
 - execute
 
before_script:
 - echo "Before script section"
 - apt-get update && apt-get -y install openjdk-8-jdk  && apt-get -y install ant
 
 
build:
 stage: build
 script:  
   - ant -f build.xml
 
 artifacts:
   paths:
   - abc.jar

execute:
 stage: execute
 script:  
  - pwd  
  - cd scripts
  - chmod -R 777 *
  - pwd  
  - ./runtest.sh
Comment