Skip to content

Commit

Permalink
Adding support for Additional Hooks Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
CerealBoy committed Dec 27, 2024
1 parent 37abab1 commit 5fc588d
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions agent/agent_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type AgentConfiguration struct {
BootstrapScript string
BuildPath string
HooksPath string
AdditionalHooksPaths []string
SocketsPath string
GitMirrorsPath string
GitMirrorsLockTimeout int
Expand Down
1 change: 1 addition & 0 deletions agent/job_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ func (r *JobRunner) createEnvironment(ctx context.Context) ([]string, error) {
env["BUILDKITE_GIT_MIRRORS_PATH"] = r.conf.AgentConfiguration.GitMirrorsPath
env["BUILDKITE_GIT_MIRRORS_SKIP_UPDATE"] = fmt.Sprint(r.conf.AgentConfiguration.GitMirrorsSkipUpdate)
env["BUILDKITE_HOOKS_PATH"] = r.conf.AgentConfiguration.HooksPath
env["BUILDKITE_ADDITIONAL_HOOKS_PATHS"] = strings.Join(r.conf.AgentConfiguration.AdditionalHooksPaths, ",")
env["BUILDKITE_PLUGINS_PATH"] = r.conf.AgentConfiguration.PluginsPath
env["BUILDKITE_SSH_KEYSCAN"] = fmt.Sprint(r.conf.AgentConfiguration.SSHKeyscan)
env["BUILDKITE_GIT_SUBMODULES"] = fmt.Sprint(r.conf.AgentConfiguration.GitSubmodules)
Expand Down
29 changes: 25 additions & 4 deletions clicommand/agent_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ type AgentStartConfig struct {
WriteJobLogsToStdout bool `cli:"write-job-logs-to-stdout"`
DisableWarningsFor []string `cli:"disable-warnings-for" normalize:"list"`

BuildPath string `cli:"build-path" normalize:"filepath" validate:"required"`
HooksPath string `cli:"hooks-path" normalize:"filepath"`
SocketsPath string `cli:"sockets-path" normalize:"filepath"`
PluginsPath string `cli:"plugins-path" normalize:"filepath"`
BuildPath string `cli:"build-path" normalize:"filepath" validate:"required"`
HooksPath string `cli:"hooks-path" normalize:"filepath"`
AdditionalHooksPaths []string `cli:"additional-hooks-paths" normalize:"list"`
SocketsPath string `cli:"sockets-path" normalize:"filepath"`
PluginsPath string `cli:"plugins-path" normalize:"filepath"`

Shell string `cli:"shell"`
BootstrapScript string `cli:"bootstrap-script" normalize:"commandpath"`
Expand Down Expand Up @@ -509,6 +510,12 @@ var AgentStartCommand = cli.Command{
Usage: "Directory where the hook scripts are found",
EnvVar: "BUILDKITE_HOOKS_PATH",
},
cli.StringSliceFlag{
Name: "additional-hooks-paths",
Value: &cli.StringSlice{},
Usage: "Additional directories to look for agent hooks",
EnvVar: "BUILDKITE_ADDITIONAL_HOOKS_PATHS",
},
cli.StringFlag{
Name: "sockets-path",
Value: defaultSocketsPath(),
Expand Down Expand Up @@ -978,6 +985,7 @@ var AgentStartCommand = cli.Command{
GitMirrorsLockTimeout: cfg.GitMirrorsLockTimeout,
GitMirrorsSkipUpdate: cfg.GitMirrorsSkipUpdate,
HooksPath: cfg.HooksPath,
AdditionalHooksPaths: cfg.AdditionalHooksPaths,
PluginsPath: cfg.PluginsPath,
GitCheckoutFlags: cfg.GitCheckoutFlags,
GitCloneFlags: cfg.GitCloneFlags,
Expand Down Expand Up @@ -1060,6 +1068,7 @@ var AgentStartCommand = cli.Command{
l.Debug("Bootstrap command: %s", agentConf.BootstrapScript)
l.Debug("Build path: %s", agentConf.BuildPath)
l.Debug("Hooks directory: %s", agentConf.HooksPath)
l.Debug("Additional hooks directories: %v", agentConf.AdditionalHooksPaths)
l.Debug("Plugins directory: %s", agentConf.PluginsPath)

if exps := experiments.KnownAndEnabled(ctx); len(exps) > 0 {
Expand Down Expand Up @@ -1398,6 +1407,18 @@ func agentLifecycleHook(hookName string, log logger.Logger, cfg AgentStartConfig
return nil
}

// also search for hook in any additionally provided locations
for _, h := range cfg.AdditionalHooksPaths {
p, err = hook.Find(h, hookName)
if err != nil {
if !os.IsNotExist(err) {
log.Error("Error finding %q hook: %v", hookName, err)
return err
}
return nil
}
}

// pipe from hook output to logger
r, w := io.Pipe()
sh, err := shell.New(
Expand Down
8 changes: 8 additions & 0 deletions clicommand/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type BootstrapConfig struct {
BinPath string `cli:"bin-path" normalize:"filepath"`
BuildPath string `cli:"build-path" normalize:"filepath"`
HooksPath string `cli:"hooks-path" normalize:"filepath"`
AdditionalHooksPaths []string `cli:"additional-hooks-paths" normalize:"list"`
SocketsPath string `cli:"sockets-path" normalize:"filepath"`
PluginsPath string `cli:"plugins-path" normalize:"filepath"`
CommandEval bool `cli:"command-eval"`
Expand Down Expand Up @@ -280,6 +281,12 @@ var BootstrapCommand = cli.Command{
Usage: "Directory where the hook scripts are found",
EnvVar: "BUILDKITE_HOOKS_PATH",
},
cli.StringSliceFlag{
Name: "additional-hooks-paths",
Value: &cli.StringSlice{},
Usage: "Any additional directories to look for agent hooks",
EnvVar: "BUILDKITE_ADDITIONAL_HOOKS_PATHS",
},
cli.StringFlag{
Name: "sockets-path",
Value: defaultSocketsPath(),
Expand Down Expand Up @@ -449,6 +456,7 @@ var BootstrapCommand = cli.Command{
GitSubmodules: cfg.GitSubmodules,
GitSubmoduleCloneConfig: cfg.GitSubmoduleCloneConfig,
HooksPath: cfg.HooksPath,
AdditionalHooksPaths: cfg.AdditionalHooksPaths,
JobID: cfg.JobID,
LocalHooksEnabled: cfg.LocalHooksEnabled,
OrganizationSlug: cfg.OrganizationSlug,
Expand Down
3 changes: 3 additions & 0 deletions internal/job/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ type ExecutorConfig struct {
// Path to the global hooks
HooksPath string

// Additional hooks directories that can be provided
AdditionalHooksPaths []string

// Path to the plugins directory
PluginsPath string

Expand Down
3 changes: 3 additions & 0 deletions packaging/docker/alpine-k8s/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/buildkite/builds"
# Directory where the hook scripts are found
hooks-path="/buildkite/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="/buildkite/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/docker/alpine/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/buildkite/builds"
# Directory where the hook scripts are found
hooks-path="/buildkite/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="/buildkite/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/docker/sidecar/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/buildkite/builds"
# Directory where the hook scripts are found
hooks-path="/buildkite/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="/buildkite/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/docker/ubuntu-20.04/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/buildkite/builds"
# Directory where the hook scripts are found
hooks-path="/buildkite/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="/buildkite/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/docker/ubuntu-22.04/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/buildkite/builds"
# Directory where the hook scripts are found
hooks-path="/buildkite/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="/buildkite/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/docker/ubuntu-24.04/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/buildkite/builds"
# Directory where the hook scripts are found
hooks-path="/buildkite/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="/buildkite/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/github/linux/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="$HOME/.buildkite-agent/builds"
# Directory where the hook scripts are found
hooks-path="$HOME/.buildkite-agent/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# Directory where plugins will be installed
plugins-path="$HOME/.buildkite-agent/plugins"

Expand Down
3 changes: 3 additions & 0 deletions packaging/github/windows/buildkite-agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ build-path="C:\buildkite-agent\builds"
# Directory where the hook scripts are found
hooks-path="C:\buildkite-agent\hooks"

# Any additional directories where hook scripts are found
# additional-hooks-paths="C:\hooks\a,C:\hooks\b"

# Directory where plugins will be installed
plugins-path="C:\buildkite-agent\plugins"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build-path="/var/lib/buildkite-agent/builds"
# Directory where the hook scripts are found
hooks-path="/etc/buildkite-agent/hooks"

# Any additional directories where hook scripts are found
# additional-hooks-path="/hook/path/a,/hook/path/b"

# When plugins are installed they will be saved to this path
plugins-path="/etc/buildkite-agent/plugins"

Expand Down

0 comments on commit 5fc588d

Please sign in to comment.