Create a Self-Hosted GitHub Action runner
In the world of continuous integration and continuous deployment (CI/CD), automation is key. GitHub Actions provides a powerful platform to automate workflows directly from your repositories. While GitHub’s hosted runners are a convenient option, there are times when you might need more control over your environment. This is where self-hosted runners come into play. In this blog, we’ll walk you through the process of creating and managing a GitHub self-hosted runner.
Prerequisites
Set Up Server
Now we need to set up the server. I am using an EC2 instance for this. You can use any server you have. First, SSH into the server and run the following commands.
1 | sudo apt-get update |
Create a repository
For this tutorial, I am going installing the runners at the repository level. You can also install them at the organization level. First, create a new repository or use an existing one. In here I am using a new repository called github-self-hosted-runner
Now, let’s configure the runner in your specific repository.
Configure Server
On your server, execute the commands provided by GitHub. Here’s a generalized version:
1 | mkdir actions-runner && cd actions-runner |
1 | ./config.sh --url https://github.com/yourusername/yourrepository --token YOUR_PERSONAL_ACCESS_TOKEN |
Replace https://github.com/yourusername/yourrepository with the URL of your GitHub repository and YOUR_PERSONAL_ACCESS_TOKEN with your personal action token.
During the configuration process, you will be prompted to enter details such as:
- Runner Group: You can add the runner to the default group or you can create a new group as well.
- Runner Name: Provide a unique name for your runner. This helps you identify the runner within GitHub.
- Runner Work Folder: Specify a directory where the runner will store job-related data. You can use the default location or specify a custom path.
Examples:
1 | Enter the runner group: [Press Enter for default] |
Note: It is recommanded to add new labels because all the self hosted runners will have those default labels but in order to bifurgate the jobs you must need to add new labels. For this tutorial I gave demorunner label to my runner.
1 | ./run.sh |
Verify the Runner
Once you complete all the steps you can see your runner on UI but it seems our runner is in the offline state. It’s because it is not running any jobs.
Start the runner
If you need to start the runner manually, you can run the run.sh script and it will listen for the jobs. If you need to install the runner as the service you can run svc.sh script. To run the runner as a service, you can use the following command.
- Stop the current runner process (if it’s running):
1
./svc.sh stop
- Install the runner as a service:
1
./svc.sh install
- Start the runner service:
1
./svc.sh start
- Check the status of the runner service:
1
./svc.sh status
- Enable the service to start on boot:
1
sudo systemctl enable actions.runner.<repository_name>.<runner_name>.service
Using Runner in Workflows
Now you can use this runner in your workflows. You can specify the runner in your workflow file. Here is an example of how you can use the runner in your workflow file.
1 | name: CI |
Check logs
You can check the logs of the runner by running the following command.
1 | cd _diag/ |
Conclusion
In this article, we learned how to create a Self-Hosted GitHub Action Runner. You can find the all the related commands for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail.