6

I want to create a VM and setup as self-hosted runner during my workflow. Right now, what blocks me is the lack of API that gives me Runner Token. If this exists, I could create the instance and register it as a runner, being able to use it in the next job.

Does anyone now a workaround to get the runner token?

Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41

2 Answers2

6

Late Update

Looks like they have finally created the runner api. See the API specs here.

[POST] /repos/{owner}/{repo}/actions/runners/registration-token

They now also have example snippets on how to do this. See the other answer posted here for a complete example.


Previous Answer

For now you have to create instances manually using the guide found here.

According to a github staff, there is a plan to eventually add an api for generating the runner tokens, but didn't disclose a timeline of when this could happen.

An API for this is on the roadmap. I don't have a timeline to share at the moment. But we'll be posting to the Changelog when this is available.

And to clear up some of the confusion around PATs/runner tokens. The runner token provided via the UI is a temporary token that expires after 60 minutes. It only has the ability to register runners.

PATs are not able to register runners.

smac89
  • 39,374
  • 15
  • 132
  • 179
4

The API for creating the Registration Token is available already:

  • Here you can read how to create the one for the repository level
  • And here - for the organization level

Using the following JavaScript code, you can create the GitHub Registration Token inside of your GitHub Action:

const core = require('@actions/core');
const github = require('@actions/github');

async function getRegistrationToken() {
  const githubToken = core.getInput('github_token'); // the GitHub Secret Token provided as an input of your GitHub Action using ${{ secrets.GITHUB_TOKEN }}
  const octokit = github.getOctokit(githubToken);

  const response = await octokit.request('POST /repos/{owner}/{repo}/actions/runners/registration-token', {
    owner: github.context.repo.owner, // the value is taken from the environment variable GITHUB_REPOSITORY which is provided by the GitHub Action during runtime
    repo: github.context.repo.repo, // the value is taken from the environment variable GITHUB_REPOSITORY which is provided by the GitHub Action during runtime
  });

  return response.data.token;
}

module.exports = {
  getRegistrationToken,
};

Volodymyr Machula
  • 1,564
  • 1
  • 15
  • 20
  • 1
    BTW: If you're interested, I'm working on the GitHub Action for creating an on-demand AWS EC2 instance and register it as a self-hosted GitHub Actions runner. It's still in progress but will be ready soon: https://github.com/machulav/aws-github-runner. So feel free to follow. – Volodymyr Machula Dec 15 '20 at 12:45