gitdeploy/internal/jobs/promote.go

111 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-02-21 11:25:42 +00:00
package jobs
import (
"os"
"os/exec"
"time"
"git.rootprojects.org/root/gitdeploy/internal/log"
"git.rootprojects.org/root/gitdeploy/internal/options"
"git.rootprojects.org/root/gitdeploy/internal/webhooks"
)
2021-02-24 08:13:36 +00:00
// Promotions channel
var Promotions = make(chan Promotion)
// Promotion is a channel message
type Promotion struct {
PromoteTo string
GitRef *webhooks.Ref
}
2021-02-21 11:25:42 +00:00
// Promote will run the promote script
func Promote(msg webhooks.Ref, promoteTo string) {
Promotions <- Promotion{
PromoteTo: promoteTo,
GitRef: &msg,
}
}
// promote will run the promote script
func promote(hook *webhooks.Ref, promoteTo string, runOpts *options.ServerConfig) {
// TODO create an origin-branch tag with a timestamp?
jobID1 := hook.GetRefID()
hookTo := *hook
hookTo.RefName = promoteTo
jobID2 := hookTo.GetRefID()
args := []string{
runOpts.ScriptsPath + "/promote.sh",
string(jobID1),
promoteTo,
hook.RefName,
hook.RefType,
hook.Owner,
hook.Repo,
hook.HTTPSURL,
}
cmd := exec.Command("bash", args...)
env := os.Environ()
envs := getEnvs(runOpts.Addr, string(jobID1), runOpts.RepoList, hook)
envs = append(envs, "GIT_DEPLOY_PROMOTE_TO="+promoteTo)
cmd.Env = append(env, envs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if _, ok := Actives.Load(jobID1); ok {
// TODO put promote in backlog
log.Printf("[promote] gitdeploy job already started for %s#%s\n", hook.HTTPSURL, hook.RefName)
return
}
if _, ok := Actives.Load(jobID2); ok {
// TODO put promote in backlog
log.Printf("[promote] gitdeploy job already started for %s#%s\n", hook.HTTPSURL, promoteTo)
return
}
if err := cmd.Start(); nil != err {
log.Printf("gitdeploy exec error: %s\n", err)
return
}
2021-02-24 08:13:36 +00:00
t := time.Now()
now := &t
promoteID := hook.RepoID + "#" + hook.RefName + ".." + promoteTo
2021-02-21 11:25:42 +00:00
Actives.Store(jobID1, &Job{
StartedAt: now,
2021-02-24 08:13:36 +00:00
ID: promoteID,
2021-02-21 11:25:42 +00:00
GitRef: hook,
2021-02-24 08:13:36 +00:00
PromoteTo: promoteTo,
Promote: true, // deprecated
cmd: cmd,
2021-02-21 11:25:42 +00:00
})
Actives.Store(jobID2, &Job{
StartedAt: now,
2021-02-24 08:13:36 +00:00
ID: promoteID,
2021-02-21 11:25:42 +00:00
GitRef: hook,
2021-02-24 08:13:36 +00:00
PromoteTo: promoteTo,
Promote: true, // deprecated
cmd: cmd,
})
Actives.Store(promoteID, &Job{
StartedAt: now,
ID: promoteID,
GitRef: hook,
PromoteTo: promoteTo,
Promote: true, // deprecated
cmd: cmd,
2021-02-21 11:25:42 +00:00
})
go func() {
log.Printf("gitdeploy promote for %s#%s started\n", hook.HTTPSURL, hook.RefName)
_ = cmd.Wait()
deathRow <- jobID1
deathRow <- jobID2
2021-02-24 08:13:36 +00:00
deathRow <- webhooks.RefID(promoteID)
2021-02-21 11:25:42 +00:00
log.Printf("gitdeploy promote for %s#%s finished\n", hook.HTTPSURL, hook.RefName)
// TODO check for backlog
}()
}