Create MacOS gitlab-runner: Difference between revisions

From Jwiki
Created page with "Category:MacOS h1. MacOS Gitlab-runner h2. MacOS services (launchctl) h3. Description LaunchAgents: Invoked only when the user logs into a graphical session LaunchDaemons: Invoked when the system boots and runs outside of a specific user session. h3. Namespaces h4. System /Library/LaunchAgents: Per-user agents provided by the administrator. /Library/LaunchDaemons: System-wide daemons provided by the administrator. h4. Use..."
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:MacOS]]
[[Category:MacOS]]
[[Category:GitLab]]
== Installing GitLab Runner with `brew services` (Simplified Method) ==
This guide details a simplified method for installing and configuring a GitLab Runner on macOS by using Homebrew's built-in service management. This is often easier than manually managing `launchd` files.


h1. MacOS Gitlab-runner
=== 1. Install GitLab Runner ===
First, install the GitLab Runner package using Homebrew:
<syntaxhighlight lang="bash">
brew install gitlab-runner
</syntaxhighlight>


h2. MacOS services (launchctl)
=== 2. Note on Paths (Apple Silicon vs. Intel) ===
Installation paths depending on your Mac's architecture. The shell environment setup in a later step will handle this automatically by using `brew shellenv`, but it's good to be aware of the difference:


h3. Description
* '''Apple Silicon (M1/M2/M3):''' `/opt/`
* '''Intel:''' `/usr/local/`


LaunchAgents:              Invoked only when the user logs into a graphical session
=== 3. Create and Switch to the Runner User ===
LaunchDaemons:              Invoked when the system boots and runs outside of a specific user session.
For security and isolation, it is best practice to run the GitLab Runner under a dedicated user account. If you haven't created one, you can do so in '''System Settings > Users & Groups'''.


h3. Namespaces
Once the user (e.g., `runner`) exists, '''switch to it for all subsequent steps''':
 
<syntaxhighlight lang="bash">
h4. System
 
/Library/LaunchAgents:      Per-user agents provided by the administrator.
/Library/LaunchDaemons:      System-wide daemons provided by the administrator.
 
h4. User
 
~/Library/LaunchAgents:      Per-user agents provided by the user.
 
h3. Examples
 
h2. Gitlab-runner (homebrew)
 
<pre><code class="bash">
su runner
su runner
</code></pre>
</syntaxhighlight>
 
<pre><code class="bash">
nano ~/Library/LaunchAgents/homebrew.mxcl.gitlab-runner-custom.plist
</code></pre>


<pre><code class="xml">
=== 4. Configure the GitLab Runner ===
<?xml version="1.0" encoding="UTF-8"?>
As the `runner` user, configure the runner's behavior by editing its `config.toml` file.
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<syntaxhighlight lang="bash">
<plist version="1.0">
<dict>
        <key>KeepAlive</key>
        <true/>
        <key>Label</key>
        <string>homebrew.mxcl.gitlab-runner</string>
        <key>LegacyTimers</key>
        <true/>
        <key>LimitLoadToSessionType</key>
        <array>
                <string>Aqua</string>
                <string>Background</string>
                <string>LoginWindow</string>
                <string>StandardIO</string>
                <string>System</string>
        </array>
        <key>ProcessType</key>
        <string>Interactive</string>
        <key>ProgramArguments</key>
        <array>
                <string>/opt/homebrew/opt/gitlab-runner/bin/gitlab-runner</string>
                <string>run</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>WorkingDirectory</key>
        <string>/Users/runner</string>
        <key>StandardErrorPath</key>
        <string>/Users/runner/gitlab-runner.err.log</string>
        <key>StandardOutPath</key>
        <string>/Users/runner/gitlab-runner.out.log</string>
</dict>
</plist>
</code></pre>
 
<pre><code class="bash">
nano ~/.gitlab-runner/config.toml
nano ~/.gitlab-runner/config.toml
</code></pre>
</syntaxhighlight>


Gitlab-runner does not support zsh as shell!
Add the following configuration. '''Note:''' GitLab Runner does not support Zsh as a shell for its jobs, so you must explicitly set the shell to `bash`.
<pre><code class="toml">
<syntaxhighlight lang="toml">
concurrent = 3
concurrent = 3
check_interval = 30
check_interval = 30
[session_server]
[session_server]
   session_timeout = 1800
   session_timeout = 1800
[[runners]]
[[runners]]
   name = "Mac-mini-runner"
   name = "Mac-mini-runner"
Line 95: Line 48:
     [runners.cache.gcs]
     [runners.cache.gcs]
     [runners.cache.azure]
     [runners.cache.azure]
</code></pre>
</syntaxhighlight>
 
=== 5. Set Up the Shell Environment ===
This is the most critical step. For the runner's jobs to find tools and execute correctly, the `runner` user's shell environment must be configured properly.


We need to create .bashrc
==== Create and configure `.bashrc` ====
<pre><code class="bash">
<syntaxhighlight lang="bash">
nano ~/.bashrc
nano ~/.bashrc
</code></pre>
</syntaxhighlight>


<pre><code class="bash">
Add the necessary environment variables and path definitions.
<syntaxhighlight lang="bash">
### Brew ###
### Brew ###
## Silicon
# This command sets up Homebrew's environment, including the correct PATH.
eval $(/opt/homebrew/bin/brew shellenv)
eval $(/opt/homebrew/bin/brew shellenv)


Line 120: Line 77:
export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"
export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"


# Path
# Add any other required PATH exports here. The 'brew shellenv' command
export PATH=/Users/runner/.rbenv/shims:/Users/runner/Downloads/flutter/bin:/opt/homebrew/bin:/opt/homebrew/opt/ruby/bin:/opt/homebrew/lib/ruby/gems/3.2.0/bin:/Users/runner/.rbenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/Library/flutter/bin:/Library/flutter/.pub-cache/bin:/Users/runner/.pub-cache/bin:/Users/runner/Library/Android/sdk/bundle-tool/:/Users/runner/Library/Android/sdk/platform-tools/:/Users/runner/Library/Android/sdk/cmdline-tools/latest/bin/:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin
# should handle the primary Homebrew paths for your architecture.
 
export PATH="/Users/runner/Library/Android/sdk/platform-tools:${PATH}"
   
# FASTLANE
# FASTLANE
export FASTLANE_SESSION=masked
export FASTLANE_SESSION=masked
Line 130: Line 88:
export SPACESHIP_ONLY_ALLOW_INTERACTIVE_2FA=true
export SPACESHIP_ONLY_ALLOW_INTERACTIVE_2FA=true
export SUPPLY_UPLOAD_MAX_RETRIES=5
export SUPPLY_UPLOAD_MAX_RETRIES=5
</code></pre>
</syntaxhighlight>


We need to create .bash_profile too
==== Create `.bash_profile` to source `.bashrc` ====
<pre><code class="bash">
This ensures your `.bashrc` configuration is loaded for new shell sessions.
<syntaxhighlight lang="bash">
nano ~/.bash_profile
nano ~/.bash_profile
</code></pre>
</syntaxhighlight>
 
Add the following lines:
<pre><code class="bash">
<syntaxhighlight lang="bash">
#####
#####
# USE "~/.bashrc" for configuration!
# USE "~/.bashrc" for configuration!
#####
#####
### Import .bashrc ###
### Import .bashrc ###
if [ -f ~/.bashrc ]; then
if [ -f ~/.bashrc ]; then
     . ~/.bashrc
     . ~/.bashrc
fi
fi
</code></pre>
</syntaxhighlight>
 
=== 6. Start and Manage the Runner with `brew services` ===
'''Crucial Point:''' You must run these commands as the `runner` user. This ensures the service runs under the correct user account.
 
To start the service and register it to run automatically at login:
<syntaxhighlight lang="bash">
brew services start gitlab-runner
</syntaxhighlight>


Enable and run:
To stop the service:
<pre><code class="bash">
<syntaxhighlight lang="bash">
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.gitlab-runner-custom.plist
brew services stop gitlab-runner
</code></pre>
</syntaxhighlight>


Disable and stop:
To check the status of all your Homebrew services:
<pre><code class="bash">
<syntaxhighlight lang="bash">
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.gitlab-runner-custom.plist
brew services list
</code></pre>
</syntaxhighlight>

Latest revision as of 11:07, 29 August 2025

Installing GitLab Runner with `brew services` (Simplified Method)

This guide details a simplified method for installing and configuring a GitLab Runner on macOS by using Homebrew's built-in service management. This is often easier than manually managing `launchd` files.

1. Install GitLab Runner

First, install the GitLab Runner package using Homebrew:

brew install gitlab-runner

2. Note on Paths (Apple Silicon vs. Intel)

Installation paths depending on your Mac's architecture. The shell environment setup in a later step will handle this automatically by using `brew shellenv`, but it's good to be aware of the difference:

  • Apple Silicon (M1/M2/M3): `/opt/`
  • Intel: `/usr/local/`

3. Create and Switch to the Runner User

For security and isolation, it is best practice to run the GitLab Runner under a dedicated user account. If you haven't created one, you can do so in System Settings > Users & Groups.

Once the user (e.g., `runner`) exists, switch to it for all subsequent steps:

su runner

4. Configure the GitLab Runner

As the `runner` user, configure the runner's behavior by editing its `config.toml` file.

nano ~/.gitlab-runner/config.toml

Add the following configuration. Note: GitLab Runner does not support Zsh as a shell for its jobs, so you must explicitly set the shell to `bash`.

concurrent = 3
check_interval = 30
[session_server]
  session_timeout = 1800
[[runners]]
  name = "Mac-mini-runner"
  limit = 1
  url = "https://gitlab.com/"
  token = "masked"
  executor = "shell"
  shell="bash"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

5. Set Up the Shell Environment

This is the most critical step. For the runner's jobs to find tools and execute correctly, the `runner` user's shell environment must be configured properly.

Create and configure `.bashrc`

nano ~/.bashrc

Add the necessary environment variables and path definitions.

### Brew ###
# This command sets up Homebrew's environment, including the correct PATH.
eval $(/opt/homebrew/bin/brew shellenv)

### Ruby ###
eval "$(rbenv init -)"

### Extra environments ###
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

# Android
export ANDROID_HOME="/Users/runner/Library/Android/sdk"

# Java
export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"

# Add any other required PATH exports here. The 'brew shellenv' command
# should handle the primary Homebrew paths for your architecture.
export PATH="/Users/runner/Library/Android/sdk/platform-tools:${PATH}"
    
# FASTLANE
export FASTLANE_SESSION=masked
export FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD="masked"
export FASTLANE_USER="mobil@example.com"
export FASTLANE_PASSWORD="masked"
export SPACESHIP_ONLY_ALLOW_INTERACTIVE_2FA=true
export SUPPLY_UPLOAD_MAX_RETRIES=5

Create `.bash_profile` to source `.bashrc`

This ensures your `.bashrc` configuration is loaded for new shell sessions.

nano ~/.bash_profile

Add the following lines:

#####
# USE "~/.bashrc" for configuration!
#####
### Import .bashrc ###
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

6. Start and Manage the Runner with `brew services`

Crucial Point: You must run these commands as the `runner` user. This ensures the service runs under the correct user account.

To start the service and register it to run automatically at login:

brew services start gitlab-runner

To stop the service:

brew services stop gitlab-runner

To check the status of all your Homebrew services:

brew services list