2019-04-06 19:32:14 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-17 07:42:42 +00:00
|
|
|
"fmt"
|
2019-04-06 19:32:14 +00:00
|
|
|
"io"
|
2020-11-17 07:42:42 +00:00
|
|
|
"io/ioutil"
|
2021-12-13 18:51:53 +00:00
|
|
|
"os"
|
2019-04-06 19:32:14 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2019-09-14 12:21:16 +00:00
|
|
|
"github.com/drone/envsubst"
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline"
|
2022-02-26 02:02:42 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend"
|
|
|
|
backendTypes "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/compiler"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/linter"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/matrix"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/multipart"
|
2022-02-28 08:27:31 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the exec command.
|
2021-10-27 19:03:14 +00:00
|
|
|
var Command = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "exec",
|
|
|
|
Usage: "execute a local build",
|
2021-10-02 08:59:34 +00:00
|
|
|
ArgsUsage: "[path/to/.woodpecker.yml]",
|
2021-12-13 18:51:53 +00:00
|
|
|
Action: run,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags, flags...),
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:51:53 +00:00
|
|
|
func run(c *cli.Context) error {
|
|
|
|
return common.RunPipelineFunc(c, execFile, execDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execDir(c *cli.Context, dir string) error {
|
|
|
|
// TODO: respect pipeline dependency
|
|
|
|
repoPath, _ := filepath.Abs(filepath.Dir(dir))
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
repoPath = convertPathForWindows(repoPath)
|
|
|
|
}
|
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if it is a regular file (not dir)
|
|
|
|
if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".yml") {
|
|
|
|
fmt.Println("#", info.Name())
|
|
|
|
_ = runExec(c, path, repoPath) // TODO: should we drop errors or store them and report back?
|
|
|
|
fmt.Println("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func execFile(c *cli.Context, file string) error {
|
|
|
|
repoPath, _ := filepath.Abs(filepath.Dir(file))
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
repoPath = convertPathForWindows(repoPath)
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
2021-12-13 18:51:53 +00:00
|
|
|
return runExec(c, file, repoPath)
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
|
2021-12-13 18:51:53 +00:00
|
|
|
func runExec(c *cli.Context, file, repoPath string) error {
|
2020-11-17 07:42:42 +00:00
|
|
|
dat, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
axes, err := matrix.ParseString(string(dat))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Parse matrix fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(axes) == 0 {
|
|
|
|
axes = append(axes, matrix.Axis{})
|
|
|
|
}
|
|
|
|
for _, axis := range axes {
|
2021-12-13 18:51:53 +00:00
|
|
|
err := execWithAxis(c, file, repoPath, axis)
|
2020-11-17 07:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:51:53 +00:00
|
|
|
func execWithAxis(c *cli.Context, file, repoPath string, axis matrix.Axis) error {
|
2021-09-21 03:47:08 +00:00
|
|
|
metadata := metadataFromContext(c, axis)
|
2019-04-06 19:32:14 +00:00
|
|
|
environ := metadata.Environ()
|
2021-09-24 14:29:26 +00:00
|
|
|
var secrets []compiler.Secret
|
2019-04-06 19:32:14 +00:00
|
|
|
for key, val := range metadata.Job.Matrix {
|
|
|
|
environ[key] = val
|
|
|
|
secrets = append(secrets, compiler.Secret{
|
|
|
|
Name: key,
|
|
|
|
Value: val,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-18 06:45:57 +00:00
|
|
|
droneEnv := make(map[string]string)
|
2019-04-06 19:32:14 +00:00
|
|
|
for _, env := range c.StringSlice("env") {
|
|
|
|
envs := strings.SplitN(env, "=", 2)
|
2020-11-18 06:45:57 +00:00
|
|
|
droneEnv[envs[0]] = envs[1]
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := envsubst.ParseFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
confstr, err := tmpl.Execute(func(name string) string {
|
|
|
|
return environ[name]
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := yaml.ParseString(confstr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure volumes for local execution
|
|
|
|
volumes := c.StringSlice("volumes")
|
|
|
|
if c.Bool("local") {
|
|
|
|
var (
|
|
|
|
workspaceBase = conf.Workspace.Base
|
|
|
|
workspacePath = conf.Workspace.Path
|
|
|
|
)
|
|
|
|
if workspaceBase == "" {
|
|
|
|
workspaceBase = c.String("workspace-base")
|
|
|
|
}
|
|
|
|
if workspacePath == "" {
|
|
|
|
workspacePath = c.String("workspace-path")
|
|
|
|
}
|
|
|
|
|
|
|
|
volumes = append(volumes, c.String("prefix")+"_default:"+workspaceBase)
|
2021-12-13 18:51:53 +00:00
|
|
|
volumes = append(volumes, repoPath+":"+path.Join(workspaceBase, workspacePath))
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lint the yaml file
|
|
|
|
if lerr := linter.New(linter.WithTrusted(true)).Lint(conf); lerr != nil {
|
|
|
|
return lerr
|
|
|
|
}
|
|
|
|
|
|
|
|
// compiles the yaml file
|
|
|
|
compiled := compiler.New(
|
|
|
|
compiler.WithEscalated(
|
|
|
|
c.StringSlice("privileged")...,
|
|
|
|
),
|
|
|
|
compiler.WithVolumes(volumes...),
|
|
|
|
compiler.WithWorkspace(
|
|
|
|
c.String("workspace-base"),
|
|
|
|
c.String("workspace-path"),
|
|
|
|
),
|
|
|
|
compiler.WithNetworks(
|
|
|
|
c.StringSlice("network")...,
|
|
|
|
),
|
|
|
|
compiler.WithPrefix(
|
|
|
|
c.String("prefix"),
|
|
|
|
),
|
|
|
|
compiler.WithProxy(),
|
|
|
|
compiler.WithLocal(
|
|
|
|
c.Bool("local"),
|
|
|
|
),
|
|
|
|
compiler.WithNetrc(
|
|
|
|
c.String("netrc-username"),
|
|
|
|
c.String("netrc-password"),
|
|
|
|
c.String("netrc-machine"),
|
|
|
|
),
|
|
|
|
compiler.WithMetadata(metadata),
|
|
|
|
compiler.WithSecret(secrets...),
|
2020-11-18 06:45:57 +00:00
|
|
|
compiler.WithEnviron(droneEnv),
|
2019-04-06 19:32:14 +00:00
|
|
|
).Compile(conf)
|
2022-02-26 02:02:42 +00:00
|
|
|
|
|
|
|
engine, err := backend.FindEngine(c.String("backend-engine"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-26 02:34:48 +00:00
|
|
|
if err = engine.Load(); err != nil {
|
2019-04-06 19:32:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), c.Duration("timeout"))
|
|
|
|
defer cancel()
|
2022-02-28 08:27:31 +00:00
|
|
|
ctx = utils.WithContextSigtermCallback(ctx, func() {
|
|
|
|
println("ctrl+c received, terminating process")
|
|
|
|
})
|
2019-04-06 19:32:14 +00:00
|
|
|
|
|
|
|
return pipeline.New(compiled,
|
|
|
|
pipeline.WithContext(ctx),
|
|
|
|
pipeline.WithTracer(pipeline.DefaultTracer),
|
|
|
|
pipeline.WithLogger(defaultLogger),
|
|
|
|
pipeline.WithEngine(engine),
|
|
|
|
).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the metadata from the cli context.
|
2021-09-21 03:47:08 +00:00
|
|
|
func metadataFromContext(c *cli.Context, axis matrix.Axis) frontend.Metadata {
|
2019-04-06 19:32:14 +00:00
|
|
|
return frontend.Metadata{
|
|
|
|
Repo: frontend.Repo{
|
|
|
|
Name: c.String("repo-name"),
|
|
|
|
Link: c.String("repo-link"),
|
|
|
|
Remote: c.String("repo-remote-url"),
|
|
|
|
Private: c.Bool("repo-private"),
|
|
|
|
},
|
|
|
|
Curr: frontend.Build{
|
2021-11-13 19:18:06 +00:00
|
|
|
Number: c.Int64("build-number"),
|
|
|
|
Parent: c.Int64("parent-build-number"),
|
2019-04-06 19:32:14 +00:00
|
|
|
Created: c.Int64("build-created"),
|
|
|
|
Started: c.Int64("build-started"),
|
|
|
|
Finished: c.Int64("build-finished"),
|
|
|
|
Status: c.String("build-status"),
|
|
|
|
Event: c.String("build-event"),
|
|
|
|
Link: c.String("build-link"),
|
|
|
|
Target: c.String("build-target"),
|
|
|
|
Commit: frontend.Commit{
|
|
|
|
Sha: c.String("commit-sha"),
|
|
|
|
Ref: c.String("commit-ref"),
|
|
|
|
Refspec: c.String("commit-refspec"),
|
|
|
|
Branch: c.String("commit-branch"),
|
|
|
|
Message: c.String("commit-message"),
|
|
|
|
Author: frontend.Author{
|
|
|
|
Name: c.String("commit-author-name"),
|
|
|
|
Email: c.String("commit-author-email"),
|
|
|
|
Avatar: c.String("commit-author-avatar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Prev: frontend.Build{
|
2021-11-13 19:18:06 +00:00
|
|
|
Number: c.Int64("prev-build-number"),
|
2019-04-06 19:32:14 +00:00
|
|
|
Created: c.Int64("prev-build-created"),
|
|
|
|
Started: c.Int64("prev-build-started"),
|
|
|
|
Finished: c.Int64("prev-build-finished"),
|
|
|
|
Status: c.String("prev-build-status"),
|
|
|
|
Event: c.String("prev-build-event"),
|
|
|
|
Link: c.String("prev-build-link"),
|
|
|
|
Commit: frontend.Commit{
|
|
|
|
Sha: c.String("prev-commit-sha"),
|
|
|
|
Ref: c.String("prev-commit-ref"),
|
|
|
|
Refspec: c.String("prev-commit-refspec"),
|
|
|
|
Branch: c.String("prev-commit-branch"),
|
|
|
|
Message: c.String("prev-commit-message"),
|
|
|
|
Author: frontend.Author{
|
|
|
|
Name: c.String("prev-commit-author-name"),
|
|
|
|
Email: c.String("prev-commit-author-email"),
|
|
|
|
Avatar: c.String("prev-commit-author-avatar"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Job: frontend.Job{
|
|
|
|
Number: c.Int("job-number"),
|
2021-09-21 03:47:08 +00:00
|
|
|
Matrix: axis,
|
2019-04-06 19:32:14 +00:00
|
|
|
},
|
|
|
|
Sys: frontend.System{
|
|
|
|
Name: c.String("system-name"),
|
|
|
|
Link: c.String("system-link"),
|
|
|
|
Arch: c.String("system-arch"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertPathForWindows(path string) string {
|
|
|
|
base := filepath.VolumeName(path)
|
|
|
|
if len(base) == 2 {
|
|
|
|
path = strings.TrimPrefix(path, base)
|
|
|
|
base = strings.ToLower(strings.TrimSuffix(base, ":"))
|
|
|
|
return "/" + base + filepath.ToSlash(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.ToSlash(path)
|
|
|
|
}
|
|
|
|
|
2022-02-26 02:02:42 +00:00
|
|
|
var defaultLogger = pipeline.LogFunc(func(proc *backendTypes.Step, rc multipart.Reader) error {
|
2019-04-06 19:32:14 +00:00
|
|
|
part, err := rc.NextPart()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
logStream := NewLineWriter(proc.Alias)
|
|
|
|
_, err = io.Copy(logStream, part)
|
|
|
|
return err
|
2019-04-06 19:32:14 +00:00
|
|
|
})
|