From 2c9222526fcf6f3ba4d78a69a7ad46dfc2c63fcb Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Thu, 29 Feb 2024 00:05:48 -0300 Subject: [PATCH 1/2] parsing variables on local images build for deploy --- services/cloud/build.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/services/cloud/build.go b/services/cloud/build.go index c6e9e36..4ca62be 100644 --- a/services/cloud/build.go +++ b/services/cloud/build.go @@ -8,6 +8,7 @@ import ( "kool-dev/kool/core/environment" "kool-dev/kool/core/shell" "kool-dev/kool/services/cloud/api" + "os" "path/filepath" "strings" @@ -51,7 +52,7 @@ func BuildPushImageForDeploy(service string, config *DeployConfigService, deploy if buildConfig.Args != nil { for k, v := range *buildConfig.Args { - dockerBuild.AppendArgs("--build-arg", fmt.Sprintf("%s=%s", k, v)) + dockerBuild.AppendArgs("--build-arg", fmt.Sprintf("%s=%s", k, parseDeployEnvs(v, deploy.Deploy.Environment.Env))) } } @@ -124,3 +125,23 @@ func parseBuild(build interface{}) (config *DeployConfigBuild, err error) { err = yaml.Unmarshal(b, config) return } + +func parseDeployEnvs(i interface{}, env interface{}) string { + // workaround to allow for escaping $ with a double $$ + _ = os.Setenv("$", "$") + + var value = os.ExpandEnv(fmt.Sprintf("%s", i)) + + if strings.Contains(value, "{{") && strings.Contains(value, "}}") { + // we have something to replace! + if recs, ok := env.(map[string]interface{}); ok { + for k, v := range recs { + if strings.Contains(value, "{{"+k+"}}") { + value = strings.ReplaceAll(value, "{{"+k+"}}", fmt.Sprintf("%v", v)) + } + } + } + } + + return value +} From 3917d20d1d8d281c1692969c7873c38291de04e5 Mon Sep 17 00:00:00 2001 From: fabriciojs Date: Thu, 29 Feb 2024 00:21:45 -0300 Subject: [PATCH 2/2] env docs for building deploy images --- docs/02-Kool-Cloud/03-Building-Images-to-Deploy.md | 13 +++++++++++++ docs/02-Kool-Cloud/05-Environment-Variables.md | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/02-Kool-Cloud/03-Building-Images-to-Deploy.md b/docs/02-Kool-Cloud/03-Building-Images-to-Deploy.md index 93558cd..85ed1c4 100644 --- a/docs/02-Kool-Cloud/03-Building-Images-to-Deploy.md +++ b/docs/02-Kool-Cloud/03-Building-Images-to-Deploy.md @@ -37,6 +37,19 @@ services: Your image will be built locally when running the `kool` CLI for a deploy and then pushed securely to the Kool.dev Cloud registry to a repository dedicated to your app environment. +### Build arguments (`args`) variables + +As stated above the args provided to Docker when building the image will come from the `services..build.args` configuration entry. + +It's a common need to have different values for different environments (i.e staging vs production). Kool Cloud supports two different ways for you to have a single `kool.cloud.yml` definition and still use different values per environment: + +- **Environment Variables**: we will parse environment variables before passing the build args to Docker so you can use the common syntax `FOO: "$FOO"` and the value will be interpolated to the current value of `FOO` when running the `kool cloud deploy` command. +- **Kool Cloud Environment Variables**: you can define the variables under the Environment on Kool.dev Cloud panel, and then use the special `FOO: {{FOO}}` syntax to have the value interpolated with the web panel managed value for `FOO`. + +#### Escaping variables + +If the value you want to use contains `$` sign that could lead to trouble on having the sign mistakenly parsed as a variable marker. To scape it you need to double it: `FOO=$$bar` will have the desired effect of getting the actual `$bar` string as the value of `FOO` environment variable. + ### Using a Private Registry You may already have or use your own private registry for handling images. You are welcome to hold the build process apart from the `kool cloud deploy` step and just use the already built images in your `kool.cloud.yml` file: diff --git a/docs/02-Kool-Cloud/05-Environment-Variables.md b/docs/02-Kool-Cloud/05-Environment-Variables.md index 3d81f7f..e2f088a 100644 --- a/docs/02-Kool-Cloud/05-Environment-Variables.md +++ b/docs/02-Kool-Cloud/05-Environment-Variables.md @@ -1,4 +1,4 @@ -Most applications and frameworks nowadays rely on environment variables to configure important aspects of their functions, mainly providing credentials and other secrets your app needs to work and access other resources. +Most applications and frameworks will rely on environment variables to configure important aspects of their functions, specially for providing credentials and other secrets your app needs to work and access other resources. Kool.dev Cloud supports a few different ways you can define your environment variables for a deploying container, so pick the one that best suits you.