Gitlab CI/CD

Fri, May 24, 2024 - 2 min read

Install / Uninstall gitlab-runner

# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner

# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

# Uninstall
# Stop and remove the service
sudo gitlab-runner stop
sudo gitlab-runner uninstall
sudo systemctl daemon-reload

# Remove gitlab-runner files and config
sudo rm -rf /usr/local/bin/gitlab-runner
sudo userdel gitlab-runner
sudo rm -rf /home/gitlab-runner/

or

# Install
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

sudo apt-get install gitlab-runner

systemctl status gitlab-runner

# Uninstall
sudo apt-get remove gitlab-runner

Command

Create runner for project

  1. Go to your GitLab project and navigate to Settings > CI/CD.

  2. Expand the Runners section.

Under Project runners, click New project runner.

After created runner, you will see the runner token for that project.

  1. This token is what you’ll use to register a Runner with the specific project.

Register runner

# Register runner
gitlab-runner register -n -u https://gitlab.com -t <token> --executor shell

# Unregister runner
gitlab-runner unregister -t <token>

# View gitlab runner service status
sudo gitlab-runner status

# View registered runner
gitlab-runner list

# Run registered runner
gitlab-runner run

# Verify and delete unused runner
gitlab-runner verify --delete

# Remove all runner
gitlab-runner unregister --all-runners

# create deployer user and add to docker group, so that gitlab-runner can run docker commands
sudo adduser deployer

sudo usermod -aG docker deployer

su deployer

ssh-keygen -t rsa -b 4096 -C "deployer"

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

cat ~/.ssh/id_rsa # -> copy to Project > Settings > CI / CD > Variables