Skip to content

Commit 908be53

Browse files
authored
feat(container): add build-args to deploy workflow (#2941)
1 parent 16b666e commit 908be53

10 files changed

+1626
-1133
lines changed

cmd/scw/testdata/test-all-usage-container-deploy-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ARGS:
1010
[dockerfile=Dockerfile] Path to the Dockerfile
1111
[build-source=.] Path to the build context
1212
[cache=true] Use cache when building the image
13+
[build-args.{key}] Build-time variables
1314
[port=8080] Port to expose
1415
[namespace-id] Container Namespace ID to deploy to
1516
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw | all)

docs/commands/container.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ scw container deploy [arg=value ...]
351351
| dockerfile | Default: `Dockerfile` | Path to the Dockerfile |
352352
| build-source | Default: `.` | Path to the build context |
353353
| cache | Default: `true` | Use cache when building the image |
354+
| build-args.{key} | | Build-time variables |
354355
| port | Default: `8080` | Port to expose |
355356
| namespace-id | | Container Namespace ID to deploy to |
356357
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw`, `all` | Region to target. If none is passed will use default region from the config |

internal/namespaces/container/v1beta1/custom_deploy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type containerDeployRequest struct {
3232
Dockerfile string
3333
BuildSource string
3434
Cache bool
35+
BuildArgs map[string]*string
3536

3637
NamespaceID *string
3738
Port uint32
@@ -65,6 +66,11 @@ func containerDeployCommand() *core.Command {
6566
Short: "Use cache when building the image",
6667
Default: core.DefaultValueSetter("true"),
6768
},
69+
{
70+
Name: "build-args.{key}",
71+
Short: "Build-time variables",
72+
Required: false,
73+
},
6874
{
6975
Name: "port",
7076
Short: "Port to expose",
@@ -232,6 +238,7 @@ func DeployStepBuildImage(t *tasks.Task, data *DeployStepPackImageResponse) (*De
232238
Dockerfile: data.Args.Dockerfile,
233239
Tags: []string{tag},
234240
NoCache: !data.Args.Cache,
241+
BuildArgs: data.Args.BuildArgs,
235242
})
236243
if err != nil {
237244
return nil, fmt.Errorf("could not build image: %w", errors.Unwrap(err))

internal/namespaces/container/v1beta1/custom_deploy_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ var (
3030
FROM nginx:alpine
3131
RUN apk add --no-cache curl git bash
3232
COPY ./index.html /usr/share/nginx/html/index.html
33+
EXPOSE 80
34+
`)
35+
nginxDockerfileWithBuildArgs = strings.TrimSpace(`
36+
FROM nginx:alpine
37+
RUN apk add --no-cache curl git bash
38+
COPY ./index.html /usr/share/nginx/html/index.html
39+
ARG TEST
40+
RUN test -n "$TEST"
3341
EXPOSE 80
3442
`)
3543
)
@@ -112,6 +120,38 @@ func Test_Deploy(t *testing.T) {
112120
),
113121
DisableParallel: true,
114122
}))
123+
124+
t.Run("Build args", core.Test(&core.TestConfig{
125+
Commands: commands,
126+
BeforeFunc: core.BeforeFuncCombine(
127+
func(ctx *core.BeforeFuncCtx) error {
128+
// Create index.html
129+
err := os.WriteFile(filepath.Join(path, "index.html"), []byte(indexHTML), 0600)
130+
if err != nil {
131+
return err
132+
}
133+
return nil
134+
},
135+
func(ctx *core.BeforeFuncCtx) error {
136+
// Create Dockerfile
137+
err := os.WriteFile(filepath.Join(path, "Dockerfile"), []byte(nginxDockerfileWithBuildArgs), 0600)
138+
if err != nil {
139+
return err
140+
}
141+
return nil
142+
},
143+
),
144+
Cmd: fmt.Sprintf("scw container deploy name=%s build-source=%s port=80 build-args.TEST=thisisatest", appName, path),
145+
Check: core.TestCheckCombine(
146+
core.TestCheckGolden(),
147+
core.TestCheckExitCode(0),
148+
),
149+
AfterFunc: core.AfterFuncCombine(
150+
testDeleteContainersNamespaceAfter(appName),
151+
testDeleteRegistryAfter(appName),
152+
),
153+
DisableParallel: true,
154+
}))
115155
}
116156

117157
func testDeleteContainersNamespaceAfter(appName string) func(*core.AfterFuncCtx) error {
@@ -137,7 +177,7 @@ func testDeleteContainersNamespaceAfter(appName string) func(*core.AfterFuncCtx)
137177
return fmt.Errorf("namespace not found")
138178
}
139179

140-
return core.ExecAfterCmd(fmt.Sprintf("scw container namespace delete %s", namespaceID))(ctx)
180+
return core.ExecAfterCmd(fmt.Sprintf("scw container namespace delete %s --wait", namespaceID))(ctx)
141181
}
142182
}
143183

internal/namespaces/container/v1beta1/testdata/test-deploy-app-name-deduced-from-path.cassette.yaml

Lines changed: 187 additions & 1003 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
22
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3-
Your application is now available at https://appclitestdeployponelvzngnoz-app-cli-test-deploy-poney.functions.fnc.fr-par.scw.cloud
3+
Your application is now available at https://appclitestdeployponewm9y7zuv-app-cli-test-deploy-poney.functions.fnc.fr-par.scw.cloud
44

55
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
6-
"Your application is now available at https://appclitestdeployponelvzngnoz-app-cli-test-deploy-poney.functions.fnc.fr-par.scw.cloud\n"
6+
"Your application is now available at https://appclitestdeployponewm9y7zuv-app-cli-test-deploy-poney.functions.fnc.fr-par.scw.cloud\n"

internal/namespaces/container/v1beta1/testdata/test-deploy-build-args.cassette.yaml

Lines changed: 1040 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
Your application is now available at https://clitestcontainerdeplattc7cav-cli-test-container-deploy.functions.fnc.fr-par.scw.cloud
4+
5+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
6+
"Your application is now available at https://clitestcontainerdeplattc7cav-cli-test-container-deploy.functions.fnc.fr-par.scw.cloud\n"

0 commit comments

Comments
 (0)