From db8159c4f12cfb6cbe91061342c934e709fd1296 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Mon, 29 Jul 2024 23:38:43 +0200 Subject: [PATCH 1/4] feat: add additional deploy config options --- internal/config/deploy_config.go | 2 ++ internal/docker/compose.go | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/config/deploy_config.go b/internal/config/deploy_config.go index f1b35c5..adbb7a3 100644 --- a/internal/config/deploy_config.go +++ b/internal/config/deploy_config.go @@ -26,6 +26,8 @@ type DeployConfig struct { ComposeFiles []string `yaml:"compose_files" default:"[\"compose.yaml\", \"compose.yml\", \"docker-compose.yml\", \"docker-compose.yaml\"]"` // ComposeFiles is the list of docker-compose files to use SkipTLSVerification bool `yaml:"skip_tls_verify" default:"false"` // SkipTLSVerification skips the TLS verification RemoveOrphans bool `yaml:"remove_orphans" default:"true"` // RemoveOrphans removes containers for services not defined in the Compose file + ForceRecreate bool `yaml:"force_recreate" default:"false"` // ForceRecreate forces the recreation/redeployment of containers even if the configuration has not changed + ForceImagePull bool `yaml:"force_image_pull" default:"false"` // ForceImagePull always pulls the latest version of the image tags you've specified if a newer version is available Timeout int `yaml:"timeout" default:"300"` // Timeout is the time to wait for the deployment to finish in seconds before timing out } diff --git a/internal/docker/compose.go b/internal/docker/compose.go index d6b4b55..10db061 100644 --- a/internal/docker/compose.go +++ b/internal/docker/compose.go @@ -196,9 +196,25 @@ func DeployCompose(ctx context.Context, dockerCli command.Cli, project *types.Pr addServiceLabels(project) + if deployConfig.ForceImagePull { + err := service.Pull(ctx, project, api.PullOptions{ + Quiet: true, + }) + if err != nil { + return err + } + } + + recreateType := api.RecreateDiverged + if deployConfig.ForceRecreate { + recreateType = api.RecreateForce + } + createOpts := api.CreateOptions{ - RemoveOrphans: deployConfig.RemoveOrphans, - QuietPull: true, + RemoveOrphans: deployConfig.RemoveOrphans, + Recreate: recreateType, + RecreateDependencies: recreateType, + QuietPull: false, } startOpts := api.StartOptions{ @@ -217,6 +233,8 @@ func DeployCompose(ctx context.Context, dockerCli command.Cli, project *types.Pr if err != nil { return err } + } else { + return err } } From ea72fe6c159e2b93b49f01183683b3a64e7df7fe Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Mon, 29 Jul 2024 23:40:01 +0200 Subject: [PATCH 2/4] fix: prevent redeployment when config is identical --- cmd/docker-compose-webhook/main.go | 2 +- internal/git/git.go | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/docker-compose-webhook/main.go b/cmd/docker-compose-webhook/main.go index b4ee042..f7f3f50 100644 --- a/cmd/docker-compose-webhook/main.go +++ b/cmd/docker-compose-webhook/main.go @@ -147,7 +147,7 @@ func main() { cloneUrl = git.GetAuthUrl(event.Repository.CloneURL, c.GitAccessToken) } - repo, err := git.CloneRepository(event.Repository.Name, cloneUrl, event.Ref) + repo, err := git.CloneRepository(event.Repository.FullName, cloneUrl, event.Ref) if err != nil { errMsg = "failed to clone repository" log.Error( diff --git a/internal/git/git.go b/internal/git/git.go index 6522b06..bbf0b6e 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -1,7 +1,7 @@ package git import ( - "errors" + "fmt" "os" "regexp" @@ -9,17 +9,15 @@ import ( "github.com/go-git/go-git/v5/plumbing" ) -var NotImplementedError = errors.New("not implemented") - // CloneRepository clones a repository from a given URL and branch into the memory filesystem func CloneRepository(name, url, ref string) (*git.Repository, error) { - // Create a temporary directory with a unique name - tempDir, err := os.MkdirTemp(os.TempDir(), "deploy-*") + dir := fmt.Sprintf("%s/%s", os.TempDir(), name) + err := os.MkdirAll(dir, os.ModePerm) if err != nil { return nil, err } - return git.PlainClone(tempDir, false, &git.CloneOptions{ + return git.PlainClone(dir, false, &git.CloneOptions{ URL: url, SingleBranch: true, ReferenceName: plumbing.ReferenceName(ref), From a1d9561e8fd720ed3ef574015cf2e068c7da8c73 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Mon, 29 Jul 2024 23:40:20 +0200 Subject: [PATCH 3/4] refactor: remove unused code --- internal/git/git.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index bbf0b6e..9df487d 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -26,11 +26,6 @@ func CloneRepository(name, url, ref string) (*git.Repository, error) { }) } -func DirectoryExists(path string) bool { - _, err := os.Stat(path) - return !os.IsNotExist(err) -} - // GetAuthUrl returns a clone URL with an access token for private repositories func GetAuthUrl(url, token string) string { // Retrieve the protocol from the clone URL (e.g. https://, http://, git:// From 4e844d328222047ac7d61da4d690feeb9c5afac0 Mon Sep 17 00:00:00 2001 From: Kim Oliver Drechsel Date: Mon, 29 Jul 2024 23:40:50 +0200 Subject: [PATCH 4/4] refactor: small adjustments to log output --- cmd/docker-compose-webhook/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/docker-compose-webhook/main.go b/cmd/docker-compose-webhook/main.go index f7f3f50..fc22a88 100644 --- a/cmd/docker-compose-webhook/main.go +++ b/cmd/docker-compose-webhook/main.go @@ -104,8 +104,8 @@ func main() { switch event := payload.(type) { case github.PushPayload: - log.Debug( - "push event received", + log.Info( + "preparing project deployment", slog.String("repository", event.Repository.FullName), slog.String("reference", event.Ref)) @@ -306,7 +306,8 @@ func main() { return } - log.Debug("deploying project", slog.String("repository", event.Repository.FullName)) + log.Info("deploying project", slog.String("repository", event.Repository.FullName)) + err = docker.DeployCompose(ctx, dockerCli, project, deployConfig) if err != nil { errMsg = "failed to deploy project"