Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CLI command to register runner tokens #23762

Merged
merged 20 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions cmd/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package cmd

import (
"context"
"errors"
"fmt"
"strings"

actions_model "code.gitea.io/gitea/models/actions"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/util"

"github.com/urfave/cli"
)

// Cmdembedded represents the available extract sub-command.
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
var (
CmdActions = cli.Command{
Name: "actions",
Usage: "",
Description: "Tasks for managing Gitea Actions",
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Subcommands: []cli.Command{
subcmdActionsGenRunnerToken,
},
}

subcmdActionsGenRunnerToken = cli.Command{
Name: "generate-runner-token",
Usage: "Generate a new token for a runner to use to register with the server",
Action: runGenerateActionsRunnerToken,
Aliases: []string{"grt"},
Flags: []cli.Flag{
cli.StringFlag{
Name: "scope, s",
Value: "",
Usage: "{owner}[/{repo}]",
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
)

func runGenerateActionsRunnerToken(c *cli.Context) error {
ctx, cancel := installSignals()
defer cancel()

if err := initDB(ctx); err != nil {
return err
}

scope := c.String("scope")

owner, repo, err := parseScope(ctx, scope)
if err != nil {
return err
}

token, err := actions_model.GetUnactivatedRunnerToken(ctx, owner, repo)
if errors.Is(err, util.ErrNotExist) {
token, err = actions_model.NewRunnerToken(ctx, owner, repo)
if err != nil {
return fmt.Errorf("CreateRunnerToken: %s", err)
}
} else if err != nil {
return fmt.Errorf("GetUnactivatedRunnerToken: %s", err)
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
}

fmt.Printf("%s", token.Token)

return nil
}

func parseScope(ctx context.Context, scope string) (ownerID, repoID int64, err error) {
ownerID = 0
repoID = 0
if scope == "" {
return ownerID, repoID, nil
}

ownerName, repoName, found := strings.Cut(scope, "/")

u, err := user_model.GetUserByName(ctx, ownerName)
if err != nil {
return ownerID, repoID, err
}

if !found {
return u.ID, repoID, nil
}

r, err := repo_model.GetRepositoryByName(u.ID, repoName)
if err != nil {
return ownerID, repoID, err
}
repoID = r.ID
return ownerID, repoID, nil
}
25 changes: 25 additions & 0 deletions docs/content/doc/administration/command-line.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,28 @@ Restore-repo restore repository data from disk dir:
- `--owner_name lunny`: Restore destination owner name
- `--repo_name tango`: Restore destination repository name
- `--units <units>`: Which items will be restored, one or more units should be separated as comma. wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.

### actions generate-runner-token

Generate a new token for a runner to use to register with the server

- Options:
- `--socpe {owner}[/{repo}]`, `-s {owner}[/{repo}]`: To limit the scope of the runner, no scope means the runner can be used for all repos, but you can also limit it to a specific repo or owner
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved

To register a global runner:

```
gitea actions generate-runner-token
```

To register a runner for a specific organization, in this case `org`:

```
gitea actions generate-runner-token -s org
```

To register a runner for a specific repo, in this case `username/test-repo`:

```
gitea actions generate-runner-token -s username/test-repo
```
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdDocs,
cmd.CmdDumpRepository,
cmd.CmdRestoreRepository,
cmd.CmdActions,
}
// Now adjust these commands to add our global configuration options

Expand Down