Sunday, October 20, 2024

self-hosted GitHub runner into a service

 To convert your self-hosted GitHub runner into a service on a Docker-installed Ubuntu machine, you can create a systemd service to automatically start and manage the runner. This will ensure that the runner starts with the machine and can be monitored like any other service.

Here’s how you can set it up:

Steps:

  1. Download and configure the GitHub runner.
  2. Create a systemd service for the GitHub runner.
  3. Start and enable the service.

Step 1: Download and Configure the GitHub Runner

# Create a directory for the runner
mkdir actions-runner && cd actions-runner

# Download the latest runner package
curl -o actions-runner-linux-x64-2.308.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.308.0/actions-runner-linux-x64-2.308.0.tar.gz

# Extract the installer
tar xzf ./actions-runner-linux-x64-2.308.0.tar.gz

# Configure the runner (replace with your repository or organization details)
./config.sh --url https://github.com/your-org-or-user/your-repo --token YOUR_RUNNER_TOKEN

# Test the runner (optional, but recommended)
./run.sh

Replace your-org-or-user, your-repo, and YOUR_RUNNER_TOKEN with your actual GitHub repository details and token.

Step 2: Create a systemd Service for the Runner

Now that the runner is configured, we can create a systemd service to manage it.

  1. Create the service file: 
    sudo nano /etc/systemd/system/github-runner.service
    
  2. Add the following content to the file:
    [Unit]
    Description=GitHub Runner
    After=network.target
    
    [Service]
    ExecStart=/home/ubuntu/actions-runner/run.sh
    WorkingDirectory=/home/ubuntu/actions-runner
    User=ubuntu
    Restart=always
    RestartSec=5s
    
    [Install]
    WantedBy=multi-user.target
  • ExecStart: Points to the run.sh script of your GitHub runner.
  • WorkingDirectory: Directory where your GitHub runner is located.
  • User: User that runs the service, usually ubuntu or your user account.
  • Restart: Ensures the service is restarted if it crashes.

Make sure the paths (/home/ubuntu/actions-runner) match your runner's location.

Step 3: Start and Enable the Service

  • Reload the systemd daemon to acknowledge the new service: sudo systemctl daemon-reload
  • Start the GitHub runner service: sudo systemctl start github-runner
  • Enable the service to start on boot: sudo systemctl enable github-runner
  • Check the status of the service: sudo systemctl status github-runner

Verifying the Setup

  • The service should now be running, and the runner should show as online in your GitHub repository.
  • To stop, restart, or check logs for the service, you can use standard systemd commands:

  • sudo systemctl stop github-runner
    sudo systemctl restart github-runner
    sudo journalctl -u github-runner
    
  • Optional: Automatically Remove Runner on Machine Shutdown

    If you want to automatically remove the GitHub runner on shutdown to avoid keeping stale runners registered:

  • Edit the service file to add a ExecStop command:


    [Service]
    ExecStop=/home/ubuntu/actions-runner/config.sh remove --token YOUR_RUNNER_TOKEN
    

This ensures that the runner is unregistered from GitHub when the machine is shut down. Be sure to replace YOUR_RUNNER_TOKEN with the actual token.

Now you have your GitHub runner configured as a service on an Ubuntu machine, managed by systemd!

No comments:

Post a Comment