Skip to content

Commit

Permalink
feat: allow setting environment variables inside project configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Jan 23, 2024
1 parent dc81eba commit 5fa20a5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
26 changes: 25 additions & 1 deletion cmd/project/docker_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package project
import (
"bytes"
_ "embed"
"fmt"
"github.com/FriendsOfShopware/shopware-cli/extension"
"github.com/FriendsOfShopware/shopware-cli/logging"
"github.com/FriendsOfShopware/shopware-cli/shop"
Expand Down Expand Up @@ -48,12 +49,35 @@ var dockerBuildCmd = &cobra.Command{
logging.FromContext(cmd.Context()).Infof("No PHP version set, using PHP version %s", phpVersion)
}

buildEnvironments := make([]string, 0)
runEnvironments := make([]string, 0)

for _, value := range shopCfg.Docker.Environment {
envLine := fmt.Sprintf("%s=%s", value.Name, value.Value)

if value.Only == "" {
buildEnvironments = append(buildEnvironments, envLine)
runEnvironments = append(runEnvironments, envLine)
} else if value.Only == "build" {
buildEnvironments = append(buildEnvironments, envLine)
} else if value.Only == "runtime" {
runEnvironments = append(runEnvironments, envLine)
}
}

templateVars := map[string]interface{}{
"PHP": shopCfg.Docker.PHP,
"ExcludePaths": shopCfg.Docker.ExcludePaths,
"BuildEnv": strings.Join(buildEnvironments, " "),
"RunEnv": strings.Join(runEnvironments, " "),
}

var buf bytes.Buffer

if err := template.
Must(template.New("Dockerfile").
Parse(dockerFileTemplate)).
Execute(&buf, *shopCfg.Docker); err != nil {
Execute(&buf, templateVars); err != nil {
return err
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/project/templates/Dockerfile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ FROM shopware-cli as build
ADD . /src
WORKDIR /src

{{- if .BuildEnv }}
ENV {{ .BuildEnv }}
{{- end }}

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

{{- if .RunEnv }}
ENV {{ .RunEnv }}
{{- end }}

COPY --from=build --chown=www-data --link /src /var/www/html
11 changes: 9 additions & 2 deletions shop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ type ConfigDockerPHP struct {
}

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

type ConfigDockerEnvironmentVariable struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
Only string `yaml:"only"`
}

type ConfigAdminApi struct {
Expand Down
24 changes: 24 additions & 0 deletions shop/shopware-project-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
},
"build": {
"$ref": "#/definitions/Build"
},
"docker": {
"$ref": "#/definitions/Docker"
}
}
},
Expand All @@ -36,6 +39,27 @@
"type": "array",
"items": {"type": "string"}
},
"env": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"required": ["name", "value"],
"properties": {
"name": {
"type": "string"
},
"value": {
"type": "string"
},
"only": {
"type": "string",
"description": "Only set the environment variable for the given stage",
"enum": ["build", "runtime"]
}
}
}
},
"php": {
"type": "object",
"additionalProperties": false,
Expand Down

0 comments on commit 5fa20a5

Please sign in to comment.