DEV Community

loading...

GitLab Runners as a Service with Github Action

Ederson Brilhante
Senior Software Engineer with 11 years of professional experience, where most of my experience comes from working in large internet companies.
・3 min read

TL;DR; This article will show how to implement the action "Gitlab Runner Service Action" in a "GitHub Workflow" that is triggered by a "GitLab-CI job", and this way having temporary GitLab Runners hosted by GitHub.

For more info about GitHub workflow, check the official documentation

For more info about GitLab-CI, check the official documentation


Steps

Step 1

Create a new GitHub repository with the following GitHub Workflow. File location: .github/workflows/gitlab-runner.yaml

name: Gitlab Runner Service
on: [repository_dispatch]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Maximize Build Space
        uses: easimon/maximize-build-space@master
        with:
          root-reserve-mb: 512
          swap-size-mb: 1024
          remove-dotnet: 'true'
          remove-android: 'true'
          remove-haskell: 'true'

      - name: Gitlab Runner
        uses: edersonbrilhante/gitlab-runner-action@main
        with:
          registration-token: "${{ github.event.client_payload.registration_token }}"
          docker-image: "docker:19.03.12"
          name: ${{ github.run_id }}
          tag-list: "crosscicd"
Enter fullscreen mode Exit fullscreen mode

What does this workflow do?

This workflow will run just when the event repository_dispatch is triggered. The first step will be to increase the free space removing useless packages for our GitLab runner. And the second step will run the action that registers a new GitLab Runner with a tag crosscicd, so start it and unregister it after a GitLab-CI job is completed with success or failure.

Step 2

Create a new GitLab repository with the following GitLab-CI config. File location: .gitlab-ci.yml

start-crosscicd:
  image: alpine
  before_script:
    - apk add --update curl && rm -rf /var/cache/apk/*
  script: |
    curl -H "Authorization: token ${GITHUB_TOKEN}" \
    -H 'Accept: application/vnd.github.everest-preview+json' \
    "https://api.github.com/repos/${GITHUB_REPO}/dispatches" \
    -d '{"event_type": "gitlab_trigger_'${CI_PIPELINE_ID}'", "client_payload": {"registration_token": "'${GITLAB_REGISTRATION_TOKEN}'"}}'

github:
  image: docker:latest
  services:
    - name: docker:dind
      alias: thedockerhost
  variables:
    DOCKER_HOST: tcp://thedockerhost:2375/
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  script:
    - df -h
    - docker run --privileged ubuntu df -h
  tags:
    - crosscicd
Enter fullscreen mode Exit fullscreen mode

What does this gitlab-ci?

The job start-crosscicd will trigger the GitHub workflow, creating the GitLab runner with the tag crosscicd. And the job GitHub will wait for a runner with a tag crosscicd.

Step 3

Set the EnvVars in the new GitLab Repo

    GITHUB_REPO:<username>/<github-repo>
    GITHUB_TOKEN:<GitHub Access Token>
    GITLAB_REGISTRATION_TOKEN:<GitLab Registration Token>
Enter fullscreen mode Exit fullscreen mode

How to create a new GitHub Access Token:

How to get Registration Token:

  • Go to https://gitlab.com/<username>/<repo>/-/settings/ci_cd and click and expand Runners

  • Copy the Registration Token

Where to store the EnvVars?

  • Go to https://gitlab.com/<username>/<repo>/-/settings/ci_cd and click and expand Variables

  • Click in Add Variable and save it for each EnvVar

Step 4

Now your pipeline is ready to run the GitLab Runner in GitHub trigger by Gitlab-CI Job :)


Example

Video Demo

Screenshots

Job start-crosscicd trigger Github Workflow
Job start-crosscicd trigger Github Workflow

Workflow triggered by Gitlab-CI job
Workflow triggered by Gitlab-CI job

There is 17GB free by default
There is 17GB free by default

After Maximize we have 54GB free to use
After Maximize we have 54GB free to use

Register a Runner, Start it, and Unregister after the job in GitLab is completed
Register a Runner, Start it, and Unregister after the job in GitLab is completed

Code


That’s it!

In case you have any questions, please leave a comment here or ping me on πŸ”— LinkedIn.

Discussion (0)