Skip to content

Commit

Permalink
feat: add project docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jan 22, 2024
1 parent 29e2a29 commit cfd7e6c
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config/folder
/var
.git
node_modules
.idea
27 changes: 22 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions cmd/project/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions cmd/project/docker.go
Original file line number Diff line number Diff line change
@@ -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)
}
76 changes: 76 additions & 0 deletions cmd/project/docker_build.go
Original file line number Diff line number Diff line change
@@ -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)
}
40 changes: 40 additions & 0 deletions cmd/project/templates/Dockerfile.tpl
Original file line number Diff line number Diff line change
@@ -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 <<EOF /usr/local/etc/php/conf.d/99-z-custom.ini
{{- range $key, $value := .PHP.Settings }}

{{$key}} = {{$value}}

{{- end }}
EOF

{{- end }}

USER 1000

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
6 changes: 3 additions & 3 deletions extension/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func validatePHPFiles(c context.Context, ctx *ValidationContext) {
return
}

phpVersion, err := getPhpVersion(c, constraint)
phpVersion, err := GetPhpVersion(c, constraint)
if err != nil {
ctx.AddWarning(fmt.Sprintf("Could not find min php version for plugin: %s", err.Error()))
return
Expand All @@ -277,7 +277,7 @@ func validatePHPFiles(c context.Context, ctx *ValidationContext) {
}
}

func getPhpVersion(ctx context.Context, constraint *version.Constraints) (string, error) {
func GetPhpVersion(ctx context.Context, constraint *version.Constraints) (string, error) {
r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://raw.githubusercontent.com/FriendsOfShopware/shopware-static-data/main/data/php-version.json", http.NoBody)

resp, err := http.DefaultClient.Do(r)
Expand All @@ -287,7 +287,7 @@ func getPhpVersion(ctx context.Context, constraint *version.Constraints) (string

defer func() {
if err := resp.Body.Close(); err != nil {
logging.FromContext(ctx).Errorf("getPhpVersion: %v", err)
logging.FromContext(ctx).Errorf("GetPhpVersion: %v", err)
}
}()

Expand Down
21 changes: 21 additions & 0 deletions shop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
AdminApi *ConfigAdminApi `yaml:"admin_api,omitempty"`
ConfigDump *ConfigDump `yaml:"dump,omitempty"`
Sync *ConfigSync `yaml:"sync,omitempty"`
Docker *ConfigDocker `yaml:"docker,omitempty"`
}

type ConfigBuild struct {
Expand All @@ -30,6 +31,17 @@ type ConfigBuild struct {
ExcludeExtensions []string `yaml:"exclude_extensions,omitempty"`
}

type ConfigDockerPHP struct {
PhpVersion string `yaml:"version,omitempty"`
Extensions []string `yaml:"extensions,omitempty"`
Settings map[string]string `yaml:"ini,omitempty"`
}

type ConfigDocker struct {
PHP ConfigDockerPHP `yaml:"php"`
ExcludePaths []string `yaml:"exclude_paths,omitempty"`
}

type ConfigAdminApi struct {
ClientId string `yaml:"client_id,omitempty"`
ClientSecret string `yaml:"client_secret,omitempty"`
Expand Down Expand Up @@ -135,6 +147,15 @@ func fillEmptyConfig(c *Config) *Config {
c.Build = &ConfigBuild{}
}

if c.Docker == nil {
c.Docker = &ConfigDocker{
PHP: ConfigDockerPHP{
Extensions: make([]string, 0),
Settings: make(map[string]string),
},
}
}

return c
}

Expand Down
27 changes: 27 additions & 0 deletions shop/shopware-project-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@
}
}
},
"Docker": {
"type": "object",
"title": "Docker configuration",
"additionalProperties": false,
"properties": {
"exclude_paths": {
"type": "array",
"items": {"type": "string"}
},
"php": {
"type": "object",
"additionalProperties": false,
"properties": {
"version": {
"type": "string"
},
"extensions": {
"type": "array",
"items": {"type": "string"}
},
"ini": {
"type": "object"
}
}
}
}
},
"Build": {
"type": "object",
"title": "Project Build Settings",
Expand Down

0 comments on commit cfd7e6c

Please sign in to comment.