-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"dagger.io/dagger" | ||
"github.com/aweris/gale/internal/dagger/images" | ||
"github.com/aweris/gale/pkg/config" | ||
"github.com/aweris/gale/pkg/gale" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) | ||
if err != nil { | ||
fmt.Printf("Error connecting to dagger: %v\n", err) | ||
os.Exit(1) | ||
} | ||
defer client.Close() | ||
|
||
container := images.RunnerBase(client) | ||
|
||
g, err := gale.New(&config.Config{}, client) | ||
if err != nil { | ||
fmt.Printf("Error creating gale: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
job, err := g.Job().Load(ctx, "example-golangci-lint", "golangci-lint") | ||
if err != nil { | ||
fmt.Printf("Error loading job: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
env := g.Env() | ||
|
||
_, err = container.With(gale.Load(env)).With(gale.Load(job)).Sync(ctx) | ||
if err != nil { | ||
fmt.Printf("Error syncing container: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/aweris/gale/tools/ghx/actions" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type CmdExecutor struct { | ||
command string // command to execute | ||
args []string // args to pass to the command | ||
env map[string]string // env to pass to the command as environment variables | ||
ac *actions.Context // ac is the action context | ||
ef map[string]*actions.EnvironmentFile // ef is the list of environment files | ||
} | ||
|
||
func NewCmdExecutorFromStepAction(sa *StepAction, entrypoint string) *CmdExecutor { | ||
ce := &CmdExecutor{env: make(map[string]string), ef: make(map[string]*actions.EnvironmentFile)} | ||
|
||
ce.command = "node" | ||
ce.args = append(ce.args, fmt.Sprintf("%s/%s", sa.Action.Path, entrypoint)) | ||
ce.ac = sa.ac | ||
|
||
// env | ||
for k, v := range sa.ac.Github.ToEnv() { | ||
ce.env[k] = v | ||
} | ||
|
||
for k, v := range sa.ac.Env.Env() { | ||
ce.env[k] = v | ||
} | ||
|
||
for k, v := range sa.Step.With { | ||
ce.env[fmt.Sprintf("INPUT_%s", strings.ToUpper(k))] = v | ||
} | ||
|
||
// add default values for inputs that are not defined in the step config | ||
for k, v := range sa.Action.Meta.Inputs { | ||
if _, ok := sa.Step.With[k]; ok { | ||
continue | ||
} | ||
|
||
if v.Default == "" { | ||
continue | ||
} | ||
|
||
ce.env[fmt.Sprintf("INPUT_%s", strings.ToUpper(k))] = v.Default | ||
} | ||
|
||
// TODO add step state to the environment | ||
|
||
return ce | ||
} | ||
|
||
func (c *CmdExecutor) Execute(_ context.Context) error { | ||
|
||
cmd := exec.Command(c.command, c.args...) | ||
|
||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
|
||
// load env files before setting the environment variables to the command. This is because the env files | ||
// are loaded from the environment variables and loadEnvFile function will override the environment variable | ||
// with the path of the file | ||
|
||
// TODO: replace this temp dir with a a step specific temp dir under runs/run-id/job-id/step-id | ||
dir := os.TempDir() | ||
|
||
if err := c.loadEnvFile(actions.EnvFileNameGithubEnv, filepath.Join(dir, "env")); err != nil { | ||
return err | ||
} | ||
|
||
if err := c.loadEnvFile(actions.EnvFileNameGithubPath, filepath.Join(dir, "path")); err != nil { | ||
return err | ||
} | ||
|
||
if err := c.loadEnvFile(actions.EnvFileNameGithubStepSummary, filepath.Join(dir, "step_summary")); err != nil { | ||
return err | ||
} | ||
|
||
if err := c.loadEnvFile(actions.EnvFileNameGithubActionOutput, filepath.Join(dir, "github_action_output")); err != nil { | ||
return err | ||
} | ||
|
||
env := os.Environ() | ||
|
||
for k, v := range c.env { | ||
// convert value to Evaluable String type | ||
str := actions.NewString(v) | ||
|
||
// evaluate the expression | ||
res, err := str.Eval(c.ac) | ||
if err != nil { | ||
return fmt.Errorf("failed to evaluate default value for input %s: %v", k, err) | ||
} | ||
|
||
env = append(env, fmt.Sprintf("%s=%s", k, res)) | ||
} | ||
|
||
cmd.Env = env | ||
|
||
return cmd.Run() | ||
} | ||
|
||
func (c *CmdExecutor) loadEnvFile(env, path string) error { | ||
ef, err := actions.NewEnvironmentFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.ef[env] = ef // add file to a map to process after the command execution | ||
c.env[env] = ef.Path // set the environment variable to the path of the file | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters