refactor: simplify env var passing by using a new config:export format
Also add a test for env vars during build time for pack-built applications.
This commit is contained in:
@@ -28,18 +28,11 @@ trigger-builder-pack-builder-build() {
|
||||
|
||||
pushd "$SOURCECODE_WORK_DIR" &>/dev/null
|
||||
|
||||
ENV_ARGS_VALUES="$(config_export app "$APP" --format envfile --merged)"
|
||||
|
||||
while IFS= read -r line; do
|
||||
ENV_ARGS="$ENV_ARGS--env ${line%=*} "
|
||||
done <<< "$ENV_ARGS_VALUES"
|
||||
|
||||
ENV_ARGS=($(config_export app "$APP" --format pack --merged))
|
||||
eval "$(config_export app "$APP" --merged)"
|
||||
|
||||
plugn trigger pre-build-pack "$APP" "$SOURCECODE_WORK_DIR"
|
||||
|
||||
eval "pack build "$IMAGE" --builder "$DOKKU_CNB_BUILDER" --path "$SOURCECODE_WORK_DIR" --default-process web "$ENV_ARGS""
|
||||
|
||||
pack build "$IMAGE" --builder "$DOKKU_CNB_BUILDER" --path "$SOURCECODE_WORK_DIR" --default-process web "${ENV_ARGS[@]}"
|
||||
docker-image-labeler --label=com.dokku.image-stage=build --label=com.dokku.app-name=$APP --label=org.label-schema.schema-version=1.0 --label=org.label-schema.vendor=dokku --label=dokku "$IMAGE"
|
||||
|
||||
plugn trigger post-build-pack "$APP" "$SOURCECODE_WORK_DIR"
|
||||
|
||||
@@ -26,7 +26,7 @@ const (
|
||||
ExportFormatEnvfile
|
||||
//ExportFormatDockerArgs format: --env KEY=VALUE args for docker
|
||||
ExportFormatDockerArgs
|
||||
//ExportFormatDockerArgsKeys format: --env KEY args for docker
|
||||
//ExportFormatDockerArgsKeys format: --env=KEY args for docker
|
||||
ExportFormatDockerArgsKeys
|
||||
//ExportFormatShell format: env arguments for shell
|
||||
ExportFormatShell
|
||||
@@ -36,6 +36,8 @@ const (
|
||||
ExportFormatJSON
|
||||
//ExportFormatJSONList format: json output as a list of objects
|
||||
ExportFormatJSONList
|
||||
//ExportFormatPackArgKeys format: --env KEY args for pack
|
||||
ExportFormatPackArgKeys
|
||||
)
|
||||
|
||||
//Env is a representation for global or app environment
|
||||
@@ -183,6 +185,8 @@ func (e *Env) Export(format ExportFormat) string {
|
||||
return e.JSONString()
|
||||
case ExportFormatJSONList:
|
||||
return e.JSONListString()
|
||||
case ExportFormatPackArgKeys:
|
||||
return e.PackArgKeysAsString()
|
||||
default:
|
||||
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
|
||||
return ""
|
||||
@@ -244,6 +248,16 @@ func (e *Env) JSONListString() string {
|
||||
return string(data)
|
||||
}
|
||||
|
||||
//PackArgKeysAsString gets the contents of this Env in the form -env KEY --env...
|
||||
func (e *Env) PackArgKeysAsString() string {
|
||||
keys := e.Keys()
|
||||
entries := make([]string, len(keys))
|
||||
for i, k := range keys {
|
||||
entries[i] = fmt.Sprintf("%s%s", "--env ", k)
|
||||
}
|
||||
return strings.Join(entries, " ")
|
||||
}
|
||||
|
||||
//ShellString gets the contents of this Env in the form "KEY='value' KEY2='value'"
|
||||
// for passing the environment in the shell
|
||||
func (e *Env) ShellString() string {
|
||||
|
||||
@@ -13,14 +13,15 @@ func export(appName string, global bool, merged bool, format string) error {
|
||||
suffix := "\n"
|
||||
|
||||
exportTypes := map[string]ExportFormat{
|
||||
"exports": ExportFormatExports,
|
||||
"envfile": ExportFormatEnvfile,
|
||||
"docker-args": ExportFormatDockerArgs,
|
||||
"docker-args-keys": ExportFormatDockerArgsKeys,
|
||||
"shell": ExportFormatShell,
|
||||
"pretty": ExportFormatPretty,
|
||||
"envfile": ExportFormatEnvfile,
|
||||
"exports": ExportFormatExports,
|
||||
"json": ExportFormatJSON,
|
||||
"json-list": ExportFormatJSONList,
|
||||
"pack": ExportFormatPackArgKeys,
|
||||
"pretty": ExportFormatPretty,
|
||||
"shell": ExportFormatShell,
|
||||
}
|
||||
|
||||
exportType, ok := exportTypes[format]
|
||||
|
||||
@@ -49,7 +49,7 @@ func main() {
|
||||
args := flag.NewFlagSet("config:export", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
|
||||
format := args.String("format", "exports", "--format: [ exports | envfile | docker-args | docker-args-keys | shell | pretty | json | json-list ] which format to export as)")
|
||||
format := args.String("format", "exports", "--format: [ docker-args | docker-args-keys | exports | envfile | json | json-list | pack | pretty | shell ] which format to export as)")
|
||||
args.Parse(os.Args[2:])
|
||||
if !*global {
|
||||
appName = args.Arg(0)
|
||||
|
||||
3
tests/apps/python/bin/pre_compile
Executable file
3
tests/apps/python/bin/pre_compile
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
echo "Build time env var SECRET_KEY=${SECRET_KEY}"
|
||||
@@ -15,6 +15,11 @@ teardown() {
|
||||
}
|
||||
|
||||
@test "(builder-pack:set)" {
|
||||
run /bin/bash -c "dokku config:set $TEST_APP SECRET_KEY=fjdkslafjdk"
|
||||
echo "output: $output"
|
||||
echo "status: $status"
|
||||
assert_success
|
||||
|
||||
run /bin/bash -c "dokku builder:set $TEST_APP selected pack"
|
||||
echo "output: $output"
|
||||
echo "status: $status"
|
||||
@@ -31,6 +36,7 @@ teardown() {
|
||||
assert_success
|
||||
assert_output_contains 'Building with buildpack 1' 0
|
||||
assert_output_contains 'Installing requirements with pip'
|
||||
assert_output_contains "Build time env var SECRET_KEY=fjdkslafjdk"
|
||||
|
||||
run /bin/bash -c "dokku builder-pack:set $TEST_APP projecttoml-path nonexistent.toml"
|
||||
echo "output: $output"
|
||||
|
||||
Reference in New Issue
Block a user