GetRegistrationToken returns a registration token tied with the name of repository and runner.
(ctx context.Context, enterprise, org, repo, name string)
| 154 | |
| 155 | // GetRegistrationToken returns a registration token tied with the name of repository and runner. |
| 156 | func (c *Client) GetRegistrationToken(ctx context.Context, enterprise, org, repo, name string) (*github.RegistrationToken, error) { |
| 157 | c.mu.Lock() |
| 158 | defer c.mu.Unlock() |
| 159 | |
| 160 | key := getRegistrationKey(org, repo, enterprise) |
| 161 | rt, ok := c.regTokens[key] |
| 162 | |
| 163 | // We'd like to allow the runner just starting up to miss the expiration date by a bit. |
| 164 | // Note that this means that we're going to cache Creation Registraion Token API response longer than the |
| 165 | // recommended cache duration. |
| 166 | // |
| 167 | // https://docs.github.com/en/rest/reference/actions#create-a-registration-token-for-a-repository |
| 168 | // https://docs.github.com/en/rest/reference/actions#create-a-registration-token-for-an-organization |
| 169 | // https://docs.github.com/en/rest/reference/actions#create-a-registration-token-for-an-enterprise |
| 170 | // https://docs.github.com/en/rest/overview/resources-in-the-rest-api#conditional-requests |
| 171 | // |
| 172 | // This is currently set to 30 minutes as the result of the discussion took place at the following issue: |
| 173 | // https://github.com/actions/actions-runner-controller/issues/1295 |
| 174 | runnerStartupTimeout := 30 * time.Minute |
| 175 | |
| 176 | if ok && rt.GetExpiresAt().After(time.Now().Add(runnerStartupTimeout)) { |
| 177 | return rt, nil |
| 178 | } |
| 179 | |
| 180 | enterprise, owner, repo, err := getEnterpriseOrganizationAndRepo(enterprise, org, repo) |
| 181 | |
| 182 | if err != nil { |
| 183 | return rt, err |
| 184 | } |
| 185 | |
| 186 | rt, res, err := c.createRegistrationToken(ctx, enterprise, owner, repo) |
| 187 | |
| 188 | if err != nil { |
| 189 | return nil, fmt.Errorf("failed to create registration token: %v", err) |
| 190 | } |
| 191 | |
| 192 | if res.StatusCode != 201 { |
| 193 | return nil, fmt.Errorf("unexpected status: %d", res.StatusCode) |
| 194 | } |
| 195 | |
| 196 | c.regTokens[key] = rt |
| 197 | go func() { |
| 198 | c.cleanup() |
| 199 | }() |
| 200 | |
| 201 | return rt, nil |
| 202 | } |
| 203 | |
| 204 | // RemoveRunner removes a runner with specified runner ID from repository. |
| 205 | func (c *Client) RemoveRunner(ctx context.Context, enterprise, org, repo string, runnerID int64) error { |