Skip to content

Commit

Permalink
Merge branch 'master' into cat/fix/remove-windows-issue-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored May 6, 2021
2 parents 8f3a506 + a144e71 commit e078fa0
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 36 deletions.
103 changes: 68 additions & 35 deletions pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type RunContext struct {
ExprEval ExpressionEvaluator
JobContainer container.Container
OutputMappings map[MappableOutput]MappableOutput
JobName string
}

type MappableOutput struct {
Expand Down Expand Up @@ -454,50 +455,67 @@ func (rc *RunContext) getStepsContext() map[string]*stepResult {
}

type githubContext struct {
Event map[string]interface{} `json:"event"`
EventPath string `json:"event_path"`
Workflow string `json:"workflow"`
RunID string `json:"run_id"`
RunNumber string `json:"run_number"`
Actor string `json:"actor"`
Repository string `json:"repository"`
EventName string `json:"event_name"`
Sha string `json:"sha"`
Ref string `json:"ref"`
HeadRef string `json:"head_ref"`
BaseRef string `json:"base_ref"`
Token string `json:"token"`
Workspace string `json:"workspace"`
Action string `json:"action"`
Event map[string]interface{} `json:"event"`
EventPath string `json:"event_path"`
Workflow string `json:"workflow"`
RunID string `json:"run_id"`
RunNumber string `json:"run_number"`
Actor string `json:"actor"`
Repository string `json:"repository"`
EventName string `json:"event_name"`
Sha string `json:"sha"`
Ref string `json:"ref"`
HeadRef string `json:"head_ref"`
BaseRef string `json:"base_ref"`
Token string `json:"token"`
Workspace string `json:"workspace"`
Action string `json:"action"`
ActionPath string `json:"action_path"`
ActionRef string `json:"action_ref"`
ActionRepository string `json:"action_repository"`
Job string `json:"job"`
JobName string `json:"job_name"`
RepositoryOwner string `json:"repository_owner"`
RetentionDays string `json:"retention_days"`
RunnerPerflog string `json:"runner_perflog"`
RunnerTrackingID string `json:"runner_tracking_id"`
}

func (rc *RunContext) getGithubContext() *githubContext {
token, ok := rc.Config.Secrets["GITHUB_TOKEN"]
if !ok {
token = os.Getenv("GITHUB_TOKEN")
ghc := &githubContext{
Event: make(map[string]interface{}),
EventPath: "/tmp/workflow/event.json",
Workflow: rc.Run.Workflow.Name,
RunID: rc.Config.Env["GITHUB_RUN_ID"],
RunNumber: rc.Config.Env["GITHUB_RUN_NUMBER"],
Actor: rc.Config.Actor,
EventName: rc.Config.EventName,
Workspace: rc.Config.ContainerWorkdir(),
Action: rc.CurrentStep,
Token: rc.Config.Secrets["GITHUB_TOKEN"],
ActionPath: rc.Config.Env["GITHUB_ACTION_PATH"],
ActionRef: rc.Config.Env["RUNNER_ACTION_REF"],
ActionRepository: rc.Config.Env["RUNNER_ACTION_REPOSITORY"],
RepositoryOwner: rc.Config.Env["GITHUB_REPOSITORY_OWNER"],
RetentionDays: rc.Config.Env["GITHUB_RETENTION_DAYS"],
RunnerPerflog: rc.Config.Env["RUNNER_PERFLOG"],
RunnerTrackingID: rc.Config.Env["RUNNER_TRACKING_ID"],
}

runID := rc.Config.Env["GITHUB_RUN_ID"]
if runID == "" {
runID = "1"
if ghc.RunID == "" {
ghc.RunID = "1"
}

runNumber := rc.Config.Env["GITHUB_RUN_NUMBER"]
if runNumber == "" {
runNumber = "1"
if ghc.RunNumber == "" {
ghc.RunNumber = "1"
}

ghc := &githubContext{
Event: make(map[string]interface{}),
EventPath: "/tmp/workflow/event.json",
Workflow: rc.Run.Workflow.Name,
RunID: runID,
RunNumber: runNumber,
Actor: rc.Config.Actor,
EventName: rc.Config.EventName,
Token: token,
Workspace: rc.Config.ContainerWorkdir(),
Action: rc.CurrentStep,
if ghc.RetentionDays == "" {
ghc.RetentionDays = "0"
}

if ghc.RunnerPerflog == "" {
ghc.RunnerPerflog = "/dev/null"
}

// Backwards compatibility for configs that require
Expand All @@ -512,6 +530,9 @@ func (rc *RunContext) getGithubContext() *githubContext {
log.Warningf("unable to get git repo: %v", err)
} else {
ghc.Repository = repo
if ghc.RepositoryOwner == "" {
ghc.RepositoryOwner = strings.Split(repo, "/")[0]
}
}

_, sha, err := common.FindGitRevision(repoPath)
Expand Down Expand Up @@ -637,6 +658,9 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
env["GITHUB_RUN_ID"] = github.RunID
env["GITHUB_RUN_NUMBER"] = github.RunNumber
env["GITHUB_ACTION"] = github.Action
if github.ActionPath != "" {
env["GITHUB_ACTION_PATH"] = github.ActionPath
}
env["GITHUB_ACTIONS"] = "true"
env["GITHUB_ACTOR"] = github.Actor
env["GITHUB_REPOSITORY"] = github.Repository
Expand All @@ -649,6 +673,15 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
env["GITHUB_SERVER_URL"] = "https://github.com"
env["GITHUB_API_URL"] = "https://api.github.com"
env["GITHUB_GRAPHQL_URL"] = "https://api.github.com/graphql"
env["GITHUB_ACTION_REF"] = github.ActionRef
env["GITHUB_ACTION_REPOSITORY"] = github.ActionRepository
env["GITHUB_BASE_REF"] = github.BaseRef
env["GITHUB_HEAD_REF"] = github.HeadRef
env["GITHUB_JOB"] = rc.JobName
env["GITHUB_REPOSITORY_OWNER"] = github.RepositoryOwner
env["GITHUB_RETENTION_DAYS"] = github.RetentionDays
env["RUNNER_PERFLOG"] = github.RunnerPerflog
env["RUNNER_TRACKING_ID"] = github.RunnerTrackingID
if rc.Config.GitHubInstance != "github.com" {
env["GITHUB_SERVER_URL"] = fmt.Sprintf("https://%s", rc.Config.GitHubInstance)
env["GITHUB_API_URL"] = fmt.Sprintf("https://%s/api/v3", rc.Config.GitHubInstance)
Expand Down
43 changes: 43 additions & 0 deletions pkg/runner/run_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

"github.com/nektos/act/pkg/model"
a "github.com/stretchr/testify/assert"
"gotest.tools/v3/assert"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
)

Expand Down Expand Up @@ -277,3 +279,44 @@ func TestRunContext_GetBindsAndMounts(t *testing.T) {
}
}
}

func TestGetGitHubContext(t *testing.T) {
log.SetLevel(log.DebugLevel)

cwd, err := os.Getwd()
assert.NilError(t, err)

rc := &RunContext{
Config: &Config{
EventName: "push",
Workdir: cwd,
},
Run: &model.Run{
Workflow: &model.Workflow{
Name: "GitHubContextTest",
},
},
Name: "GitHubContextTest",
CurrentStep: "step",
Matrix: map[string]interface{}{},
Env: map[string]string{},
ExtraPath: []string{},
StepResults: map[string]*stepResult{},
OutputMappings: map[MappableOutput]MappableOutput{},
}

ghc := rc.getGithubContext()

log.Debugf("%v", ghc)

assert.Equal(t, ghc.RunID, "1")
assert.Equal(t, ghc.Workspace, cwd)
assert.Equal(t, ghc.RunNumber, "1")
assert.Equal(t, ghc.RetentionDays, "0")
assert.Equal(t, ghc.Actor, "nektos/act")
assert.Equal(t, ghc.Repository, "nektos/act")
assert.Equal(t, ghc.RepositoryOwner, "nektos")
assert.Equal(t, ghc.RunnerPerflog, "/dev/null")
assert.Equal(t, ghc.EventPath, "/tmp/workflow/event.json")
assert.Equal(t, ghc.Token, rc.Config.Secrets["GITHUB_TOKEN"])
}
1 change: 1 addition & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {

for i, matrix := range matrixes {
rc := runner.newRunContext(run, matrix)
rc.JobName = rc.Name
if len(matrixes) > 1 {
rc.Name = fmt.Sprintf("%s-%d", rc.Name, i+1)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/runner/step_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,12 @@ func (sc *StepContext) execAsComposite(ctx context.Context, step *model.Step, _
}
}

stepClone.Run = strings.ReplaceAll(stepClone.Run, "${{ github.action_path }}", filepath.Join(containerActionDir, actionName))
if stepClone.Env == nil {
stepClone.Env = make(map[string]string)
}
actionPath := filepath.Join(containerActionDir, actionName)
stepClone.Env["GITHUB_ACTION_PATH"] = actionPath
stepClone.Run = strings.ReplaceAll(stepClone.Run, "${{ github.action_path }}", actionPath)

stepContext := StepContext{
RunContext: rcClone,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ runs:
fi
shell: bash
- run: |
if [ -z "$GITHUB_ACTION_PATH" ]; then
exit 1
fi
if [ -z "${{ github.action_path }}" ]; then
exit 2
fi
shell: bash
# Let's send up an output to test
- run: echo "::set-output name=test_output::test_output_value"
shell: bash
Expand Down

0 comments on commit e078fa0

Please sign in to comment.