func icon indicating copy to clipboard operation
func copied to clipboard

Image Embedded Environment Variables do not work with buildpacks

Open andrinbr opened this issue 10 months ago • 2 comments

According to the documentation if I wanted to have a environment variable embedded in the image, I could use buildEnvs and pass them to the buildpack:

  • https://github.com/knative/func/blob/main/docs/reference/func_yaml.md#buildenvs
  • https://paketo.io/docs/howto/configuration/#image-embedded-environment-variables

My func.yaml

specVersion: 0.36.0
name: golang-buildpack-env
runtime: go
image: localhost/golang-test:pack
created: 2025-02-24T15:24:20.227225588+01:00
invoke: http
build:
  builder: pack
  buildEnvs:
  - name: BPE_COMMIT_SHA
    value: 'test123'

This is then dectected by the buildpack when buildung with func build -i localhost/golang-test:pack -v:

Provided Environment Variables
  BPE_COMMIT_SHA=test123

However the environment variable is not available (started with: podman run --rm -p 8080:8080 -it localhost/golang-test:pack):

Initializing HTTP function
listening on http port 8080
env test: ""
Received request
...

The test project is just a simple go function which prints the env var and returns the http request:

package function

import (
        "os"
        "strings"
        "fmt"
        "net/http"
        "net/http/httputil"
)

// Handle an HTTP Request.
func Handle(w http.ResponseWriter, r *http.Request) {
        envTest := strings.ToLower(os.Getenv("COMMIT_SHA"))
        fmt.Printf("env test: %q\n", envTest)

        dump, err := httputil.DumpRequest(r, true)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
        }

        fmt.Println("Received request")
        fmt.Printf("%q\n", dump)
        fmt.Fprintf(w, "%q", dump)
}

Used func cli version is v1.17.0 (tough the CLI says "v0.44.0")

andrinbr avatar Feb 25 '25 13:02 andrinbr

Is this for local or on-cluster build?

matejvasek avatar Mar 04 '25 13:03 matejvasek

This is with a local build with Podman v5.2.2 on RHEL 9.5

andrinbr avatar Mar 04 '25 14:03 andrinbr

This issue is stale because it has been open for 90 days with no activity. It will automatically close after 30 more days of inactivity. Reopen the issue with /reopen. Mark the issue as fresh by adding the comment /remove-lifecycle stale.

github-actions[bot] avatar Jun 04 '25 01:06 github-actions[bot]

Analogous to https://github.com/knative/func/issues/2719 I think it would be nice if this could be fixed.

andrinbr avatar Jul 14 '25 07:07 andrinbr