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:
1 parent
7f213beefa
commit
6f55b89149
6 files changed
+32
-15
No files matched your search
@@ -28,18 +28,11 @@ trigger-builder-pack-builder-build() {
|
|||||||
|
|
||||||
pushd "$SOURCECODE_WORK_DIR" &>/dev/null
|
pushd "$SOURCECODE_WORK_DIR" &>/dev/null
|
||||||
|
|
||||||
ENV_ARGS_VALUES="$(config_export app "$APP" --format envfile --merged)"
|
ENV_ARGS=($(config_export app "$APP" --format pack --merged))
|
||||||
|
|
||||||
while IFS= read -r line; do
|
|
||||||
ENV_ARGS="$ENV_ARGS--env ${line%=*} "
|
|
||||||
done <<< "$ENV_ARGS_VALUES"
|
|
||||||
|
|
||||||
eval "$(config_export app "$APP" --merged)"
|
eval "$(config_export app "$APP" --merged)"
|
||||||
|
|
||||||
plugn trigger pre-build-pack "$APP" "$SOURCECODE_WORK_DIR"
|
plugn trigger pre-build-pack "$APP" "$SOURCECODE_WORK_DIR"
|
||||||
|
pack build "$IMAGE" --builder "$DOKKU_CNB_BUILDER" --path "$SOURCECODE_WORK_DIR" --default-process web "${ENV_ARGS[@]}"
|
||||||
eval "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"
|
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"
|
plugn trigger post-build-pack "$APP" "$SOURCECODE_WORK_DIR"
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ const (
|
|||||||
ExportFormatEnvfile
|
ExportFormatEnvfile
|
||||||
//ExportFormatDockerArgs format: --env KEY=VALUE args for docker
|
//ExportFormatDockerArgs format: --env KEY=VALUE args for docker
|
||||||
ExportFormatDockerArgs
|
ExportFormatDockerArgs
|
||||||
//ExportFormatDockerArgsKeys format: --env KEY args for docker
|
//ExportFormatDockerArgsKeys format: --env=KEY args for docker
|
||||||
ExportFormatDockerArgsKeys
|
ExportFormatDockerArgsKeys
|
||||||
//ExportFormatShell format: env arguments for shell
|
//ExportFormatShell format: env arguments for shell
|
||||||
ExportFormatShell
|
ExportFormatShell
|
||||||
@@ -36,6 +36,8 @@ const (
|
|||||||
ExportFormatJSON
|
ExportFormatJSON
|
||||||
//ExportFormatJSONList format: json output as a list of objects
|
//ExportFormatJSONList format: json output as a list of objects
|
||||||
ExportFormatJSONList
|
ExportFormatJSONList
|
||||||
|
//ExportFormatPackArgKeys format: --env KEY args for pack
|
||||||
|
ExportFormatPackArgKeys
|
||||||
)
|
)
|
||||||
|
|
||||||
//Env is a representation for global or app environment
|
//Env is a representation for global or app environment
|
||||||
@@ -183,6 +185,8 @@ func (e *Env) Export(format ExportFormat) string {
|
|||||||
return e.JSONString()
|
return e.JSONString()
|
||||||
case ExportFormatJSONList:
|
case ExportFormatJSONList:
|
||||||
return e.JSONListString()
|
return e.JSONListString()
|
||||||
|
case ExportFormatPackArgKeys:
|
||||||
|
return e.PackArgKeysAsString()
|
||||||
default:
|
default:
|
||||||
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
|
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
|
||||||
return ""
|
return ""
|
||||||
@@ -244,6 +248,16 @@ func (e *Env) JSONListString() string {
|
|||||||
return string(data)
|
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'"
|
//ShellString gets the contents of this Env in the form "KEY='value' KEY2='value'"
|
||||||
// for passing the environment in the shell
|
// for passing the environment in the shell
|
||||||
func (e *Env) ShellString() string {
|
func (e *Env) ShellString() string {
|
||||||
|
|||||||
@@ -13,14 +13,15 @@ func export(appName string, global bool, merged bool, format string) error {
|
|||||||
suffix := "\n"
|
suffix := "\n"
|
||||||
|
|
||||||
exportTypes := map[string]ExportFormat{
|
exportTypes := map[string]ExportFormat{
|
||||||
"exports": ExportFormatExports,
|
|
||||||
"envfile": ExportFormatEnvfile,
|
|
||||||
"docker-args": ExportFormatDockerArgs,
|
"docker-args": ExportFormatDockerArgs,
|
||||||
"docker-args-keys": ExportFormatDockerArgsKeys,
|
"docker-args-keys": ExportFormatDockerArgsKeys,
|
||||||
"shell": ExportFormatShell,
|
"envfile": ExportFormatEnvfile,
|
||||||
"pretty": ExportFormatPretty,
|
"exports": ExportFormatExports,
|
||||||
"json": ExportFormatJSON,
|
"json": ExportFormatJSON,
|
||||||
"json-list": ExportFormatJSONList,
|
"json-list": ExportFormatJSONList,
|
||||||
|
"pack": ExportFormatPackArgKeys,
|
||||||
|
"pretty": ExportFormatPretty,
|
||||||
|
"shell": ExportFormatShell,
|
||||||
}
|
}
|
||||||
|
|
||||||
exportType, ok := exportTypes[format]
|
exportType, ok := exportTypes[format]
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func main() {
|
|||||||
args := flag.NewFlagSet("config:export", flag.ExitOnError)
|
args := flag.NewFlagSet("config:export", flag.ExitOnError)
|
||||||
global := args.Bool("global", false, "--global: use the global environment")
|
global := args.Bool("global", false, "--global: use the global environment")
|
||||||
merged := args.Bool("merged", false, "--merged: merge app environment and 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:])
|
args.Parse(os.Args[2:])
|
||||||
if !*global {
|
if !*global {
|
||||||
appName = args.Arg(0)
|
appName = args.Arg(0)
|
||||||
|
|||||||
Executable
+3
@@ -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)" {
|
@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"
|
run /bin/bash -c "dokku builder:set $TEST_APP selected pack"
|
||||||
echo "output: $output"
|
echo "output: $output"
|
||||||
echo "status: $status"
|
echo "status: $status"
|
||||||
@@ -31,6 +36,7 @@ teardown() {
|
|||||||
assert_success
|
assert_success
|
||||||
assert_output_contains 'Building with buildpack 1' 0
|
assert_output_contains 'Building with buildpack 1' 0
|
||||||
assert_output_contains 'Installing requirements with pip'
|
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"
|
run /bin/bash -c "dokku builder-pack:set $TEST_APP projecttoml-path nonexistent.toml"
|
||||||
echo "output: $output"
|
echo "output: $output"
|
||||||
|
|||||||
Reference in new issue
Block a user