Skip to content

Commit d24bf1c

Browse files
committed
feat(runners): read registration token from file
1 parent 5279ab0 commit d24bf1c

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

cli/cmd/runner_register.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,42 @@ import (
1010
)
1111

1212
var runnerRegisterArgs struct {
13-
stdinRegistrationToken bool
14-
projectID int
13+
stdinRegistrationToken bool
14+
registrationTokenFilePath string
15+
projectID int
1516
}
1617

1718
func init() {
1819
runnerRegisterCmd.PersistentFlags().BoolVar(&runnerRegisterArgs.stdinRegistrationToken, "stdin-registration-token", false, "Read registration token from stdin")
20+
runnerRegisterCmd.PersistentFlags().StringVar(&runnerRegisterArgs.registrationTokenFilePath, "registration-token-file", "", "Read registration token from a file")
1921
runnerRegisterCmd.PersistentFlags().IntVar(&runnerRegisterArgs.projectID, "project-id", 0, "Project ID for project-level runner (global runner if not provided)")
2022
runnerCmd.AddCommand(runnerRegisterCmd)
2123
}
2224

25+
func readRegistrationTokenFromFile(path string) {
26+
tokenBytes, err := os.ReadFile(path)
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
if len(tokenBytes) == 0 {
32+
panic("Empty token")
33+
}
34+
35+
util.Config.Runner.RegistrationToken = strings.TrimSpace(string(tokenBytes))
36+
}
37+
2338
func initRunnerRegistrationToken() {
39+
if runnerRegisterArgs.registrationTokenFilePath != "" {
40+
readRegistrationTokenFromFile(runnerRegisterArgs.registrationTokenFilePath)
41+
return
42+
}
43+
44+
if util.Config.Runner.RegistrationTokenFile != "" {
45+
readRegistrationTokenFromFile(util.Config.Runner.RegistrationTokenFile)
46+
return
47+
}
48+
2449
if !runnerRegisterArgs.stdinRegistrationToken {
2550
return
2651
}

util/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ const (
114114
// */
115115

116116
type RunnerConfig struct {
117-
RegistrationToken string `json:"-" env:"SEMAPHORE_RUNNER_REGISTRATION_TOKEN,sensitive"`
118-
Token string `json:"token,omitempty" env:"SEMAPHORE_RUNNER_TOKEN,sensitive"`
119-
TokenFile string `json:"token_file,omitempty" env:"SEMAPHORE_RUNNER_TOKEN_FILE"`
120-
PrivateKeyFile string `json:"private_key_file,omitempty" env:"SEMAPHORE_RUNNER_PRIVATE_KEY_FILE"`
117+
RegistrationToken string `json:"-" env:"SEMAPHORE_RUNNER_REGISTRATION_TOKEN,sensitive"`
118+
RegistrationTokenFile string `json:"registration_token_file,omitempty" env:"SEMAPHORE_RUNNER_REGISTRATION_TOKEN_FILE"`
119+
Token string `json:"token,omitempty" env:"SEMAPHORE_RUNNER_TOKEN,sensitive"`
120+
TokenFile string `json:"token_file,omitempty" env:"SEMAPHORE_RUNNER_TOKEN_FILE"`
121+
PrivateKeyFile string `json:"private_key_file,omitempty" env:"SEMAPHORE_RUNNER_PRIVATE_KEY_FILE"`
121122

122123
// OneOff indicates than runner runs only one job and exit. It is very useful for dynamic runners.
123124
// How it works?

0 commit comments

Comments
 (0)