A simple script to set up GitLab runner on AWS EC2 instance

janesoo

Jane

Posted on September 18, 2023

A simple script to set up GitLab runner on AWS EC2 instance

Pre-requisite:

  • AWS EC2 instance - in this case, I have RHEL9 as the base OS (could so work for CentOS)
  • GitLab's runner token: export RUNNER_TOKEN="your-runner-token" - you can get it from your GitLab's project

Steps to set up the runner:

  • Create a file: touch set-up-runner.sh (copy code below to set-up-runner.sh)
  • Then in the same path of your set-up-runner.sh, on your terminal, run set +x set-up-runner.sh: this is to set execution permission for your script
  • Finally, run ./set-up-runner.sh
#!/bin/bash

set -euxo pipefail

#Download and install gitlab-runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash
sudo yum install -y gitlab-runner

#Add docker repository to RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum makecache

#Check for existing docker, podman before installing docker
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine \
                  podman \
                  runc

#Install and enable docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo systemctl enable --now docker

#Register gitlab runner
sudo gitlab-runner register \
    --non-interactive \
    --url "https://gitlab.com/" \
    --registration-token $RUNNER_TOKEN \
    --description "gitlab-runner-ec2" \
    --executor "docker" \
    --docker-image alpine:latest \
     --tag-list "container-builds"

#Start service
sudo gitlab-runner start
sudo gitlab-runner verify
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
janesoo
Jane

Posted on September 18, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related