Skip to content

Commit cbe147f

Browse files
authored
Merge pull request #1853 from FabianKramm/master
fix: fixed runtime var issue in after:build hook
2 parents c5f488b + 36e7b8e commit cbe147f

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
lines changed

e2e/tests/build/build.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,17 @@ var _ = DevSpaceDescribe("build", func() {
6666
imageList, err := dockerClient.ImageList(ctx, types.ImageListOptions{})
6767
framework.ExpectNoError(err)
6868

69+
found := false
70+
Outer:
6971
for _, image := range imageList {
70-
if image.RepoTags[0] == "my-docker-username/helloworld:latest" {
71-
err = nil
72-
break
73-
} else {
74-
err = errors.New("image not found")
72+
for _, tag := range image.RepoTags {
73+
if tag == "my-docker-username/helloworld:latest" {
74+
found = true
75+
break Outer
76+
}
7577
}
7678
}
77-
framework.ExpectNoError(err)
79+
framework.ExpectEqual(found, true, "image not found in cache")
7880
})
7981

8082
ginkgo.It("should build dockerfile with buildkit", func() {

e2e/tests/config/config.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88

9+
"github.com/loft-sh/devspace/pkg/devspace/config/generated"
910
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable"
1011

1112
"github.com/loft-sh/devspace/cmd"
@@ -15,6 +16,7 @@ import (
1516

1617
"github.com/loft-sh/devspace/cmd/use"
1718
"github.com/loft-sh/devspace/e2e/framework"
19+
"github.com/loft-sh/devspace/e2e/kube"
1820
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
1921
"github.com/loft-sh/devspace/pkg/util/survey"
2022
"github.com/onsi/ginkgo"
@@ -29,32 +31,45 @@ var _ = DevSpaceDescribe("config", func() {
2931

3032
// create a new factory
3133
var (
32-
f *framework.DefaultFactory
34+
f *framework.DefaultFactory
35+
kubeClient *kube.KubeHelper
3336
)
3437

3538
ginkgo.BeforeEach(func() {
3639
f = framework.NewDefaultFactory()
40+
41+
kubeClient, err = kube.NewKubeHelper()
42+
framework.ExpectNoError(err)
3743
})
3844

3945
ginkgo.It("should resolve runtime environment variables correctly", func() {
4046
tempDir, err := framework.CopyToTempDir("tests/config/testdata/runtime-variables")
4147
framework.ExpectNoError(err)
4248
defer framework.CleanupTempDir(initialDir, tempDir)
4349

44-
configBuffer := &bytes.Buffer{}
45-
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{
4658
GlobalFlags: &flags.GlobalFlags{
47-
Namespace: "test-ns",
59+
Namespace: ns,
4860
},
49-
Out: configBuffer,
50-
SkipInfo: true,
5161
}
5262

5363
err = printCmd.Run(f)
5464
framework.ExpectNoError(err)
5565
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out.txt"), "test-testimage-latest-dep1")
5666
framework.ExpectLocalFileContentsImmediately(filepath.Join(tempDir, "out2.txt"), "Done")
57-
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)
5873
})
5974

6075
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)