Skip to content

Commit b1ae30d

Browse files
ChristopherHXlunny
andcommitted
ephemeral act runner (#649)
Works for both interactive and non-interactive registration mode. A further enhancement would be jitconfig support of the daemon command, because after some changes in Gitea Actions the registration token became reusable. removing runner and fail seems not possible at the current api level Part of go-gitea/gitea#33570 Co-authored-by: Lunny Xiao <[email protected]> Reviewed-on: https://gitea.com/gitea/act_runner/pulls/649 Reviewed-by: Zettat123 <[email protected]> Reviewed-by: Lunny Xiao <[email protected]> Co-authored-by: Christopher Homberger <[email protected]> Co-committed-by: Christopher Homberger <[email protected]>
1 parent 0d68726 commit b1ae30d

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

internal/app/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func Execute(ctx context.Context) {
3939
registerCmd.Flags().StringVar(&regArgs.Token, "token", "", "Runner token")
4040
registerCmd.Flags().StringVar(&regArgs.RunnerName, "name", "", "Runner name")
4141
registerCmd.Flags().StringVar(&regArgs.Labels, "labels", "", "Runner tags, comma separated")
42+
registerCmd.Flags().BoolVar(&regArgs.Ephemeral, "ephemeral", false, "Configure the runner to be ephemeral and only ever be able to pick a single job (stricter than --once)")
4243
rootCmd.AddCommand(registerCmd)
4344

4445
// ./act_runner daemon

internal/app/cmd/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func runDaemon(ctx context.Context, daemArgs *daemonArgs, configFile *string) fu
122122

123123
poller := poll.New(cfg, cli, runner)
124124

125-
if daemArgs.Once {
125+
if daemArgs.Once || reg.Ephemeral {
126126
done := make(chan struct{})
127127
go func() {
128128
defer close(done)

internal/app/cmd/register.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type registerArgs struct {
7575
Token string
7676
RunnerName string
7777
Labels string
78+
Ephemeral bool
7879
}
7980

8081
type registerStage int8
@@ -101,6 +102,7 @@ type registerInputs struct {
101102
Token string
102103
RunnerName string
103104
Labels []string
105+
Ephemeral bool
104106
}
105107

106108
func (r *registerInputs) validate() error {
@@ -258,6 +260,7 @@ func registerNoInteractive(ctx context.Context, configFile string, regArgs *regi
258260
Token: regArgs.Token,
259261
RunnerName: regArgs.RunnerName,
260262
Labels: defaultLabels,
263+
Ephemeral: regArgs.Ephemeral,
261264
}
262265
regArgs.Labels = strings.TrimSpace(regArgs.Labels)
263266
// command line flag.
@@ -321,10 +324,11 @@ func doRegister(ctx context.Context, cfg *config.Config, inputs *registerInputs)
321324
}
322325

323326
reg := &config.Registration{
324-
Name: inputs.RunnerName,
325-
Token: inputs.Token,
326-
Address: inputs.InstanceAddr,
327-
Labels: inputs.Labels,
327+
Name: inputs.RunnerName,
328+
Token: inputs.Token,
329+
Address: inputs.InstanceAddr,
330+
Labels: inputs.Labels,
331+
Ephemeral: inputs.Ephemeral,
328332
}
329333

330334
ls := make([]string, len(reg.Labels))
@@ -339,6 +343,7 @@ func doRegister(ctx context.Context, cfg *config.Config, inputs *registerInputs)
339343
Version: ver.Version(),
340344
AgentLabels: ls, // Could be removed after Gitea 1.20
341345
Labels: ls,
346+
Ephemeral: reg.Ephemeral,
342347
}))
343348
if err != nil {
344349
log.WithError(err).Error("poller: cannot register new runner")
@@ -350,6 +355,11 @@ func doRegister(ctx context.Context, cfg *config.Config, inputs *registerInputs)
350355
reg.Name = resp.Msg.Runner.Name
351356
reg.Token = resp.Msg.Runner.Token
352357

358+
if inputs.Ephemeral != resp.Msg.Runner.Ephemeral {
359+
// TODO we cannot remove the configuration via runner api, if we return an error here we just fill the database
360+
log.Error("poller: cannot register new runner as ephemeral upgrade Gitea to gain security, run-once will be used automatically")
361+
}
362+
353363
if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil {
354364
return fmt.Errorf("failed to save runner config: %w", err)
355365
}

internal/pkg/config/registration.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ const registrationWarning = "This file is automatically generated by act-runner.
1414
type Registration struct {
1515
Warning string `json:"WARNING"` // Warning message to display, it's always the registrationWarning constant
1616

17-
ID int64 `json:"id"`
18-
UUID string `json:"uuid"`
19-
Name string `json:"name"`
20-
Token string `json:"token"`
21-
Address string `json:"address"`
22-
Labels []string `json:"labels"`
17+
ID int64 `json:"id"`
18+
UUID string `json:"uuid"`
19+
Name string `json:"name"`
20+
Token string `json:"token"`
21+
Address string `json:"address"`
22+
Labels []string `json:"labels"`
23+
Ephemeral bool `json:"ephemeral"`
2324
}
2425

2526
func LoadRegistration(file string) (*Registration, error) {

0 commit comments

Comments
 (0)