Self-hosted GitHub Actions runners using AWS CodeBuild
Tags: aws, githubactions, github, devops
Published: 2024-04-28 16:14:59

## Introduction

I have always wanted to have the most efficient setup for the self-hosted GitHub Actions runners.
I think [AWS recent announcement](https://aws.amazon.com/about-aws/whats-new/2024/04/aws-codebuild-managed-github-action-runners/) helps to approach this goal.

So I was keen to test it myself.

## Instructions

Open the CodeBuild console and create a new project with the name `aws-codebuild-github-action-self-hosted-runner`. This name will be also used in the GitHub actions workflow at the `runs-on` parameter. 

![Open the CodeBuild console and create a new project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbygo8gcduedy2w85s5x.png)

Select GitHub from the available Sources and select the default connect option `Connect using OAuth`.
You can find more details on GitHub OAuth [here](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps).


![Select GitHub from the available Sources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jtt9ta7v0hk5a39tfd1.png)


![Select the default connect option `Connect using OAuth`](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0yaifsxladv2618knx0.png)

Select the GitHub repository from the drop-down list.


![Select the GitHub repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xioua5zrzs764tpfv0ey.png)

Check the `Rebuild every time a code change is pushed to this repository` checkbox at the Primary source webhook events section.

Select `WORKFLOW_JOB_QUEUED` from the Event types.

![Select `WORKFLOW_JOB_QUEUED` from the Event types](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40vg1jdzmem629m4g671.png)

Leave the default values in the Environment section.


![Leave the default values in the Environment section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3owbfb1m40yqtxvrbhj.png)

Choose `Use a buildspec file` in the Buildspec section.


![Choose `Use a buildspec file` in the Buildspec section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tis945aw4tewe295537o.png)

And at the last step click on `Create build project`.


![Click on Create build project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnsnfuzkb8shnr0hhuiu.png)


After the project is created you can check if the required webhook exists in your repo. 
Please use the following URL after you replace the words written in capital letters with your repo's related values:

`https://github.com/USER-NAME/REPOSITORY-NAME/settings/hooks`


![Check if the required webhook exists in your repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyc3nolrv055mcp2zpnb.png)

The last step is to create a workflow in your repo and then you can start using CodeBuild as your self-hosted GitHub action runner as a just-in-time runner.


```
name: Hello World

on: [push]

jobs:
  codebuild-job:
    runs-on: codebuild-aws-codebuild-github-action-self-hosted-runner-${{ github.run_id }}-${{ github.run_attempt }}
    steps:
      - run: |
          echo "Hello World!"
```

You just need to add this GitHub Actions workflow yaml file to the following place in your repo and name the file as you like it. In my case it's codebuild.yaml:

`.github/workflows/codebuild.yml`

And when you commit and push this change the CodeBuild job just kicks off.


![CodeBuild job just kicks off](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jub8c3x1bvx4qoha5i1u.png)