Skip to content

Commit 6511719

Browse files
authored
Merge pull request #509 from kool-dev/build-args-envs
Build args environment variables support
2 parents 9b31f94 + 3917d20 commit 6511719

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docs/02-Kool-Cloud/03-Building-Images-to-Deploy.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ services:
3737
3838
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.
3939

40+
### Build arguments (`args`) variables
41+
42+
As stated above the args provided to Docker when building the image will come from the `services.<service>.build.args` configuration entry.
43+
44+
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:
45+
46+
- **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.
47+
- **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`.
48+
49+
#### Escaping variables
50+
51+
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.
52+
4053
### Using a Private Registry
4154

4255
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:

docs/02-Kool-Cloud/05-Environment-Variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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.
1+
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.
22

33
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.
44

services/cloud/build.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"kool-dev/kool/core/environment"
99
"kool-dev/kool/core/shell"
1010
"kool-dev/kool/services/cloud/api"
11+
"os"
1112
"path/filepath"
1213
"strings"
1314

@@ -51,7 +52,7 @@ func BuildPushImageForDeploy(service string, config *DeployConfigService, deploy
5152

5253
if buildConfig.Args != nil {
5354
for k, v := range *buildConfig.Args {
54-
dockerBuild.AppendArgs("--build-arg", fmt.Sprintf("%s=%s", k, v))
55+
dockerBuild.AppendArgs("--build-arg", fmt.Sprintf("%s=%s", k, parseDeployEnvs(v, deploy.Deploy.Environment.Env)))
5556
}
5657
}
5758

@@ -124,3 +125,23 @@ func parseBuild(build interface{}) (config *DeployConfigBuild, err error) {
124125
err = yaml.Unmarshal(b, config)
125126
return
126127
}
128+
129+
func parseDeployEnvs(i interface{}, env interface{}) string {
130+
// workaround to allow for escaping $ with a double $$
131+
_ = os.Setenv("$", "$")
132+
133+
var value = os.ExpandEnv(fmt.Sprintf("%s", i))
134+
135+
if strings.Contains(value, "{{") && strings.Contains(value, "}}") {
136+
// we have something to replace!
137+
if recs, ok := env.(map[string]interface{}); ok {
138+
for k, v := range recs {
139+
if strings.Contains(value, "{{"+k+"}}") {
140+
value = strings.ReplaceAll(value, "{{"+k+"}}", fmt.Sprintf("%v", v))
141+
}
142+
}
143+
}
144+
}
145+
146+
return value
147+
}

0 commit comments

Comments
 (0)