Skip to content

Commit

Permalink
feat(zbpack/image): Resolve the envvar first
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Jul 6, 2023
1 parent f98b3ac commit d3f986a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions pkg/zeaburpack/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"sort"
"strconv"
"strings"

"github.com/zeabur/zbpack/pkg/env"
)

type buildImageOptions struct {
Expand All @@ -22,6 +24,9 @@ type buildImageOptions struct {
}

func buildImage(opt *buildImageOptions) error {
// resolve env variable statically and don't depend on Dockerfile's order
resolvedVars := env.ResolveEnvVariable(opt.UserVars)

lines := strings.Split(opt.Dockerfile, "\n")
stageLines := []int{}
for i, line := range lines {
Expand All @@ -30,17 +35,24 @@ func buildImage(opt *buildImageOptions) error {
}
}

var userVarsKeys []string
for key := range opt.UserVars {
userVarsKeys = append(userVarsKeys, key)
// sort the resolvedVars by key so we can build
// the reproducible dockerfile
sortedResolvedVarsKey := make([]string, 0, len(resolvedVars))
for key := range resolvedVars {
sortedResolvedVarsKey = append(sortedResolvedVarsKey, key)
}
sort.Strings(userVarsKeys)
sort.Strings(sortedResolvedVarsKey)

// build the dockerfile
dockerfileEnv := ""
for _, key := range userVarsKeys {
value := opt.UserVars[key]
for _, key := range sortedResolvedVarsKey {
value := resolvedVars[key]

// skip empty value
if len(value) == 0 {
continue
}

dockerfileEnv += "ENV " + key + " " + value + "\n"
}

Expand Down

0 comments on commit d3f986a

Please sign in to comment.