How to add a self-hosted runner in GitHub?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we will discuss how to install, configure, and add a self-hosted runner in GitHub.
In GitHub Actions (CI/CD pipeline), you have an option to run your pipeline in GitHub hosted runner or Self-Hosted runner. A runner is a server which will execute your pipeline through GitHub Actions. You can add a runner in either repository level or organization/enterprise level.
How to add a self-hosted runner in GitHub?
Prerequisites:
- One GitHub repository with proper access
- One Linux instance with proper access
Step 1: Install GitHub self-hosted Runner in your system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
## -------- ## Linux OS ## -------- ## Create a new directory and get inside the directory mkdir actions-runner && cd actions-runner ## Download the latest GitHub runner package curl -o actions-runner-linux-x64-2.284.0.tar.gz \ -L https://github.com/actions/runner/releases/download/v2.284.0/\ actions-runner-linux-x64-2.284.0.tar.gz ## Extract the GitHub runner installer package tar xzf ./actions-runner-linux-x64-2.284.0.tar.gz ## Register the runner with your GitHub repository ./config.sh \ --url https://github.com/ --token ## Get the token from GitHub ## GitHub > Your Repository > Settings > Actions > Runner > New self-hosted runner > OS (Linux) > Configure # Authentication # √ Connected to GitHub # Runner Registration # Enter the name of the runner group to add this runner to: [press Enter for Default] # Enter the name of runner: [press Enter for ip-172-31-41-155] cloudaffaire # This runner will have the following labels: 'self-hosted', 'Linux', 'X64' # Enter any additional labels (ex. label-1,label-2): [press Enter to skip] shell,aws # √ Runner successfully added # √ Runner connection is good # Runner settings # Enter name of work folder: [press Enter for _work] # √ Settings Saved. ## Run GitHub self-hosted runner ./run.sh ## Or if you want to run the runner as service (recommended) ## Install the service sudo ./svc.sh install ## Start the service sudo ./svc.sh start ## Check the status of the service sudo ./svc.sh status ## ------ ## MAC OS ## ------ ## Create a new directory and get inside the directory mkdir actions-runner && cd actions-runner ## Download the latest GitHub runner package curl -o actions-runner-osx-x64-2.284.0.tar.gz \ -L https://github.com/actions/runner/releases/download/v2.284.0/\ actions-runner-osx-x64-2.284.0.tar.gz ## Extract the GitHub runner installer package tar xzf ./actions-runner-osx-x64-2.284.0.tar.gz ## Register the runner with your GitHub repository ./config.sh \ --url https://github.com/ --token ## Get the token from GitHub ## GitHub > Your Repository > Settings > Actions > Runner > New self-hosted runner > OS (MAC) > Configure ## Run GitHub self-hosted runner ./run.sh ## Or if you want to run the runner as service (recommended) ## Install the service ./svc.sh install ## Start the service ./svc.sh start ## Check the status of the service ./svc.sh status ## ---------- ## Windows OS ## ---------- ## Create a new directory and get inside the directory mkdir actions-runner; cd actions-runner ## Download the latest GitHub runner package Invoke-WebRequest -Uri https://github.com/actions/runner/releases/download/v2.284.0/actions-runner-win-x64-2.284.0.zip ` -OutFile actions-runner-win-x64-2.284.0.zip ## Extract the GitHub runner installer package Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD/actions-runner-win-x64-2.284.0.zip", "$PWD") ## Register the runner with your GitHub repository ./config.cmd \ --url https://github.com/ --token ## Get the token from GitHub ## GitHub > Your Repository > Settings > Actions > Runner > New self-hosted runner > OS (Windows) > Configure ## Run GitHub self-hosted runner ./run.cmd |
Step 2: Test your GitHub self-hosted runner.
Once you have configured and started your GitHub self-hosted runner, refresh the runner page in GitHub and you will be able to see your runner listed there.
Next, create a workflow that will use this self-hosted runner and test if your pipeline gets executed in this runner successfully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
## ---------------------------- ## Test your self-hosted runner ## ---------------------------- ## Clone your GitHub repository git clone git@github.com: cd ## Create your workflow file cat << EOF > .github/workflows/myworkflow.yaml name: GitHub Actions Demo on: [push] jobs: job1: name: job1 runs-on: self-hosted steps: - name: step1 run: echo "action 1" - name: step2 run: echo "action 2" EOF ## Add, commit and push the changes git add . git commit -m "workflow added" git push |
As soon as you push the code, your pipeline will get triggered.
You can see the workflow execution status under “Actions” in GitHub.
Hope you have enjoyed this article. To know more about GitHub, please refer below official documentation