Install and Register GitLab Runner

In this section, you will install and configure GitLab Runner on an Amazon EC2 instance. GitLab Runner acts as the compute worker agent that picks up CI/CD pipeline jobs from your GitLab repository and executes build, container packaging, and deployment scripts directly against AWS services.


Architectural Overview

GitLab CI/CD separates the orchestrator (GitLab server) from the execution agents (GitLab Runners).

In this lab, we configure a Shell Executor hosted on an Amazon EC2 build instance:

  • Dedicated Project Runners: Two distinct runner instances (fcaj-lab-runner-be for backend tasks and fcaj-lab-runner-fe for frontend tasks) allow pipeline jobs to be routed cleanly using Git tags.
  • Shell Execution Mode: Executes build scripts natively on the host where Docker and the AWS CLI v2 have direct access to AWS IAM Instance Profile credentials.

Step 1: Connect to the EC2

  1. Make sure you followed all steps in 5.1 and 5.2

Connect to EC2 instance

Step 2: Install GitLab Runner

  1. Create a dedicated tools directory on the EC2 instance:

    sudo nano /tools/setup.sh
    

    Create Config Directory

  2. Add the following installation script to /tools/setup.sh:

    #!/bin/bash
    # Update package list
    apt update -y
    
    # Install GitLab Runner
    curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
    apt install gitlab-runner
    
    # Check version
    gitlab-runner --version
    

    Install GitLab Runner

  3. Make the script executable and run the installation:

    sudo chmod +x /tools/setup.sh
    sudo /tools/setup.sh
    
  4. Verify that the GitLab Runner binary is installed and operational:

    gitlab-runner --version
    

    Check Version

  5. Verify that the background system service is active. Expected Status: active (running).

sudo systemctl status gitlab-runner --no-pager

Step 3: Create and Register Backend Runner (fcaj-lab-runner-be)

3.1 Create the Runner in GitLab UI

  1. In your forked GitLab repository, navigate to the left sidebar:

    • Go to Settings → CI/CD.
    • Scroll down to the Runners section and click Expand.

    Switch User

  2. Under Project runners, click Create project runner.

    Access Settings

  3. Configure the runner parameters:

    • Tags: Enter fcaj-lab-runner-be and press Enter.
    • Run untagged jobs: Check this box (ensures the runner can process jobs that do not explicitly specify a tag).
    • Click Create runner.

    Configure Runner

Tags are used in .gitlab-ci.yml files (via the tags keyword) to route specific build jobs to designated runners. For example, fcaj-lab-runner-be ensures backend tasks run exclusively on this runner.

3.2 Register the Runner on the EC2 Host

  1. After creating the runner in the UI, GitLab displays the registration command containing your unique authentication token

  2. Run the registration command on your EC2 instance:

    Provide the configuration values when prompted:

    • GitLab instance URL: Press Enter to accept https://gitlab.com (or type https://gitlab.com).
    • Registration token: Paste the runner token generated from the GitLab console
    • Name for the runner: fcaj-lab-runner-be
    • Enter an executor: shell Register Runner Register Runner
  3. Run the runner under the GitLab Runner service or verify the runner daemon in the background:

    # Switch to root and start runner in background
    sudo -i
    su - gitlab-runner
    nohup gitlab-runner run > start-runner-be.txt 2>&1 &
    
    # Check live runner log
    tail -n 20 start-runner-be.txt
    

    Runner Running


Step 4: Create and Register Frontend Runner (fcaj-lab-runner-fe)

To enable parallel execution and clean separation of concerns, create a second runner dedicated to frontend assets:

  1. Return to GitLab: Settings → CI/CD → Runners → Click Create project runner. Configure Runner

  2. Configure the frontend runner:

    • Tags: Enter fcaj-lab-runner-fe and press Enter.
    • Run untagged jobs: Check this box.
    • Click Create runner. Register Frontend Runner
  3. Copy the new token and register on the EC2 terminal: When prompted:

    • GitLab instance URL: https://gitlab.com
    • Token: Paste the runner token generated from the GitLab console
    • Name for the runner: fcaj-lab-runner-fe
    • Enter an executor: shell

    Start Frontend Runner


Verify Active Runners

  1. Verify via GitLab Web Console:
    • Go to Settings → CI/CD → Runners.
    • Under Assigned project runners, verify that both runners appear with a green online status indicator.
Runner NameExecutorExpected Status
fcaj-lab-runner-beshellOnline (Green indicator)
fcaj-lab-runner-feshellOnline (Green indicator)

Register Frontend Runner

If a runner shows an offline status (gray/black dot), restart the runner service with sudo systemctl restart gitlab-runner and refresh the page.