feat: add post_exec_script support (#520)

This commit is contained in:
Richard Steinbrück 2025-06-29 20:18:06 +02:00 committed by GitHub
parent 04f9fb89ba
commit 8e197fe8ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 3 deletions

View File

@ -28,7 +28,7 @@ ENV GHORG_CONFIG=/config/conf.yaml
ENV GHORG_RECLONE_PATH=/config/reclone.yaml ENV GHORG_RECLONE_PATH=/config/reclone.yaml
ENV GHORG_ABSOLUTE_PATH_TO_CLONE_TO=/data ENV GHORG_ABSOLUTE_PATH_TO_CLONE_TO=/data
RUN apk add -U --no-cache ca-certificates openssh-client tzdata git \ RUN apk add -U --no-cache ca-certificates openssh-client tzdata git curl \
&& mkdir -p /data $XDG_CONFIG_HOME \ && mkdir -p /data $XDG_CONFIG_HOME \
&& addgroup --gid $GID $GROUP \ && addgroup --gid $GID $GROUP \
&& adduser -D -H --gecos "" \ && adduser -D -H --gecos "" \

View File

@ -249,6 +249,35 @@ The `ghorg reclone` command is a way to store all your `ghorg clone` commands in
Once your [reclone.yaml](https://github.com/gabrie30/ghorg/blob/master/sample-reclone.yaml) configuration is set you can call `ghorg reclone` to clone each entry individually or clone all at once, see examples below. Once your [reclone.yaml](https://github.com/gabrie30/ghorg/blob/master/sample-reclone.yaml) configuration is set you can call `ghorg reclone` to clone each entry individually or clone all at once, see examples below.
Each reclone entry can have:
- `cmd`: The ghorg clone command to execute (required)
- `description`: A description of what the command does (optional)
- `post_exec_script`: Path to a script that will be called after the clone command finishes (optional). The script will always be called, regardless of success or failure, and receives two arguments: the status (`success` or `fail`) and the name of the reclone entry. This allows you to implement custom notifications, monitoring, or other automation (optional)
Example `reclone.yaml` entry:
```yaml
gitlab-examples:
cmd: "ghorg clone gitlab-examples --scm=gitlab --token=XXXXXXX"
post_exec_script: "/path/to/notify.sh"
```
Example script for `post_exec_script` (e.g. `/path/to/notify.sh`):
```sh
#!/bin/sh
STATUS="$1"
NAME="$2"
if [ "$STATUS" = "success" ]; then
# Success webhook
curl -fsS https://hc-ping.com/your-uuid-here
else
# Failure webhook
curl -fsS https://hc-ping.com/your-uuid-here/fail
fi
```
``` ```
# To clone all the entries in your reclone.yaml omit any arguments # To clone all the entries in your reclone.yaml omit any arguments
ghorg reclone ghorg reclone

View File

@ -20,8 +20,9 @@ var reCloneCmd = &cobra.Command{
} }
type ReClone struct { type ReClone struct {
Cmd string `yaml:"cmd"` Cmd string `yaml:"cmd"`
Description string `yaml:"description"` Description string `yaml:"description"`
PostExecScript string `yaml:"post_exec_script"` // optional
} }
func isQuietReClone() bool { func isQuietReClone() bool {
@ -196,6 +197,22 @@ func runReClone(rc ReClone, rcIdentifier string) {
} }
err = ghorgClone.Wait() err = ghorgClone.Wait()
status := "success"
if err != nil {
status = "fail"
}
if rc.PostExecScript != "" {
postCmd := exec.Command(rc.PostExecScript, status, rcIdentifier)
postCmd.Stdout = os.Stdout
postCmd.Stderr = os.Stderr
errPost := postCmd.Run()
if errPost != nil {
colorlog.PrintError(fmt.Sprintf("ERROR: Running post_exec_script %s: %v", rc.PostExecScript, errPost))
}
}
if err != nil { if err != nil {
spinningSpinner.Stop() spinningSpinner.Stop()
colorlog.PrintErrorAndExit(fmt.Sprintf("ERROR: Running ghorg clone cmd: %v, err: %v", safeToLogCmd, err)) colorlog.PrintErrorAndExit(fmt.Sprintf("ERROR: Running ghorg clone cmd: %v, err: %v", safeToLogCmd, err))

View File

@ -16,6 +16,7 @@
# Example for gitlab; update with your gitlab cloud token # Example for gitlab; update with your gitlab cloud token
gitlab-examples: gitlab-examples:
cmd: "ghorg clone gitlab-examples --scm=gitlab --preserve-dir --token=XXXXXXX" cmd: "ghorg clone gitlab-examples --scm=gitlab --preserve-dir --token=XXXXXXX"
post_exec_script: "/path/to/notify.sh"
# Examples from README.md; update with your github cloud token # Examples from README.md; update with your github cloud token
kubernetes: kubernetes: