woodpecker/drone/exec.go

424 lines
9.6 KiB
Go
Raw Normal View History

package main
2016-05-10 05:57:57 +00:00
import (
2017-03-16 10:14:02 +00:00
"context"
"io"
2016-05-10 05:57:57 +00:00
"log"
"os"
2017-03-16 10:14:02 +00:00
"path"
2016-05-10 05:57:57 +00:00
"path/filepath"
"strings"
"time"
2017-03-16 10:14:02 +00:00
"github.com/cncd/pipeline/pipeline"
"github.com/cncd/pipeline/pipeline/backend"
"github.com/cncd/pipeline/pipeline/backend/docker"
"github.com/cncd/pipeline/pipeline/frontend"
"github.com/cncd/pipeline/pipeline/frontend/yaml"
"github.com/cncd/pipeline/pipeline/frontend/yaml/compiler"
"github.com/cncd/pipeline/pipeline/interrupt"
"github.com/cncd/pipeline/pipeline/multipart"
"github.com/drone/envsubst"
2016-05-10 05:57:57 +00:00
2017-03-16 10:14:02 +00:00
"github.com/urfave/cli"
2016-05-10 05:57:57 +00:00
)
var execCmd = cli.Command{
Name: "exec",
Usage: "execute a local build",
Action: func(c *cli.Context) {
if err := exec(c); err != nil {
log.Fatalln(err)
}
},
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "local",
Usage: "build from local directory",
EnvVar: "DRONE_LOCAL",
},
cli.DurationFlag{
Name: "timeout",
Usage: "build timeout",
2016-05-10 05:57:57 +00:00
Value: time.Hour,
EnvVar: "DRONE_TIMEOUT",
},
2017-03-16 10:14:02 +00:00
cli.StringSliceFlag{
Name: "volumes",
Usage: "build volumes",
EnvVar: "DRONE_VOLUMES",
2016-05-10 05:57:57 +00:00
},
cli.StringSliceFlag{
2017-03-16 10:14:02 +00:00
Name: "privileged",
Usage: "privileged plugins",
2016-05-10 05:57:57 +00:00
Value: &cli.StringSlice{
"plugins/docker",
2016-09-09 23:16:06 +00:00
"plugins/gcr",
2016-05-10 05:57:57 +00:00
"plugins/ecr",
},
},
2017-03-16 10:14:02 +00:00
//
// Please note the below flags are mirrored in the pipec and
// should be kept synchronized. Do not edit directly
// https://github.com/cncd/pipeline/pipec
//
2016-05-10 05:57:57 +00:00
2017-03-16 10:14:02 +00:00
//
// workspace default
//
2016-05-10 05:57:57 +00:00
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "workspace-base",
Value: "/pipeline",
EnvVar: "DRONE_WORKSPACE_BASE",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.StringFlag{
Name: "workspace-path",
Value: "src",
EnvVar: "DRONE_WORKSPACE_PATH",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
//
// netrc parameters
//
2016-05-10 05:57:57 +00:00
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "netrc-username",
EnvVar: "DRONE_NETRC_USERNAME",
},
cli.StringFlag{
Name: "netrc-password",
EnvVar: "DRONE_NETRC_PASSWORD",
},
cli.StringFlag{
Name: "netrc-machine",
EnvVar: "DRONE_NETRC_MACHINE",
2016-05-10 05:57:57 +00:00
},
//
2017-03-16 10:14:02 +00:00
// metadata parameters
2016-05-10 05:57:57 +00:00
//
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "system-arch",
Value: "linux/amd64",
EnvVar: "DRONE_SYSTEM_ARCH",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "system-name",
Value: "pipec",
EnvVar: "DRONE_SYSTEM_NAME",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "system-link",
Value: "https://github.com/cncd/pipec",
EnvVar: "DRONE_SYSTEM_LINK",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "repo-name",
EnvVar: "DRONE_REPO_NAME",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "repo-link",
2016-05-10 05:57:57 +00:00
EnvVar: "DRONE_REPO_LINK",
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "repo-remote-url",
EnvVar: "DRONE_REPO_REMOTE",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "repo-private",
2016-05-10 05:57:57 +00:00
EnvVar: "DRONE_REPO_PRIVATE",
},
2017-03-16 10:14:02 +00:00
cli.IntFlag{
Name: "build-number",
EnvVar: "DRONE_BUILD_NUMBER",
},
cli.Int64Flag{
Name: "build-created",
EnvVar: "DRONE_BUILD_CREATED",
},
cli.Int64Flag{
Name: "build-started",
EnvVar: "DRONE_BUILD_STARTED",
},
cli.Int64Flag{
Name: "build-finished",
EnvVar: "DRONE_BUILD_FINISHED",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "build-status",
EnvVar: "DRONE_BUILD_STATUS",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "build-event",
EnvVar: "DRONE_BUILD_EVENT",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "build-link",
EnvVar: "DRONE_BUILD_LINK",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "build-target",
EnvVar: "DRONE_BUILD_TARGET",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "commit-sha",
EnvVar: "DRONE_COMMIT_SHA",
},
cli.StringFlag{
Name: "commit-ref",
EnvVar: "DRONE_COMMIT_REF",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "commit-refspec",
EnvVar: "DRONE_COMMIT_REFSPEC",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "commit-branch",
EnvVar: "DRONE_COMMIT_BRANCH",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "commit-message",
EnvVar: "DRONE_COMMIT_MESSAGE",
},
cli.StringFlag{
Name: "commit-author-name",
EnvVar: "DRONE_COMMIT_AUTHOR_NAME",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "commit-author-avatar",
2016-05-10 05:57:57 +00:00
EnvVar: "DRONE_COMMIT_AUTHOR_AVATAR",
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "commit-author-email",
EnvVar: "DRONE_COMMIT_AUTHOR_EMAIL",
2016-05-10 05:57:57 +00:00
},
cli.IntFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-build-number",
EnvVar: "DRONE_PREV_BUILD_NUMBER",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.Int64Flag{
Name: "prev-build-created",
EnvVar: "DRONE_PREV_BUILD_CREATED",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.Int64Flag{
Name: "prev-build-started",
EnvVar: "DRONE_PREV_BUILD_STARTED",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.Int64Flag{
Name: "prev-build-finished",
EnvVar: "DRONE_PREV_BUILD_FINISHED",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-build-status",
EnvVar: "DRONE_PREV_BUILD_STATUS",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-build-event",
EnvVar: "DRONE_PREV_BUILD_EVENT",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-build-link",
EnvVar: "DRONE_PREV_BUILD_LINK",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.StringFlag{
Name: "prev-commit-sha",
EnvVar: "DRONE_PREV_COMMIT_SHA",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.StringFlag{
Name: "prev-commit-ref",
EnvVar: "DRONE_PREV_COMMIT_REF",
2016-05-10 05:57:57 +00:00
},
2017-03-16 10:14:02 +00:00
cli.StringFlag{
Name: "prev-commit-refspec",
EnvVar: "DRONE_PREV_COMMIT_REFSPEC",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-commit-branch",
EnvVar: "DRONE_PREV_COMMIT_BRANCH",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-commit-message",
EnvVar: "DRONE_PREV_COMMIT_MESSAGE",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-commit-author-name",
EnvVar: "DRONE_PREV_COMMIT_AUTHOR_NAME",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-commit-author-avatar",
EnvVar: "DRONE_PREV_COMMIT_AUTHOR_AVATAR",
2016-05-10 05:57:57 +00:00
},
cli.StringFlag{
2017-03-16 10:14:02 +00:00
Name: "prev-commit-author-email",
EnvVar: "DRONE_PREV_COMMIT_AUTHOR_EMAIL",
},
cli.IntFlag{
Name: "job-number",
EnvVar: "DRONE_JOB_NUMBER",
2016-05-10 05:57:57 +00:00
},
},
}
func exec(c *cli.Context) error {
2017-03-16 10:14:02 +00:00
file := c.Args().First()
if file == "" {
file = ".drone.yml"
}
2016-05-10 05:57:57 +00:00
2017-03-16 10:14:02 +00:00
metadata := metadataFromContext(c)
environ := metadata.Environ()
for k, v := range metadata.EnvironDrone() {
environ[k] = v
}
for _, env := range os.Environ() {
k := strings.Split(env, "=")[0]
v := strings.Split(env, "=")[1]
environ[k] = v
2016-05-10 05:57:57 +00:00
}
2017-03-16 10:14:02 +00:00
tmpl, err := envsubst.ParseFile(file)
2016-05-10 05:57:57 +00:00
if err != nil {
return err
}
2017-03-16 10:14:02 +00:00
confstr, err := tmpl.Execute(func(name string) string {
return environ[name]
})
2016-05-10 05:57:57 +00:00
if err != nil {
return err
}
2017-03-16 10:14:02 +00:00
conf, err := yaml.ParseString(confstr)
if err != nil {
return err
}
2017-03-16 10:14:02 +00:00
// 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")
}
2017-03-16 10:14:02 +00:00
if workspacePath == "" {
workspacePath = c.String("workspace-path")
}
dir, _ := filepath.Abs(filepath.Dir(file))
volumes = append(volumes, dir+":"+path.Join(workspaceBase, workspacePath))
2016-05-10 05:57:57 +00:00
}
2017-03-16 10:14:02 +00:00
// 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.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),
).Compile(conf)
2016-05-10 05:57:57 +00:00
2017-03-16 10:14:02 +00:00
engine, err := docker.NewEnv()
if err != nil {
return err
2016-05-10 05:57:57 +00:00
}
2017-03-16 10:14:02 +00:00
ctx, cancel := context.WithTimeout(context.Background(), c.Duration("timeout"))
defer cancel()
ctx = interrupt.WithContext(ctx)
2017-03-16 10:14:02 +00:00
return pipeline.New(compiled,
pipeline.WithContext(ctx),
pipeline.WithLogger(defaultLogger),
pipeline.WithTracer(pipeline.DefaultTracer),
pipeline.WithLogger(defaultLogger),
pipeline.WithEngine(engine),
).Run()
}
2017-03-16 10:14:02 +00:00
// return the metadata from the cli context.
func metadataFromContext(c *cli.Context) frontend.Metadata {
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{
Number: c.Int("build-number"),
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"),
},
},
2017-03-16 10:14:02 +00:00
},
Prev: frontend.Build{
Number: c.Int("prev-build-number"),
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"),
},
Sys: frontend.System{
Name: c.String("system-name"),
Link: c.String("system-link"),
Arch: c.String("system-arch"),
},
2016-05-10 05:57:57 +00:00
}
}
2017-03-16 10:14:02 +00:00
var defaultLogger = pipeline.LogFunc(func(proc *backend.Step, rc multipart.Reader) error {
part, err := rc.NextPart()
if err != nil {
return err
}
io.Copy(os.Stderr, part)
return nil
})