parent
36303ce43d
commit
a04141c444
4 changed files with 56 additions and 30 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/hcl"
|
||||
"github.com/hashicorp/hcl/hcl/ast"
|
||||
|
@ -54,7 +55,7 @@ func cleanWorkflowsAST(node ast.Node) (ast.Node, bool) {
|
|||
}
|
||||
quote := literalType.Token.Text[0]
|
||||
for _, part := range parts {
|
||||
part = fmt.Sprintf("%c%s%c", quote, part, quote)
|
||||
part = fmt.Sprintf("%c%s%c", quote, strings.Replace(part, "\\", "\\\\", -1), quote)
|
||||
listType.Add(&ast.LiteralType{
|
||||
Token: token.Token{
|
||||
Type: token.STRING,
|
||||
|
|
|
@ -13,40 +13,50 @@ func TestParseWorkflowsFile(t *testing.T) {
|
|||
|
||||
conf := `
|
||||
workflow "build-and-deploy" {
|
||||
on = "push"
|
||||
resolves = ["deploy"]
|
||||
on = "push"
|
||||
resolves = ["deploy"]
|
||||
}
|
||||
|
||||
action "build" {
|
||||
uses = "./action1"
|
||||
args = "echo 'build'"
|
||||
uses = "./action1"
|
||||
args = "echo 'build'"
|
||||
}
|
||||
|
||||
action "test" {
|
||||
uses = "docker://ubuntu:18.04"
|
||||
runs = "echo 'test'"
|
||||
needs = ["build"]
|
||||
uses = "docker://ubuntu:18.04"
|
||||
runs = "echo 'test'"
|
||||
needs = ["build"]
|
||||
}
|
||||
|
||||
action "deploy" {
|
||||
uses = "./action2"
|
||||
args = ["echo","deploy"]
|
||||
needs = ["test"]
|
||||
uses = "./action2"
|
||||
args = ["echo","deploy"]
|
||||
needs = ["test"]
|
||||
}
|
||||
|
||||
action "docker-login" {
|
||||
uses = "docker://docker"
|
||||
runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"]
|
||||
secrets = ["DOCKER_AUTH"]
|
||||
env = {
|
||||
REGISTRY_USER = "username"
|
||||
}
|
||||
uses = "docker://docker"
|
||||
runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"]
|
||||
secrets = ["DOCKER_AUTH"]
|
||||
env = {
|
||||
REGISTRY_USER = "username"
|
||||
}
|
||||
}
|
||||
|
||||
action "unit-tests" {
|
||||
uses = "./scripts/github_actions"
|
||||
runs = "yarn test:ci-unittest || echo \"Unit tests failed, but running danger to present the results!\" 2>&1"
|
||||
}
|
||||
uses = "./scripts/github_actions"
|
||||
runs = "yarn test:ci-unittest || echo \"Unit tests failed, but running danger to present the results!\" 2>&1"
|
||||
}
|
||||
|
||||
action "regex-in-args" {
|
||||
uses = "actions/bin/filter@master"
|
||||
args = "tag v?[0-9]+\\.[0-9]+\\.[0-9]+"
|
||||
}
|
||||
|
||||
action "regex-in-args-array" {
|
||||
uses = "actions/bin/filter@master"
|
||||
args = ["tag","v?[0-9]+\\.[0-9]+\\.[0-9]+"]
|
||||
}
|
||||
`
|
||||
|
||||
workflows, err := parseWorkflowsFile(strings.NewReader(conf))
|
||||
|
@ -101,6 +111,20 @@ func TestParseWorkflowsFile(t *testing.T) {
|
|||
nil,
|
||||
nil,
|
||||
},
|
||||
{"regex-in-args",
|
||||
"actions/bin/filter@master",
|
||||
nil,
|
||||
nil,
|
||||
[]string{"tag", `v?[0-9]+\.[0-9]+\.[0-9]+`},
|
||||
nil,
|
||||
},
|
||||
{"regex-in-args-array",
|
||||
"actions/bin/filter@master",
|
||||
nil,
|
||||
nil,
|
||||
[]string{"tag", `v?[0-9]+\.[0-9]+\.[0-9]+`},
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, exp := range actions {
|
||||
|
|
|
@ -128,11 +128,11 @@ func (runner *runnerImpl) applyEnvironment(env map[string]string) {
|
|||
env["GITHUB_REPOSITORY"] = repo
|
||||
}
|
||||
|
||||
branch, err := common.FindGitBranch(repoPath)
|
||||
ref, err := common.FindGitRef(repoPath)
|
||||
if err != nil {
|
||||
log.Warningf("unable to get git branch: %v", err)
|
||||
log.Warningf("unable to get git ref: %v", err)
|
||||
} else {
|
||||
env["GITHUB_REF"] = fmt.Sprintf("refs/heads/%s", branch)
|
||||
env["GITHUB_REF"] = ref
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,20 +30,20 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
|
|||
return "", "", err
|
||||
}
|
||||
|
||||
head, err := findGitHead(file)
|
||||
ref, err := FindGitRef(file)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
var refBuf []byte
|
||||
if strings.HasPrefix(head, "refs/") {
|
||||
if strings.HasPrefix(ref, "refs/") {
|
||||
// load commitid ref
|
||||
refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, head))
|
||||
refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, ref))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
} else {
|
||||
refBuf = []byte(head)
|
||||
refBuf = []byte(ref)
|
||||
}
|
||||
|
||||
log.Debugf("Found revision: %s", refBuf)
|
||||
|
@ -52,18 +52,19 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
|
|||
|
||||
// FindGitBranch get the current git branch
|
||||
func FindGitBranch(file string) (string, error) {
|
||||
head, err := findGitHead(file)
|
||||
ref, err := FindGitRef(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// get branch name
|
||||
branch := strings.TrimPrefix(head, "refs/heads/")
|
||||
branch := strings.TrimPrefix(ref, "refs/heads/")
|
||||
log.Debugf("Found branch: %s", branch)
|
||||
return branch, nil
|
||||
}
|
||||
|
||||
func findGitHead(file string) (string, error) {
|
||||
// FindGitRef get the current git ref
|
||||
func FindGitRef(file string) (string, error) {
|
||||
gitDir, err := findGitDirectory(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
Loading…
Reference in a new issue