Skip to content

Commit fa301fd

Browse files
committed
fix: fixed runtime var issue in after:build hook
1 parent d478d58 commit fa301fd

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

e2e/tests/config/config.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ package config
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable"
76
"os"
87
"path/filepath"
98

9+
"github.com/loft-sh/devspace/pkg/devspace/config/generated"
10+
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable"
11+
1012
"github.com/loft-sh/devspace/cmd"
1113
"github.com/loft-sh/devspace/cmd/flags"
1214
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1315
"gopkg.in/yaml.v2"
1416

1517
"github.com/loft-sh/devspace/cmd/use"
1618
"github.com/loft-sh/devspace/e2e/framework"
19+
"github.com/loft-sh/devspace/e2e/kube"
1720
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
1821
"github.com/loft-sh/devspace/pkg/util/survey"
1922
"github.com/onsi/ginkgo"
@@ -28,32 +31,45 @@ var _ = DevSpaceDescribe("config", func() {
2831

2932
// create a new factory
3033
var (
31-
f *framework.DefaultFactory
34+
f *framework.DefaultFactory
35+
kubeClient *kube.KubeHelper
3236
)
3337

3438
ginkgo.BeforeEach(func() {
3539
f = framework.NewDefaultFactory()
40+
41+
kubeClient, err = kube.NewKubeHelper()
42+
framework.ExpectNoError(err)
3643
})
3744

38-
ginkgo.It("should resolve runtime environment variables correctly", func() {
45+
ginkgo.FIt("should resolve runtime environment variables correctly", func() {
3946
tempDir, err := framework.CopyToTempDir("tests/config/testdata/runtime-variables")
4047
framework.ExpectNoError(err)
4148
defer framework.CleanupTempDir(initialDir, tempDir)
4249

43-
configBuffer := &bytes.Buffer{}
44-
printCmd := &cmd.PrintCmd{
50+
ns, err := kubeClient.CreateNamespace("config")
51+
framework.ExpectNoError(err)
52+
defer func() {
53+
err := kubeClient.DeleteNamespace(ns)
54+
framework.ExpectNoError(err)
55+
}()
56+
57+
printCmd := &cmd.DeployCmd{
4558
GlobalFlags: &flags.GlobalFlags{
46-
Namespace: "test-ns",
59+
Namespace: ns,
4760
},
48-
Out: configBuffer,
49-
SkipInfo: true,
5061
}
5162

5263
err = printCmd.Run(f)
5364
framework.ExpectNoError(err)
5465
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out.txt"), "test-testimage-latest-dep1")
5566
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out2.txt"), "Done")
56-
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out3.txt"), "test-ns-resolved-${NOT_RESOLVED}")
67+
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out3.txt"), ns+"-resolved-${NOT_RESOLVED}")
68+
69+
// read the generated.yaml
70+
config, err := generated.NewConfigLoader("").Load()
71+
framework.ExpectNoError(err)
72+
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out0.txt"), "my-docker-username/helloworld2:"+config.GetActive().GetImageCache("app-test").Tag)
5773
})
5874

5975
ginkgo.It("should load multiple profiles in order via --profile", func() {

e2e/tests/config/testdata/runtime-variables/dep1.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ hooks:
1414
- name: test-123
1515
command: |-
1616
echo ${runtime.dependencies.dep2.images.app.image}
17-
events: ["after:resolveDependencies"]
17+
events: ["after:deployDependencies"]

e2e/tests/config/testdata/runtime-variables/devspace.yaml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,38 @@ dependencies:
1212
source:
1313
path: ./dep1.yaml
1414
images:
15+
aap-test:
16+
image: my-docker-username/helloworld3
17+
build:
18+
docker:
19+
skipPush: true
20+
app-test:
21+
image: my-docker-username/helloworld2
22+
build:
23+
docker:
24+
skipPush: true
1525
app:
1626
image: my-docker-username/helloworld
1727
tags:
1828
- latest
1929
build:
2030
disabled: true
2131
hooks:
32+
- name: test-012
33+
command: |-
34+
echo -n ${runtime.images.app-test.image}:${runtime.images.app-test.tag} > out0.txt
35+
events: ["after:build:app-test"]
2236
- name: test-123
2337
command: |-
2438
# This should print test-testimage-latest-dep1
2539
echo -n $(echo -n "${OTHER}-${runtime.dependencies.dep1.hooks.test-123.stdout}-${runtime.images.app.tag}-${runtime.dependencies.dep1.images.app.tag}") > out.txt
2640
echo -n "Done"
27-
events: ["after:resolveDependencies"]
41+
events: ["after:deployDependencies"]
2842
- name: test-124
2943
command: |-
3044
# This should print Done
3145
echo -n ${runtime.hooks.test-123.stdout} > out2.txt
32-
events: ["after:resolveDependencies"]
46+
events: ["after:deployDependencies"]
3347
- name: test-125
3448
command: $(cat command.txt)
35-
events: ["after:resolveDependencies"]
49+
events: ["after:deployDependencies"]

pkg/devspace/build/build.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/loft-sh/devspace/pkg/devspace/config"
8+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
89
"github.com/loft-sh/devspace/pkg/devspace/dependency/types"
910
"github.com/loft-sh/devspace/pkg/util/scanner"
1011

@@ -20,6 +21,8 @@ type imageNameAndTag struct {
2021
imageConfigName string
2122
imageName string
2223
imageTag string
24+
imageTags []string
25+
imageConfig latest.ImageConfig
2326
}
2427

2528
// Options describe how images should be build
@@ -246,22 +249,13 @@ func (c *controller) Build(options *Options, log logpkg.Logger) (map[string]stri
246249
return
247250
}
248251

249-
// Execute plugin hook
250-
pluginErr := hook.ExecuteHooks(c.client, c.config, c.dependencies, map[string]interface{}{
251-
"IMAGE_CONFIG_NAME": imageConfigName,
252-
"IMAGE_NAME": imageName,
253-
"IMAGE_CONFIG": cImageConf,
254-
"IMAGE_TAGS": imageTags,
255-
}, log, hook.EventsForSingle("after:build", imageConfigName).With("build.afterBuild")...)
256-
if pluginErr != nil {
257-
errChan <- pluginErr
258-
}
259-
260252
// Send the response
261253
cacheChan <- imageNameAndTag{
262254
imageConfigName: imageConfigName,
263255
imageName: imageName,
264256
imageTag: imageTags[0],
257+
imageTags: imageTags,
258+
imageConfig: cImageConf,
265259
}
266260
}()
267261
}
@@ -306,6 +300,17 @@ func (c *controller) waitForBuild(errChan <-chan error, cacheChan <-chan imageNa
306300

307301
// Track built images
308302
builtImages[done.imageName] = done.imageTag
303+
304+
// Execute plugin hook
305+
pluginErr := hook.ExecuteHooks(c.client, c.config, c.dependencies, map[string]interface{}{
306+
"IMAGE_CONFIG_NAME": done.imageConfigName,
307+
"IMAGE_NAME": done.imageName,
308+
"IMAGE_CONFIG": done.imageConfig,
309+
"IMAGE_TAGS": done.imageTags,
310+
}, log, hook.EventsForSingle("after:build", done.imageConfigName).With("build.afterBuild")...)
311+
if pluginErr != nil {
312+
return pluginErr
313+
}
309314
}
310315

311316
return nil

0 commit comments

Comments
 (0)