GitLab Runners as a Service with Github Action
Tags: gitlabci, cicd, devops, githubactions
Published: 2021-04-01 14:52:43


**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](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions)

For more info about `GitLab-CI`, check the official [documentation](https://docs.gitlab.com/ee/ci/yaml/gitlab_ci_yaml.html)


---


## 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"
```

#### 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
```

#### 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>
```

#### How to create a new GitHub Access Token:

* Go to [https://github.com/settings/tokens/new](https://github.com/settings/tokens/new)

* Mark the item `workflow` and click in generate a token

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t41gibobma1uyfa7tnz.png)

#### How to get Registration Token:

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

* Copy the Registration Token

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5m0s0al49w76jiqjyjsq.png)

#### 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

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8kfcu8v4p37m12z1e1w.png)

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvncw3tiqqdle5lpk1yd.png)

### Step 4

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


---


## Example

### Video Demo

{% youtube jI5U1iMboOs %}

### Screenshots

<figure class="image">
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs0l7kh49uiyb7z71w4p.png" alt="Job start-crosscicd trigger Github Workflow"><figcaption>Job start-crosscicd trigger Github Workflow</figcaption>
</figure>
<figure class="image">
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d97dpb7iu5zabbrn1ab3.png" alt="Workflow triggered by Gitlab-CI job"><figcaption>Workflow triggered by Gitlab-CI job</figcaption>
</figure>
<figure class="image">
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2rzlaf1yxq3uzwyaa3f.png" alt="There is 17GB free by default"><figcaption>There is 17GB free by default</figcaption>
</figure>
<figure class="image">
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycjolujvyhkgsjutgwdd.png" alt="After Maximize we have 54GB free to use"><figcaption>After Maximize we have 54GB free to use</figcaption>
</figure>
<figure class="image">
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzcfrksmy46gss18m4db.png" alt="Register a Runner, Start it, and Unregister after the job in GitLab is completed"><figcaption>Register a Runner, Start it, and Unregister after the job in GitLab is completed</figcaption>
</figure>

### Code

* [Repo with example code](https://github.com/edersonbrilhante/gitlab-runner-service-example)

* [GitHub Action](https://github.com/edersonbrilhante/gitlab-runner-action)


---


## That’s it!

In case you have any questions, please leave a comment here or ping me on [🔗 LinkedIn](https://www.linkedin.com/in/edersonbrilhante).