GitLab Runner supports multiple executors, with Shell and Docker being the most widely used for CI/CD pipelines. The choice depends on system constraints, security policies, and scalability requirements.
Executors Overview
Shell Executor
- Runs jobs directly on the host machine
- Requires manual installation of dependencies
- Faster execution (no container overhead)
- Limited isolation → potential dependency conflicts
Best for:
- Dedicated servers
- Controlled environments
Docker Executor
- Runs jobs inside containers
- No manual dependency setup required
- Provides isolated, reproducible builds
- Slight startup overhead
Best for:
- Team environments
- Scalable CI/CD pipelines
- Microservices architecture
Java CI/CD with Shell Executor
Software Requirements
Install the following:
- Git : version control
- JDK (e.g., OpenJDK 8+) : compile Java code
- Apache Ant : build automation
Verify Installation
which git
java -version
ant -versionGitLab Runner Setup
1. Download Binary
sudo curl -L --output /usr/local/bin/gitlab-runner \
https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd642. Grant Execute Permission
sudo chmod +x /usr/local/bin/gitlab-runner3. Create Runner User
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash4. Install as Service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner5. Start Runner
sudo gitlab-runner startRegister Runner
sudo gitlab-runner registerProvide:
- GitLab instance URL (e.g., http://gitlab.example.com)
- Project registration token
(GitLab → Project → Settings → CI/CD → Runners) - Runner description (any name)
- Tags (optional but recommended)
- Executor: shell
Shell Executor: .gitlab-ci.yml
stages:
- build
- execute
build:
stage: build
script:
- ant -f build.xml
artifacts:
paths:
- abc.jar
execute:
stage: execute
script:
- cd scripts
- chmod +x run.sh
- ./run.sh
Java CI/CD with Docker Executor
- Use prebuilt images instead of installing packages every run
- Prefer
openjdkimages over generic OS images
Docker Executor: .gitlab-ci.yml
image: openjdk:8
stages:
- build
- execute
before_script:
- apt-get update && apt-get install -y ant
build:
stage: build
script:
- ant -f build.xml
artifacts:
paths:
- abc.jar
execute:
stage: execute
script:
- cd scripts
- chmod +x run.sh
- ./run.sh
Add Caching
cache:
paths:
- .ant/
Use Runner Tags
tags:
- shell
Common GitLab Runner Commands
| Command | Purpose |
|---|---|
| sudo gitlab-runner register | Register runner |
| sudo gitlab-runner start | Start runner |
| sudo gitlab-runner stop | Stop runner |
| sudo gitlab-runner status | Check status |
| sudo gitlab-runner restart | Restart service |
| sudo gitlab-runner unregister --all-runners | Remove all runners |
| sudo gitlab-runner --help | List commands |