Skip to main content

Automating GitHub Runner with Systemd

· 3 min read
Zephyr
Engineer

title

Cover Image: Automatically generated by GPT-4 after reading this article


In our collaborative work using GitHub, we often utilize private hosts for CI/CD tasks. While GitHub provides documentation on how to install runners, following those steps might lead to instances where the host is rebooted, but the runner fails to start again.

This issue often slips through the cracks until someone notices the CI/CD isn't responding, prompting the reminder to manually start the service.

This cycle of forgetfulness is disruptive!

Hence, automation is needed!

  • Documentation Illustration

    github_set_runner

Configuration Process

To automatically execute a task after the host boots up, systemd service can be employed.

  1. Create a new systemd service file:

    sudo vim /etc/systemd/system/actions-runner.service
  2. Paste the following content into the file:

    [Unit]
    Description=GitHub Action Runner
    After=network.target

    [Service]
    Type=simple
    User=your_username
    WorkingDirectory=/home/your_username/actions-runner
    ExecStart=/home/your_username/actions-runner/run.sh
    Restart=always
    RestartSec=5

    [Install]
    WantedBy=multi-user.target

    Note: Replace User and WorkingDirectory with your own username.

  3. Inform systemd to reload the service configurations:

    sudo systemctl daemon-reload
  4. Enable this service to start automatically during boot-up:

    sudo systemctl enable actions-runner.service
  5. Now you can manually start this service or test it by rebooting:

    sudo systemctl start actions-runner.service

    Using this method, the actions-runner will execute in the background upon booting up your host.

    If you wish to stop the service, you can use the following command:

    sudo systemctl stop actions-runner.service

    Note: Ensure run.sh has executable permissions.

Checking Status

When managing services with systemd, you can't directly "attach" to a service to view its output. However, you can check the service's logs to understand its operational status. To view the logs of the actions-runner service, you can use the following command:

sudo journalctl -u actions-runner.service -f

Explanation:

  • -u actions-runner.service: Only view logs for the service named actions-runner.
  • -f: This parameter allows journalctl to continuously track the latest logs, enabling you to see real-time output.

Furthermore, if you want to check the current status of the service, you can use:

sudo systemctl status actions-runner.service

This will display the current status of the actions-runner service, including whether it's running and recent log outputs:

action-service

Reconfiguration

If the original runner is lost, it's typically due to switching between public and private repositories, or if the runner hasn't been invoked for a long time. In any case, the original runner is lost, necessitating reconfiguration.

In such cases, navigate to the actions-runner directory, delete the .runner file, and then rerun:

./config.sh --url ... (replace with new token configuration)

The remaining process remains the same. After configuration, restart the service, and you're good to go.