Setting up CI/CD with Gitlab Runner

gitlab | gitlab-runner | gitlab docker

tested on debian/buster

assuming a dedicated system and shell executor

Requirements

grab some TOKEN from the gitlab server

SHARED: Admin area > Settings > CI/CD > Expand Runners
GROUP: Gitlab Groups > (choose group) > Settings / CI/CD > Expand Runners
SPECIFIC: Gitlab Projects > (choose project) > Settings / CI/CD > Expand Runners

you can also review all kinds of runners at once here

Admin area > Overview / Runners

make sure you’ve got good name resolution against the gitlab server e.g. here static one

vi /etc/hosts

INTERNAL-IP GITLAB-SERVER-FQDN

and check that it is reachable

nmap -sTV -p 443 GITLAB-SERVER-FQDN

Install

install and register a new runner

wget -O - "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | bash
apt update
apt install gitlab-runner rsync
systemctl status gitlab-runner # enabled

gitlab-runner register

instance: https://GITLAB-SERVER-FQDN/
(SHARED vs. GROUP vs. SPECIFIC TOKEN)
descr: runner1
tags: runner1
executor: shell

Setup

trigger a test-run from your workstation

git clone PROJECT-URL
cd PROJECT/
vi .gitlab-ci.yml

stages:
  - deploy

deploy-prod:
  stage: deploy
  script:
    - echo TEST OK
  only:
    - master
  tags:
    - runner1

git add .gitlab-ci.yml
git commit
git push

notice the gitlab-runner user got created

date
ls -lF /home/
su - gitlab-runner

ssh-keygen -t ed25519
cat .ssh/id_ed25519.pub

now allow that user to reach the target servers for deployment as e.g. user runner

some target host

useradd -m -s /bin/bash runner
cd /home/runner/
mkdir .ssh/
cat > .ssh/authorized_keys <<EOF
THE PUBLIC KEY FROM ABOVE

EOF
chown -R runner:runner .ssh/
chmod 700 .ssh/
chmod 600 .ssh/authorized_keys

which rsync
cd /var/www/
mkdir catch-all/
chown runner:runner catch-all/

record the target server host keys on the runner

back to the runner system

su - gitlab-runner

ssh TARGET-SERVER -l runner

Additional notes

this might be required for the CI/CD builds

on the runner system as root

    ver=8.1
    wget https://packages.sury.org/php/apt.gpg -O /etc/apt/trusted.gpg.d/php.gpg
    echo "deb https://packages.sury.org/php/ `lsb_release -sc` main" > /etc/apt/sources.list.d/php.list
    apt update && apt install php$ver-{redis,mongodb}
    systemctl stop apache2
    systemctl disable apache2

cd /root/
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    ls -lF /usr/local/bin/composer

as user

su - gitlab-runner

composer -v

Resources

Continuous integration https://en.wikipedia.org/wiki/Continuous_integration

Migrating from Jenkins https://docs.gitlab.com/ee/ci/migration/jenkins.html

GitLab CI/CD job token https://docs.gitlab.com/ee/ci/jobs/ci_job_token.html

install

Install GitLab Runner https://docs.gitlab.com/runner/install/

Registering runners https://docs.gitlab.com/runner/register/

Install GitLab Runner using the official GitLab repositories https://docs.gitlab.com/runner/install/linux-repository.html

kinds of runners

The scope of runners https://docs.gitlab.com/ee/ci/runners/runners_scope.html

ops

GitLab Runner commands https://docs.gitlab.com/runner/commands/

Un-register gitlab-runner on Ubuntu not working https://stackoverflow.com/questions/62977329/un-register-gitlab-runner-on-ubuntu-not-working

Unregister gitlab runner https://gist.github.com/zinovyev/25f7d6095ef4cb8bfc2213e54901f6a5

moar on tokens

GitLab Token overview https://docs.gitlab.com/ee/security/token_overview.html

alternatives

A Brief History of DevOps, Part III: Automated Testing and Continuous Integration https://circleci.com/blog/a-brief-history-of-devops-part-iii-automated-testing-and-continuous-integration/

Getting started with continuous integration for Nest.js APIs https://circleci.com/blog/getting-started-with-nestjs-and-automatic-testing/

composer

https://getcomposer.org/download/

https://tecadmin.net/how-to-install-and-use-php-composer-on-debian-11/

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-composer-on-ubuntu-20-04

https://www.hostinger.com/tutorials/how-to-install-composer


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5
Copyright © 2022 Pierre-Philipp Braun