Terraform-aws-github-runner: Automate the creation of an offline runner

Created on 6 Feb 2021  ·  11Comments  ·  Source: philips-labs/terraform-aws-github-runner

The current approach requires we alwsys have an offline runner regirstered. Those offline runners are removed by github eacht 60 days. Therefore it would be convient to automate the process to keep 1 offline runner so we can scale back to 0.

Potential solution direction

Register via ED2 instance

Use the same mechanism we use to spin up runners with an extra lambda that only execute the user data till the config step. And also ensure the ec2 instance is take down

Reverse egineer github config process

Create lamda that is using github http calls based on reverse engineering, see https://github.com/actions/runner/issues/558

Run the config in a lambda

Create a lambda that can exectue the config via a lambda layer

enhancement help wanted

Most helpful comment

@npalm I've figured out most of the logic, urls, etc involved with registering a runner. I plan on making a python module to handle this using python 3x and requests. This will probably be this weekend when I'm off work.

If you would prefer to implement it yourself, I can share my notes I've gathered about the process before then.

I honestly think I want to spend some more time tracing the entire process, so it could actually pull and perform workflows as well, but the new runner registration was a must have for me.

Basically the process I have found to work:
Get runner token via api using pat or app creds
Post token to secretish endpoint, receive back json data with new secretish endpoint and a jwt
Use jwt to auth via auth bearer header, using newly revealed endpoint, can query existing "agents"(as they are called in the api), add a new one, or update an existing one.

There's obviously some more details involved, including creating an RSA key, and a bunch of headers. I haven't looked further than registration yet...

All 11 comments

@npalm I've figured out most of the logic, urls, etc involved with registering a runner. I plan on making a python module to handle this using python 3x and requests. This will probably be this weekend when I'm off work.

If you would prefer to implement it yourself, I can share my notes I've gathered about the process before then.

I honestly think I want to spend some more time tracing the entire process, so it could actually pull and perform workflows as well, but the new runner registration was a must have for me.

Basically the process I have found to work:
Get runner token via api using pat or app creds
Post token to secretish endpoint, receive back json data with new secretish endpoint and a jwt
Use jwt to auth via auth bearer header, using newly revealed endpoint, can query existing "agents"(as they are called in the api), add a new one, or update an existing one.

There's obviously some more details involved, including creating an RSA key, and a bunch of headers. I haven't looked further than registration yet...

@gertjanmaas I think you will like the comment above

@miked63017 let me know if you don't get to it. We are looking for this as well so I would have some time to work on it.

Edit: Not sure if you saw, but this is newly released for Python: https://github.blog/2020-12-18-learn-about-ghapi-a-new-third-party-python-client-for-the-github-api/

@npalm @mcaulifn here is a link, it's still pretty beta'ish and not well documented, but I guess we can say the same thing about the runners/actions API in general :-)

https://github.com/miked63017/pyghrunner

@mcaulifn in RE to the ghapi module it looks cool, but most of these calls are undocumented pieces of the api, and probably subject to change.

Overall looks like it should work. Are you planning on adding it to this repo?

@gertjanmaas any opinion?

@npalm @mcaulifn not sure if I have the context to add it here to this repo, I am personally working in a GKE operator to do similar but figured it could help others to share some simple code to integrate with other projects. Seems to be a fairly common request for this functionality. If you'd like me to take a crack at adding here via a PR I can maybe spend some time this weekend.

I quickly skimmed through the python code and it seems to confirm what I saw when I tried to reverse engineer it a while ago. Would be great if this could be implemented here. Getting tired of adding offline runners by hand :P

@gertjanmaas where I am running (the equivalent) of this code(in a private library), we just run the few methods periodically, or in response to an event, and overwrite the previous "virtual runner". We are basically just using it as a placeholder so jobs queue rather than fail because no runners with labels exist. Then we look at the jobs details, and spin up the appropriate runner, with appropriate labels as needed, and with the --once flag.

I still have plans of further investigation into creating a full custom runner, most likely written in python, that can then be embedded in other places. This just hasn't been high priority for me yet.

How about runner deregistration?

The offline runner basically needs to be recreated every 30 days, in order to never have 0 runners in the org.

This should be automated as well.

Was this page helpful?
0 / 5 - 0 ratings