Skip to content

Commit

Permalink
feat: add additional deploy config options (#14)
Browse files Browse the repository at this point in the history
* fix: add error handling

* feat: add additional deploy config options
  • Loading branch information
kimdre authored Jul 29, 2024
1 parent 6aaf9fc commit 524810e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
3 changes: 1 addition & 2 deletions cmd/docker-compose-webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ func main() {
}

log.Debug("deploying project", slog.String("repository", event.Repository.FullName))
// TODO docker-compose deployment logic here
err = docker.DeployCompose(ctx, dockerCli, project)
err = docker.DeployCompose(ctx, dockerCli, project, deployConfig)
if err != nil {
errMsg = "failed to deploy project"
log.Error(
Expand Down
4 changes: 2 additions & 2 deletions internal/config/deploy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
var (
DefaultDeploymentConfigFileNames = []string{".compose-deploy.yaml", ".compose-deploy.yml"}
ErrConfigFileNotFound = errors.New("configuration file not found in repository")
ErrNoValue = errors.New("no value for field 'value'")
ErrInvalidValue = errors.New("invalid value for field 'value'")
ErrInvalidConfig = errors.New("invalid deploy configuration")
ErrKeyNotFound = errors.New("key not found")
)
Expand All @@ -27,6 +25,8 @@ type DeployConfig struct {
WorkingDirectory string `yaml:"working_dir" default:"."` // WorkingDirectory is the working directory for the deployment
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
Timeout int `yaml:"timeout" default:"300"` // Timeout is the time to wait for the deployment to finish in seconds before timing out
}

// DefaultDeployConfig creates a DeployConfig with default values
Expand Down
17 changes: 11 additions & 6 deletions internal/docker/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"syscall"
"time"

"github.com/kimdre/docker-compose-webhook/internal/config"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"

Expand All @@ -29,8 +31,10 @@ const (
apiVersion = "v1.46"
)

var ErrDockerSocketConnectionFailed = errors.New("failed to connect to docker socket")
var ErrNoContainerToStart = errors.New("no container to start")
var (
ErrDockerSocketConnectionFailed = errors.New("failed to connect to docker socket")
ErrNoContainerToStart = errors.New("no container to start")
)

// ConnectToSocket connects to the docker socket
func ConnectToSocket() (net.Conn, error) {
Expand Down Expand Up @@ -156,7 +160,7 @@ func addServiceLabels(project *types.Project) {
api.ProjectLabel: project.Name,
api.ServiceLabel: s.Name,
api.VersionLabel: api.ComposeVersion,
api.WorkingDirLabel: "/",
api.WorkingDirLabel: project.WorkingDir,
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
api.OneoffLabel: "False", // default, will be overridden by `run` command
}
Expand Down Expand Up @@ -187,19 +191,20 @@ func LoadCompose(ctx context.Context, workingDir, projectName string, composeFil
}

// DeployCompose deploys a project as specified by the Docker Compose specification (LoadCompose)
func DeployCompose(ctx context.Context, dockerCli command.Cli, project *types.Project) error {
func DeployCompose(ctx context.Context, dockerCli command.Cli, project *types.Project, deployConfig *config.DeployConfig) error {
service := compose.NewComposeService(dockerCli)

addServiceLabels(project)

createOpts := api.CreateOptions{
RemoveOrphans: true,
RemoveOrphans: deployConfig.RemoveOrphans,
QuietPull: true,
}

startOpts := api.StartOptions{
Project: project,
Wait: true,
WaitTimeout: time.Duration(3) * time.Minute,
WaitTimeout: time.Duration(deployConfig.Timeout) * time.Second,
}

err := service.Up(ctx, project, api.UpOptions{
Expand Down
6 changes: 5 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ func JSONError(w http.ResponseWriter, err interface{}, details, repo string, cod
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
json.NewEncoder(w).Encode(err)

err = json.NewEncoder(w).Encode(err)
if err != nil {
return
}
}

0 comments on commit 524810e

Please sign in to comment.