Skip to content

Commit

Permalink
enhance(compiler): environment and template adjustments for local com…
Browse files Browse the repository at this point in the history
…pilation (#983)
  • Loading branch information
ecrupper authored Oct 6, 2023
1 parent ae4356a commit 174ab52
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 38 deletions.
61 changes: 30 additions & 31 deletions compiler/native/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ func (c *client) EnvironmentStep(s *yaml.Step, stageEnv raw.StringSliceMap) (*ya
// gather set of default environment variables
defaultEnv := environment(c.build, c.metadata, c.repo, c.user)

// check if the compiler is setup for a local pipeline
// and the step isn't setup to run in a detached state
if c.local && !s.Detach {
// capture all environment variables from the local environment
for _, e := range os.Environ() {
// split the environment variable on = into a key value pair
parts := strings.SplitN(e, "=", 2)

env[parts[0]] = parts[1]
}
}

// inject the declared stage environment
// WARNING: local env can override global + stage
env = appendMap(env, stageEnv)
Expand All @@ -120,6 +108,17 @@ func (c *client) EnvironmentStep(s *yaml.Step, stageEnv raw.StringSliceMap) (*ya
env[k] = v
}

// check if the compiler is setup for a local pipeline
if c.local && !s.Detach {
// capture all environment variables from the local environment
for _, e := range os.Environ() {
// split the environment variable on = into a key value pair
parts := strings.SplitN(e, "=", 2)

env[parts[0]] = parts[1]
}
}

// inject the declared parameter
// variables to the build step
for k, v := range s.Parameters {
Expand Down Expand Up @@ -192,17 +191,6 @@ func (c *client) EnvironmentSecrets(s yaml.SecretSlice, globalEnv raw.StringSlic
// gather set of default environment variables
defaultEnv := environment(c.build, c.metadata, c.repo, c.user)

// check if the compiler is setup for a local pipeline
if c.local {
// capture all environment variables from the local environment
for _, e := range os.Environ() {
// split the environment variable on = into a key value pair
parts := strings.SplitN(e, "=", 2)

env[parts[0]] = parts[1]
}
}

// inject the declared global environment
// WARNING: local env can override global
env = appendMap(env, globalEnv)
Expand All @@ -221,6 +209,17 @@ func (c *client) EnvironmentSecrets(s yaml.SecretSlice, globalEnv raw.StringSlic
env[k] = v
}

// check if the compiler is setup for a local pipeline
if c.local {
// capture all environment variables from the local environment
for _, e := range os.Environ() {
// split the environment variable on = into a key value pair
parts := strings.SplitN(e, "=", 2)

env[parts[0]] = parts[1]
}
}

// inject the declared parameter
// variables to the build secret
for k, v := range secret.Origin.Parameters {
Expand Down Expand Up @@ -250,6 +249,14 @@ func (c *client) EnvironmentBuild() map[string]string {
// gather set of default environment variables
defaultEnv := environment(c.build, c.metadata, c.repo, c.user)

// inject the default environment
// variables to the build
// we do this after injecting the declared environment
// to ensure the default env overrides any conflicts
for k, v := range defaultEnv {
env[k] = v
}

// check if the compiler is setup for a local pipeline
if c.local {
// capture all environment variables from the local environment
Expand All @@ -261,14 +268,6 @@ func (c *client) EnvironmentBuild() map[string]string {
}
}

// inject the default environment
// variables to the build secret
// we do this after injecting the declared environment
// to ensure the default env overrides any conflicts
for k, v := range defaultEnv {
env[k] = v
}

return env
}

Expand Down
33 changes: 32 additions & 1 deletion compiler/native/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package native
import (
"flag"
"reflect"
"strings"
"testing"

"github.com/go-vela/types/raw"
Expand Down Expand Up @@ -208,17 +209,47 @@ func TestNative_EnvironmentSteps(t *testing.T) {
},
}

// run test
// run test non-local
compiler, err := New(c)
if err != nil {
t.Errorf("Unable to create new compiler: %v", err)
}

// run test local
compiler.WithLocal(true)

t.Setenv("VELA_BUILD_COMMIT", "123abc")

got, err := compiler.EnvironmentSteps(s, e)
if err != nil {
t.Errorf("EnvironmentSteps returned err: %v", err)
}

// cannot use complete diff since local compiler pulls from OS env
if !strings.EqualFold(got[0].Environment["VELA_BUILD_COMMIT"], "123abc") {
t.Errorf("EnvironmentSteps with local compiler should have set VELA_BUILD_COMMIT to 123abc, got %s", got[0].Environment["VELA_BUILD_COMMIT"])
}

// test without local
compiler.WithLocal(false)

// reset s
s = yaml.StepSlice{
&yaml.Step{
Image: "alpine",
Name: str,
Pull: "always",
Environment: raw.StringSliceMap{
"BUILD_CHANNEL": "foo",
},
},
}

got, err = compiler.EnvironmentSteps(s, e)
if err != nil {
t.Errorf("EnvironmentSteps returned err: %v", err)
}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("EnvironmentSteps mismatch (-want +got):\n%s", diff)
}
Expand Down
27 changes: 21 additions & 6 deletions compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,19 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) {

switch {
case c.local:
a := &afero.Afero{
Fs: afero.NewOsFs(),
}

// iterate over locally provided templates
for _, t := range c.localTemplates {
parts := strings.Split(t, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("local templates must be provided in the form <name>:<path>, got %s", t)
}

// if local template has a match, read file path provided
if strings.EqualFold(tmpl.Name, parts[0]) {
a := &afero.Afero{
Fs: afero.NewOsFs(),
}

bytes, err = a.ReadFile(parts[1])
if err != nil {
return bytes, err
Expand All @@ -225,8 +226,22 @@ func (c *client) getTemplate(tmpl *yaml.Template, name string) ([]byte, error) {
}
}

// no template found in provided templates, exit with error
return nil, fmt.Errorf("unable to find template %s: not supplied in list %s", tmpl.Name, c.localTemplates)
// file type templates can be retrieved locally using `source`
if strings.EqualFold(tmpl.Type, "file") {
bytes, err = a.ReadFile(tmpl.Source)
if err != nil {
return nil, fmt.Errorf("unable to read file for template %s. `File` type templates must be located at `source` or supplied to local template files", tmpl.Name)
}

return bytes, nil
}

// local exec may still request remote templates
if !strings.EqualFold(tmpl.Type, "github") {
return nil, fmt.Errorf("unable to find template %s: not supplied in list %s", tmpl.Name, c.localTemplates)
}

fallthrough

case strings.EqualFold(tmpl.Type, "github"):
// parse source from template
Expand Down

0 comments on commit 174ab52

Please sign in to comment.