GitHub Self-Hosted Runner Setup on Ubuntu with systemctl Service
This guide provides steps to set up a GitHub self-hosted runner on an Ubuntu server and run it as a service using systemctl.
Prerequisites
- Ubuntu server with root or
sudoaccess. - GitHub repository or organization where the runner will be connected.
- A GitHub personal access token with the necessary permissions to add self-hosted runners.
Step 1: Download and Configure the GitHub Runner
-
Create a directory for the runner:
-
Download the GitHub runner binary:
Replace
REPO_OWNERandREPO_NAMEwith the owner and repository names, or use your organization name if you’re adding a runner to an organization. -
Extract the runner:
-
Configure the runner:
You can find the registration token in the GitHub repository or organization settings under Settings > Actions > Runners.
Step 2: Create the run.sh Script
-
Create
run.shto ensure the runner restarts automatically in case of failure. -
Add the following content to
run.sh: -
Make
run.shexecutable:
Step 3: Set Up systemctl Service
-
Create a new systemd service file for the runner:
-
Add the following configuration to the service file:
Replace
$USERwith your actual username or use$USERif applicable in this context.NOTE:Github runner cannot be run as root -
Reload the systemd daemon to recognize the new service:
-
Enable and start the service:
-
Verify the runner is active:
You should see an “active (running)” status if the service is correctly set up.
Step 4: Confirm Runner Connection in GitHub
Go to your GitHub repository or organization settings under Settings > Actions > Runners to check if the runner is connected and ready.Managing the GitHub Runner Service
- Start the service:
sudo systemctl start github-runner - Stop the service:
sudo systemctl stop github-runner - Restart the service:
sudo systemctl restart github-runner - Check service status:
sudo systemctl status github-runner
Troubleshooting
- Check the runner logs:
- Make sure your GitHub registration token is up to date.
Unregistering the Runner
If you need to unregister the runner, stop the service and remove it:-
Stop the service:
-
Remove the service:
-
Unregister the runner in GitHub:
-
Clean up by deleting the runner directory:
