fix(stacks): run webhooks in background to avoid GitHub timeouts BE-11260
parent
2bd880ec29
commit
8aae557266
|
@ -1,6 +1,7 @@
|
||||||
package deployments
|
package deployments
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -43,21 +44,19 @@ func RedeployWhenChanged(stackID portainer.StackID, deployer StackDeployer, data
|
||||||
|
|
||||||
// Webhook
|
// Webhook
|
||||||
if stack.AutoUpdate != nil && stack.AutoUpdate.Webhook != "" {
|
if stack.AutoUpdate != nil && stack.AutoUpdate.Webhook != "" {
|
||||||
return redeployWhenChanged(stack, deployer, datastore, gitService)
|
return redeployWhenChanged(stack, deployer, datastore, gitService, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Polling
|
// Polling
|
||||||
_, err, _ = singleflightGroup.Do(strconv.Itoa(int(stackID)), func() (any, error) {
|
_, err, _ = singleflightGroup.Do(strconv.Itoa(int(stackID)), func() (any, error) {
|
||||||
return nil, redeployWhenChanged(stack, deployer, datastore, gitService)
|
return nil, redeployWhenChanged(stack, deployer, datastore, gitService, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datastore dataservices.DataStore, gitService portainer.GitService) error {
|
func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datastore dataservices.DataStore, gitService portainer.GitService, webhook bool) error {
|
||||||
stackID := stack.ID
|
log.Debug().Int("stack_id", int(stack.ID)).Msg("redeploying stack")
|
||||||
|
|
||||||
log.Debug().Int("stack_id", int(stackID)).Msg("redeploying stack")
|
|
||||||
|
|
||||||
if stack.GitConfig == nil {
|
if stack.GitConfig == nil {
|
||||||
return nil // do nothing if it isn't a git-based stack
|
return nil // do nothing if it isn't a git-based stack
|
||||||
|
@ -76,17 +75,14 @@ func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datasto
|
||||||
return errors.WithMessagef(err, "failed to find the environment %v associated to the stack %v", stack.EndpointID, stack.ID)
|
return errors.WithMessagef(err, "failed to find the environment %v associated to the stack %v", stack.EndpointID, stack.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
author := stack.UpdatedBy
|
author := cmp.Or(stack.UpdatedBy, stack.CreatedBy)
|
||||||
if author == "" {
|
|
||||||
author = stack.CreatedBy
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := datastore.User().UserByUsername(author)
|
user, err := datastore.User().UserByUsername(author)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().
|
log.Warn().
|
||||||
Int("stack_id", int(stackID)).
|
Int("stack_id", int(stack.ID)).
|
||||||
Str("author", author).
|
|
||||||
Str("stack", stack.Name).
|
Str("stack", stack.Name).
|
||||||
|
Str("author", author).
|
||||||
Int("endpoint_id", int(stack.EndpointID)).
|
Int("endpoint_id", int(stack.EndpointID)).
|
||||||
Msg("cannot auto update a stack, stack author user is missing")
|
Msg("cannot auto update a stack, stack author user is missing")
|
||||||
|
|
||||||
|
@ -97,9 +93,36 @@ func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datasto
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if webhook {
|
||||||
|
go func() {
|
||||||
|
if err := redeployWhenChangedSecondStage(stack, deployer, datastore, gitService, user, endpoint); err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Int("stack_id", int(stack.ID)).
|
||||||
|
Str("stack", stack.Name).
|
||||||
|
Str("author", author).
|
||||||
|
Int("endpoint_id", int(stack.EndpointID)).
|
||||||
|
Msg("webhook failed to redeploy a stack")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return redeployWhenChangedSecondStage(stack, deployer, datastore, gitService, user, endpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
func redeployWhenChangedSecondStage(
|
||||||
|
stack *portainer.Stack,
|
||||||
|
deployer StackDeployer,
|
||||||
|
datastore dataservices.DataStore,
|
||||||
|
gitService portainer.GitService,
|
||||||
|
user *portainer.User,
|
||||||
|
endpoint *portainer.Endpoint,
|
||||||
|
) error {
|
||||||
var gitCommitChangedOrForceUpdate bool
|
var gitCommitChangedOrForceUpdate bool
|
||||||
|
|
||||||
if !stack.FromAppTemplate {
|
if !stack.FromAppTemplate {
|
||||||
updated, newHash, err := update.UpdateGitObject(gitService, fmt.Sprintf("stack:%d", stackID), stack.GitConfig, false, false, stack.ProjectPath)
|
updated, newHash, err := update.UpdateGitObject(gitService, fmt.Sprintf("stack:%d", stack.ID), stack.GitConfig, false, false, stack.ProjectPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -124,7 +147,6 @@ func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datasto
|
||||||
|
|
||||||
switch stack.Type {
|
switch stack.Type {
|
||||||
case portainer.DockerComposeStack:
|
case portainer.DockerComposeStack:
|
||||||
|
|
||||||
if stackutils.IsRelativePathStack(stack) {
|
if stackutils.IsRelativePathStack(stack) {
|
||||||
err = deployer.DeployRemoteComposeStack(stack, endpoint, registries, true, false)
|
err = deployer.DeployRemoteComposeStack(stack, endpoint, registries, true, false)
|
||||||
} else {
|
} else {
|
||||||
|
@ -132,7 +154,7 @@ func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datasto
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessagef(err, "failed to deploy a docker compose stack %v", stackID)
|
return errors.WithMessagef(err, "failed to deploy a docker compose stack %v", stack.ID)
|
||||||
}
|
}
|
||||||
case portainer.DockerSwarmStack:
|
case portainer.DockerSwarmStack:
|
||||||
if stackutils.IsRelativePathStack(stack) {
|
if stackutils.IsRelativePathStack(stack) {
|
||||||
|
@ -141,16 +163,13 @@ func redeployWhenChanged(stack *portainer.Stack, deployer StackDeployer, datasto
|
||||||
err = deployer.DeploySwarmStack(stack, endpoint, registries, true, true)
|
err = deployer.DeploySwarmStack(stack, endpoint, registries, true, true)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessagef(err, "failed to deploy a docker compose stack %v", stackID)
|
return errors.WithMessagef(err, "failed to deploy a docker compose stack %v", stack.ID)
|
||||||
}
|
}
|
||||||
case portainer.KubernetesStack:
|
case portainer.KubernetesStack:
|
||||||
log.Debug().
|
log.Debug().Int("stack_id", int(stack.ID)).Msg("deploying a kube app")
|
||||||
Int("stack_id", int(stackID)).
|
|
||||||
Msg("deploying a kube app")
|
|
||||||
|
|
||||||
err := deployer.DeployKubernetesStack(stack, endpoint, user)
|
if err := deployer.DeployKubernetesStack(stack, endpoint, user); err != nil {
|
||||||
if err != nil {
|
return errors.WithMessagef(err, "failed to deploy a kubernetes app stack %v", stack.ID)
|
||||||
return errors.WithMessagef(err, "failed to deploy a kubernetes app stack %v", stackID)
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("cannot update stack, type %v is unsupported", stack.Type)
|
return errors.Errorf("cannot update stack, type %v is unsupported", stack.Type)
|
||||||
|
|
Loading…
Reference in New Issue