fix #2 - add support for an array of 'Runs'

This commit is contained in:
Casey Lee 2019-01-15 09:57:36 -08:00
parent 44e21b102e
commit 2b471fbff0
No known key found for this signature in database
GPG key ID: 4CC378651BF9C168
5 changed files with 35 additions and 15 deletions

View file

@ -2,12 +2,15 @@ package actions
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast" "github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/hcl/hcl/token"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -64,11 +67,33 @@ func cleanWorkflowsAST(node ast.Node) (ast.Node, bool) {
// handle condition where value is a string but should be a list // handle condition where value is a string but should be a list
switch key { switch key {
case "resolves", "needs", "args": case "args", "runs":
if literalType, ok := objectItem.Val.(*ast.LiteralType); ok {
listType := new(ast.ListType)
parts := strings.Split(literalType.Token.Value().(string), " ")
log.Debugf("got list: %v", parts)
if len(parts) > 0 {
quote := literalType.Token.Text[0]
for _, part := range parts {
part = fmt.Sprintf("%c%s%c", quote, part, quote)
log.Debugf("Adding part %s", part)
listType.Add(&ast.LiteralType{
Token: token.Token{
Type: token.STRING,
Text: part,
},
})
}
}
objectItem.Val = listType
}
case "resolves", "needs":
if literalType, ok := objectItem.Val.(*ast.LiteralType); ok { if literalType, ok := objectItem.Val.(*ast.LiteralType); ok {
listType := new(ast.ListType) listType := new(ast.ListType)
listType.Add(literalType) listType.Add(literalType)
objectItem.Val = listType objectItem.Val = listType
} }
} }
} }

View file

@ -216,6 +216,7 @@ func (action *actionDef) asExecutor(ctx context.Context, dryrun bool, workingDir
executors = append(executors, container.NewDockerRunExecutor(container.NewDockerRunExecutorInput{ executors = append(executors, container.NewDockerRunExecutor(container.NewDockerRunExecutorInput{
DockerExecutorInput: in, DockerExecutorInput: in,
Cmd: action.Args, Cmd: action.Args,
Entrypoint: action.Runs,
Image: image, Image: image,
WorkingDir: "/github/workspace", WorkingDir: "/github/workspace",
Env: env, Env: env,

View file

@ -41,7 +41,7 @@ type workflowDef struct {
type actionDef struct { type actionDef struct {
Needs []string Needs []string
Uses string Uses string
Runs string Runs []string
Args []string Args []string
Env map[string]string Env map[string]string
Secrets []string Secrets []string

View file

@ -204,7 +204,9 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
return err return err
} }
w.Pull(&git.PullOptions{}) w.Pull(&git.PullOptions{
ReferenceName: refName,
})
input.Logger.Debugf("Cloned %s to %s", input.URL.String(), input.Dir) input.Logger.Debugf("Cloned %s to %s", input.URL.String(), input.Dir)
err = w.Checkout(&git.CheckoutOptions{ err = w.Checkout(&git.CheckoutOptions{

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
@ -19,7 +18,7 @@ import (
type NewDockerRunExecutorInput struct { type NewDockerRunExecutorInput struct {
DockerExecutorInput DockerExecutorInput
Image string Image string
Entrypoint string Entrypoint []string
Cmd []string Cmd []string
WorkingDir string WorkingDir string
Env []string Env []string
@ -33,7 +32,7 @@ type NewDockerRunExecutorInput struct {
func NewDockerRunExecutor(input NewDockerRunExecutorInput) common.Executor { func NewDockerRunExecutor(input NewDockerRunExecutorInput) common.Executor {
return func() error { return func() error {
input.Logger.Infof("docker run %s %s", input.Image, input.Cmd) input.Logger.Infof("docker run %s %s %s", input.Image, input.Entrypoint, input.Cmd)
if input.Dryrun { if input.Dryrun {
return nil return nil
} }
@ -73,14 +72,10 @@ func NewDockerRunExecutor(input NewDockerRunExecutorInput) common.Executor {
func createContainer(input NewDockerRunExecutorInput, cli *client.Client) (string, error) { func createContainer(input NewDockerRunExecutorInput, cli *client.Client) (string, error) {
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd())) isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
cmd := input.Cmd
if len(input.Cmd) == 1 {
cmd = strings.Split(cmd[0], " ")
}
config := &container.Config{ config := &container.Config{
Image: input.Image, Image: input.Image,
Cmd: cmd, Cmd: input.Cmd,
Entrypoint: input.Entrypoint,
WorkingDir: input.WorkingDir, WorkingDir: input.WorkingDir,
Env: input.Env, Env: input.Env,
Tty: isTerminal, Tty: isTerminal,
@ -93,9 +88,6 @@ func createContainer(input NewDockerRunExecutorInput, cli *client.Client) (strin
} }
} }
if input.Entrypoint != "" {
config.Entrypoint = []string{input.Entrypoint}
}
resp, err := cli.ContainerCreate(input.Ctx, config, &container.HostConfig{ resp, err := cli.ContainerCreate(input.Ctx, config, &container.HostConfig{
Binds: input.Binds, Binds: input.Binds,
}, nil, input.Name) }, nil, input.Name)