-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
171 lines (155 loc) · 4.91 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package github
import (
"bytes"
"context"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/xxf098/actionflow/plan/github/model"
)
type actionStep interface {
step
getActionModel() *model.Action
// getCompositeRunContext(context.Context) *RunContext
// getCompositeSteps() *compositeSteps
}
func readActionImpl(ctx context.Context, actionDir string) (*model.Action, error) {
actionPath := path.Join(actionDir, "action.yml")
f, err := os.Open(actionPath)
if os.IsNotExist(err) {
f, err = os.Open(actionPath)
}
if err != nil {
return nil, err
}
defer f.Close()
return model.ReadAction(f)
}
func actionCacheDir() string {
var xdgCache string
var ok bool
if xdgCache, ok = os.LookupEnv("XDG_CACHE_HOME"); !ok || xdgCache == "" {
if home, err := homedir.Dir(); err == nil {
xdgCache = filepath.Join(home, ".cache")
} else if xdgCache, err = filepath.Abs("."); err != nil {
log.Fatal(err)
}
}
return filepath.Join(xdgCache, "flow")
}
func runActionImpl(ctx context.Context, step actionStep, actionDir string) error {
stepModel := step.getStepModel() // workflow.yml
action := step.getActionModel() // action.yml
actionPath := path.Join(actionDir, action.Runs.Main)
cmd := exec.CommandContext(ctx, "node", actionPath)
envs := setupActionEnv(ctx, step)
cmd.Env = append(cmd.Env, envs...)
log.Printf("type=%v actionDir=%s actionPath=%s\n envs=%s", stepModel.Type(), actionDir, actionPath, strings.Join(envs, " "))
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err := cmd.Run()
if err != nil {
log.Println(outBuf.String())
err = fmt.Errorf("%s: %s", err.Error(), errBuf.String())
}
// log.Println(outBuf.String())
return err
}
func runPostStep(ctx context.Context, step actionStep, actionDir string) error {
stepModel := step.getStepModel() // workflow.yml
action := step.getActionModel() // action.yml
actionPath := path.Join(actionDir, action.Runs.Main)
cmd := exec.CommandContext(ctx, "node", actionPath)
envs := setupActionEnv(ctx, step)
cmd.Env = append(cmd.Env, envs...)
log.Printf("type=%v actionDir=%s actionPath=%s\n envs=%s", stepModel.Type(), actionDir, actionPath, strings.Join(envs, " "))
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err := cmd.Run()
if err != nil {
log.Println(outBuf.String())
err = fmt.Errorf("%s: %s", err.Error(), errBuf.String())
}
// log.Println(outBuf.String())
return err
}
func setupActionEnv(ctx context.Context, step actionStep) []string {
// populateEnvsFromInput(ctx, step.getEnv(), step.getActionModel())
envs := []string{}
envs = setupGithubEnv(envs)
stepEnvs := step.getEnv()
populateEnvsFromInput(ctx, stepEnvs, step.getActionModel())
for k, v := range *stepEnvs {
// FIXME
if strings.HasPrefix(v, "${{") && strings.HasSuffix(v, "}}") {
continue
}
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
envs = setupRunnerEnv(envs)
return envs
}
func setupGithubEnv(envs []string) []string {
envs = append(envs, "CI=true")
envs = append(envs, "GITHUB_ACTIONS=true")
envs = append(envs, "GITHUB_SERVER_URL=https://github.com")
envs = append(envs, "GITHUB_API_URL=https://api.github.com")
envs = append(envs, "GITHUB_GRAPHQL_URL=https://api.github.com/graphql")
keys := []string{"GITHUB_ENV", "GITHUB_PATH", "GITHUB_WORKFLOW", "GITHUB_RUN_ID", "GITHUB_RUN_NUMBER", "GITHUB_REPOSITORY", "GITHUB_EVENT_NAME",
"GITHUB_EVENT_PATH", "GITHUB_WORKSPACE", "GITHUB_SHA", "GITHUB_REF", "GITHUB_REF_NAME", "GITHUB_REF_TYPE", "GITHUB_TOKEN", "GITHUB_JOB",
"GITHUB_REPOSITORY_OWNER", "GITHUB_RETENTION_DAYS", "RUNNER_PERFLOG", "RUNNER_TRACKING_ID", "XDG_CONFIG_HOME"}
for _, key := range keys {
envs = append(envs, fmt.Sprintf("%s=%s", key, os.Getenv(key)))
}
return envs
}
func setupRunnerEnv(envs []string) []string {
if !checkEnv(envs, "RUNNER_TOOL_CACHE") {
envs = append(envs, fmt.Sprintf("%s=%s", "RUNNER_TOOL_CACHE", "/opt/hostedtoolcache"))
}
if !checkEnv(envs, "RUNNER_OS") {
envs = append(envs, fmt.Sprintf("%s=%s", "RUNNER_OS", "Linux"))
}
if !checkEnv(envs, "RUNNER_ARCH") {
goarch := runtime.GOARCH
if goarch == "amd64" {
goarch = "x64"
}
if goarch == "386" {
goarch = "x86"
}
envs = append(envs, fmt.Sprintf("%s=%s", "RUNNER_ARCH", goarch))
}
if !checkEnv(envs, "RUNNER_TEMP") {
envs = append(envs, fmt.Sprintf("%s=%s", "RUNNER_TEMP", "/tmp"))
}
return envs
}
func checkEnv(envs []string, key string) bool {
for _, v := range envs {
if strings.HasPrefix(v, key+"=") {
return true
}
}
return false
}
// setup input
// FIXME eval
func populateEnvsFromInput(ctx context.Context, env *map[string]string, action *model.Action) {
for inputID, input := range action.Inputs {
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_")
envKey = fmt.Sprintf("INPUT_%s", envKey)
if _, ok := (*env)[envKey]; !ok {
(*env)[envKey] = input.Default
}
}
}