Skip to content

Commit

Permalink
Add google.runtime_version label to final base image.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 670059789
Change-Id: Ic0dfb3e14509cd5f450cbeff5589155c1ffa5525
  • Loading branch information
YashBanka authored and copybara-github committed Sep 2, 2024
1 parent ba1567a commit 9641f14
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions builders/nodejs/acceptance/fah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func TestAcceptanceNodeJs(t *testing.T) {
Env: []string{"GOOGLE_NODEJS_VERSION=16.17.1"},
MustUse: []string{nodeRuntime},
},
{
// Tests addition of RUNTIME_VERSION label to the final image.
Name: "environment variable FIREBASE_OUTPUT_BUNDLE_DIR",
App: "simple",
Path: "/version?want=16.17.1",
Env: []string{"GOOGLE_NODEJS_VERSION=16.17.1", "FIREBASE_OUTPUT_BUNDLE_DIR=/output/dir"},
MustUse: []string{nodeRuntime},
},

// TODO(b/315008858) This should be reenabled once yarn support is re added
/*
Expand Down
1 change: 1 addition & 0 deletions cmd/nodejs/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ go_binary(
"-w",
],
deps = [
"//pkg/env",
"//pkg/gcpbuildpack",
"//pkg/nodejs",
"//pkg/ruby",
Expand Down
21 changes: 20 additions & 1 deletion cmd/nodejs/runtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ package main

import (
"fmt"
"os"

"github.com/GoogleCloudPlatform/buildpacks/pkg/env"
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
"github.com/GoogleCloudPlatform/buildpacks/pkg/nodejs"
"github.com/GoogleCloudPlatform/buildpacks/pkg/ruby"
"github.com/GoogleCloudPlatform/buildpacks/pkg/runtime"
)

const nodeLayer = "node"
const (
nodeLayer = "node"
runtimeVersionLabel = "runtime_version"
)

func main() {
gcp.Main(detectFn, buildFn)
Expand Down Expand Up @@ -67,6 +72,20 @@ func buildFn(ctx *gcp.Context) error {
if err != nil {
return err
}

if _, ok := os.LookupEnv(env.FirebaseOutputDir); ok {
osName := runtime.OSForStack(ctx)
latestAvailableVersion, err := runtime.ResolveVersion(ctx, runtime.Nodejs, version, osName)
if err != nil {
return fmt.Errorf("resolving version %s: %w", version, err)
}
majorVersion, err := nodejs.MajorVersion(latestAvailableVersion)
if err != nil {
return fmt.Errorf("getting major version for %s: %w", latestAvailableVersion, err)
}
ctx.AddLabel(runtimeVersionLabel, string(runtime.Nodejs)+majorVersion)
}

nrl, err := ctx.Layer(nodeLayer, gcp.BuildLayer, gcp.CacheLayer, gcp.LaunchLayerUnlessSkipRuntimeLaunch)
if err != nil {
return fmt.Errorf("creating %v layer: %w", nodeLayer, err)
Expand Down
9 changes: 9 additions & 0 deletions cmd/nodejs/runtime/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func TestDetect(t *testing.T) {
env: []string{"GOOGLE_RUNTIME=nodejs"},
want: 0,
},
{
name: "with FIREBASE_OUTPUT_BUNDLE_DIR env variable",
files: map[string]string{
"index.js": "",
"package.json": "",
},
env: []string{"GOOGLE_RUNTIME=nodejs", "FIREBASE_OUTPUT_BUNDLE_DIR=/output/dir"},
want: 0,
},
{
name: "with package and runtime set to python",
files: map[string]string{
Expand Down
3 changes: 3 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ const (

// RuntimeImageRegion is the region to fetch runtime images.
RuntimeImageRegion = "GOOGLE_RUNTIME_IMAGE_REGION"

// FirebaseOutputDir is the directory to store the firebase output bundle.
FirebaseOutputDir = "FIREBASE_OUTPUT_BUNDLE_DIR"
)

// IsGAE returns true if the buildpack target platform is gae.
Expand Down
10 changes: 10 additions & 0 deletions pkg/nodejs/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,13 @@ func parsePackageManager(packageManagerField string) (string, string, error) {
}
return packageManagerSplit[0], packageManagerSplit[1], nil
}

// MajorVersion returns the major version of a version string of format "major.minor.patch".
func MajorVersion(versionString string) (string, error) {
parts := strings.Split(versionString, ".")
if len(parts) < 3 {
return "", fmt.Errorf("invalid version format: %s", versionString)
}

return parts[0], nil
}

0 comments on commit 9641f14

Please sign in to comment.