github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/buildpacks/env.go (about)

     1  /*
     2  Copyright 2020 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package buildpacks
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  
    23  	"github.com/buildpacks/pack/pkg/project"
    24  	"github.com/buildpacks/pack/pkg/project/types"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    30  )
    31  
    32  func GetEnv(a *latest.Artifact, mode config.RunMode) (map[string]string, error) {
    33  	artifact := a.BuildpackArtifact
    34  	workspace := a.Workspace
    35  
    36  	var projectDescriptor types.Descriptor
    37  	path := filepath.Join(workspace, artifact.ProjectDescriptor)
    38  	if util.IsFile(path) {
    39  		var err error
    40  		projectDescriptor, err = project.ReadProjectDescriptor(path)
    41  		if err != nil {
    42  			return nil, fmt.Errorf("failed to read project descriptor %q: %w", path, err)
    43  		}
    44  	}
    45  	return env(a, mode, projectDescriptor)
    46  }
    47  
    48  func env(a *latest.Artifact, mode config.RunMode, projectDescriptor types.Descriptor) (map[string]string, error) {
    49  	envVars, err := misc.EvaluateEnv(a.BuildpackArtifact.Env)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("unable to evaluate env variables: %w", err)
    52  	}
    53  
    54  	if mode == config.RunModes.Dev && a.Sync != nil && a.Sync.Auto != nil && *a.Sync.Auto {
    55  		envVars = append(envVars, "GOOGLE_DEVMODE=1")
    56  	}
    57  
    58  	env := envMap(envVars)
    59  	for _, kv := range projectDescriptor.Build.Env {
    60  		env[kv.Name] = kv.Value
    61  	}
    62  	env = addDefaultArgs(mode, env)
    63  	return env, nil
    64  }