Skip to content

Commit

Permalink
Always write launch metadata
Browse files Browse the repository at this point in the history
- We need to do this even if the node_modules are not rebuilt

[#164992442]

Co-authored-by: Daniel Thornton <[email protected]>
  • Loading branch information
Tyler Phelan and dwillist committed Apr 1, 2019
1 parent 2078dcb commit 0518ca6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
42 changes: 42 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package integration

import (
"bytes"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"testing"

"github.com/cloudfoundry/dagger"
Expand Down Expand Up @@ -75,4 +80,41 @@ func testIntegration(t *testing.T, when spec.G, it spec.S) {
Expect(body).To(ContainSubstring("Package A value 2"))
})
})

when("the app is pushed twice", func() {
it("does not reinstall node_modules", func() {
appDir := filepath.Join("testdata", "simple_app")
app, err := dagger.PackBuild(appDir, nodeBP, bp)
Expect(err).ToNot(HaveOccurred())
defer app.Destroy()

Expect(app.BuildLogs()).To(MatchRegexp("node_modules .*: Contributing to layer"))

buildLogs := &bytes.Buffer{}

// TODO: Move this to dagger

_, imageID, _, err := app.Info()
Expect(err).NotTo(HaveOccurred())

cmd := exec.Command("pack", "build", imageID, "--builder", "cfbuildpacks/cflinuxfs3-cnb-test-builder", "--clear-cache", "--buildpack", nodeBP, "--buildpack", bp)
cmd.Dir = appDir
cmd.Stdout = io.MultiWriter(os.Stdout, buildLogs)
cmd.Stderr = io.MultiWriter(os.Stderr, buildLogs)
Expect(cmd.Run()).To(Succeed())

const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"

re := regexp.MustCompile(ansi)
strippedLogs := re.ReplaceAllString(buildLogs.String(), "")

Expect(strippedLogs).To(MatchRegexp("node_modules .*: Reusing cached layer"))
Expect(strippedLogs).NotTo(MatchRegexp("node_modules .*: Contributing to layer"))

Expect(app.Start()).To(Succeed())

_, _, err = app.HTTPGet("/")
Expect(err).NotTo(HaveOccurred())
})
})
}
25 changes: 8 additions & 17 deletions modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewContributor(context build.Build, pkgManager PackageManager) (Contributor
}

func (c Contributor) Contribute() error {
return c.modulesLayer.Contribute(c.Metadata, func(layer layers.Layer) error {
if err := c.modulesLayer.Contribute(c.Metadata, func(layer layers.Layer) error {
offlineCache := filepath.Join(c.app.Root, "npm-packages-offline-cache")

modulesDir := filepath.Join(c.modulesLayer.Root, yarn.ModulesDir)
Expand All @@ -109,15 +109,6 @@ func (c Contributor) Contribute() error {
return fmt.Errorf("unable make layer: %s", err.Error())
}

//nodeModules := filepath.Join(c.app.Root, yarn.ModulesDir)
//if err := helper.CopyDirectory(nodeModules, filepath.Join(layer.Root, yarn.ModulesDir)); err != nil {
// return fmt.Errorf(`unable to copy "%s" to "%s": %s`, nodeModules, layer.Root, err.Error())
//}

//if err := os.RemoveAll(nodeModules); err != nil {
// return fmt.Errorf("unable to remove node_modules from the app dir: %s", err.Error())
//}

yarnCache := filepath.Join(c.app.Root, yarn.CacheDir)
if err := helper.CopyDirectory(yarnCache, filepath.Join(layer.Root, yarn.CacheDir)); err != nil {
return fmt.Errorf(`unable to copy "%s" to "%s": %s`, yarnCache, layer.Root, err.Error())
Expand All @@ -135,14 +126,14 @@ func (c Contributor) Contribute() error {
return err
}

if err := layer.OverrideSharedEnv("npm_config_nodedir", os.Getenv("NODE_HOME")); err != nil {
return err
}
return layer.OverrideSharedEnv("npm_config_nodedir", os.Getenv("NODE_HOME"))
}, c.flags()...); err != nil {
return err
}

return c.launchLayer.WriteApplicationMetadata(layers.Metadata{
Processes: []layers.Process{{"web", "yarn start"}},
})
}, c.flags()...)
return c.launchLayer.WriteApplicationMetadata(layers.Metadata{
Processes: []layers.Process{{"web", "yarn start"}},
})
}

func (c Contributor) flags() []layers.Flag {
Expand Down
14 changes: 8 additions & 6 deletions scripts/install_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if [ "$#" -eq 1 ]; then
fi

install_pack() {
if [[ -f ".bin/pack" ]]; then return 0; fi

OS=$(uname -s)

if [[ $OS == "Darwin" ]]; then
Expand Down Expand Up @@ -75,18 +77,18 @@ expand() {
rm $PACK_ARTIFACT
}

configure_pack() {
pack add-stack org.cloudfoundry.stacks.cflinuxfs3 \
--build-image cfbuildpacks/cflinuxfs3-cnb-experimental:build \
--run-image cfbuildpacks/cflinuxfs3-cnb-experimental:run || echo "Ignoring add stack error"
}
#configure_pack() {
# pack add-stack org.cloudfoundry.stacks.cflinuxfs3 \
# --build-image cfbuildpacks/cflinuxfs3-cnb-experimental:build \
# --run-image cfbuildpacks/cflinuxfs3-cnb-experimental:run || echo "Ignoring add stack error"
#}

cd "$( dirname "${BASH_SOURCE[0]}" )/.."

mkdir -p .bin
export PATH=$(pwd)/.bin:$PATH

install_pack
configure_pack
#configure_pack
install_packager

0 comments on commit 0518ca6

Please sign in to comment.