From cfd7e6c32320f67dbb485fc4fe69e2a609252a2e Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Mon, 22 Jan 2024 15:04:48 +0100 Subject: [PATCH] feat: add project docker build --- .dockerignore | 5 ++ Dockerfile | 27 ++++++++-- cmd/project/ci.go | 1 + cmd/project/docker.go | 12 +++++ cmd/project/docker_build.go | 76 ++++++++++++++++++++++++++++ cmd/project/templates/Dockerfile.tpl | 40 +++++++++++++++ extension/platform.go | 6 +-- shop/config.go | 21 ++++++++ shop/shopware-project-schema.json | 27 ++++++++++ 9 files changed, 207 insertions(+), 8 deletions(-) create mode 100755 .dockerignore create mode 100644 cmd/project/docker.go create mode 100644 cmd/project/docker_build.go create mode 100644 cmd/project/templates/Dockerfile.tpl diff --git a/.dockerignore b/.dockerignore new file mode 100755 index 00000000..0a728c17 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +config/folder +/var +.git +node_modules +.idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 7b8bcf25..2fca561e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,25 @@ -ARG PHP_VERSION +#syntax=docker/dockerfile:1.4 -FROM ghcr.io/friendsofshopware/shopware-cli-base:${PHP_VERSION} +# pin versions +FROM shopware/docker-base:8.2 as base-image +FROM ghcr.io/friendsofshopware/shopware-cli:latest-php-8.2 as shopware-cli -COPY shopware-cli /usr/local/bin/ +FROM base-image as base-extended +RUN install-php-extensions opentelemetry -ENTRYPOINT ["/usr/local/bin/entrypoint.sh", "shopware-cli"] -CMD ["--help"] +RUN echo "display_errors = 1" >> /usr/local/etc/php/conf.d/99-z-custom.ini + + +FROM shopware-cli as build + +ADD . /src +WORKDIR /src + +RUN --mount=type=secret,id=composer_auth,dst=/src/auth.json \ + --mount=type=cache,target=/root/.composer \ + --mount=type=cache,target=/root/.npm \ + /usr/local/bin/entrypoint.sh shopware-cli project ci /src + +FROM base-extended + +COPY --from=build --chown=www-data --link /src /var/www/html \ No newline at end of file diff --git a/cmd/project/ci.go b/cmd/project/ci.go index 75357b74..6d589865 100644 --- a/cmd/project/ci.go +++ b/cmd/project/ci.go @@ -176,6 +176,7 @@ func commandWithRoot(cmd *exec.Cmd, root string) *exec.Cmd { } func runTransparentCommand(cmd *exec.Cmd) error { + cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Env = append(os.Environ(), "APP_SECRET=test", "LOCK_DSN=flock") diff --git a/cmd/project/docker.go b/cmd/project/docker.go new file mode 100644 index 00000000..49565a29 --- /dev/null +++ b/cmd/project/docker.go @@ -0,0 +1,12 @@ +package project + +import "github.com/spf13/cobra" + +var dockerRootCmd = &cobra.Command{ + Use: "docker", + Short: "Docker Tools", +} + +func init() { + projectRootCmd.AddCommand(dockerRootCmd) +} diff --git a/cmd/project/docker_build.go b/cmd/project/docker_build.go new file mode 100644 index 00000000..56d92482 --- /dev/null +++ b/cmd/project/docker_build.go @@ -0,0 +1,76 @@ +package project + +import ( + "bytes" + _ "embed" + "github.com/FriendsOfShopware/shopware-cli/extension" + "github.com/FriendsOfShopware/shopware-cli/logging" + "github.com/FriendsOfShopware/shopware-cli/shop" + "github.com/spf13/cobra" + "os" + "os/exec" + "strings" + "text/template" +) + +//go:embed templates/Dockerfile.tpl +var dockerFileTemplate string + +var dockerBuildCmd = &cobra.Command{ + Use: "build [name]", + Short: "Build Docker Image", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + shopCfg, err := shop.ReadConfig(projectConfigPath, true) + if err != nil { + return err + } + + if shopCfg.Docker.PHP.PhpVersion == "" { + projectRoot, err := os.Getwd() + + if err != nil { + return err + } + + constraint, err := extension.GetShopwareProjectConstraint(projectRoot) + if err != nil { + return err + } + + phpVersion, err := extension.GetPhpVersion(cmd.Context(), constraint) + + if err != nil { + return err + } + + shopCfg.Docker.PHP.PhpVersion = phpVersion + logging.FromContext(cmd.Context()).Infof("No PHP version set, using PHP version %s", phpVersion) + } + + var buf bytes.Buffer + + if err := template. + Must(template.New("Dockerfile"). + Parse(dockerFileTemplate)). + Execute(&buf, *shopCfg.Docker); err != nil { + return err + } + + if err := os.WriteFile("Dockerfile", buf.Bytes(), os.ModePerm); err != nil { + return err + } + + shopCfg.Docker.ExcludePaths = append(shopCfg.Docker.ExcludePaths, "/var", ".git", "node_modules", ".idea") + + if err := os.WriteFile(".dockerignore", []byte(strings.Join(shopCfg.Docker.ExcludePaths, "\n")), os.ModePerm); err != nil { + return err + } + + return runTransparentCommand(exec.CommandContext(cmd.Context(), "docker", "build", "-t", args[0], ".")) + }, +} + +func init() { + dockerRootCmd.AddCommand(dockerBuildCmd) +} diff --git a/cmd/project/templates/Dockerfile.tpl b/cmd/project/templates/Dockerfile.tpl new file mode 100644 index 00000000..d50c6730 --- /dev/null +++ b/cmd/project/templates/Dockerfile.tpl @@ -0,0 +1,40 @@ +#syntax=docker/dockerfile:1.4 + +# pin versions +FROM shopware/docker-base:{{.PHP.PhpVersion}} as base-image +FROM ghcr.io/friendsofshopware/shopware-cli:latest-php-{{.PHP.PhpVersion}} as shopware-cli + +FROM base-image as base-extended + +USER root + +{{- if .PHP.Extensions }} +RUN /usr/local/bin/install-php-extensions {{- range $key, $value := .PHP.Extensions }} {{$value}} {{- end }} +{{- end }} +{{- if .PHP.Settings }} + +COPY <