Skip to content

Commit

Permalink
Merge pull request #15 from kimdre/feat/add-deploy-options
Browse files Browse the repository at this point in the history
feat: add additional deploy config options
  • Loading branch information
kimdre authored Jul 29, 2024
2 parents 524810e + 4e844d3 commit e3d0a2c
Showing 4 changed files with 31 additions and 17 deletions.
9 changes: 5 additions & 4 deletions cmd/docker-compose-webhook/main.go
Original file line number Diff line number Diff line change
@@ -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))

@@ -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(
@@ -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"
2 changes: 2 additions & 0 deletions internal/config/deploy_config.go
Original file line number Diff line number Diff line change
@@ -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
}

22 changes: 20 additions & 2 deletions internal/docker/compose.go
Original file line number Diff line number Diff line change
@@ -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
}
}

15 changes: 4 additions & 11 deletions internal/git/git.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package git

import (
"errors"
"fmt"
"os"
"regexp"

"github.com/go-git/go-git/v5"
"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),
@@ -28,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://

0 comments on commit e3d0a2c

Please sign in to comment.