Skip to content

Commit

Permalink
fix action
Browse files Browse the repository at this point in the history
  • Loading branch information
yindia committed Jan 2, 2025
1 parent b2ee8c7 commit 840b7bf
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 37 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PR Checks

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]


jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: true

- name: Install dependencies
run: go mod download

- name: Run tests
run: go test -v ./...

- name: Run vet
run: go vet ./...

- name: Install golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Build
run: go build -v ./...
80 changes: 43 additions & 37 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,48 +82,21 @@ func (t *Task) Run() error {
return fmt.Errorf("pre-run hook failed for workflow %s: %v", t.Name, err)
}
}

var lastErr error
for attempt := 1; attempt <= t.Retries; attempt++ {
t.logger.Debug("Attempt %d of %d for task: %s", attempt, t.Retries, t.Name)

cmd := exec.Command(t.Command, t.Args...)
cmd.Env = append(os.Environ(), t.Env...)

// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
cmd.WaitDelay = t.Timeout

// Set the command to use our context
cmd = exec.CommandContext(ctx, t.Command, t.Args...)
cmd.Env = append(os.Environ(), t.Env...)

t.logger.Debug("Command: %s", t.Command+" "+strings.Join(t.Args, " "))
output, err := cmd.CombinedOutput()
t.Actual.ExitCode = getExitCode(err)
t.Actual.Output = string(output)

if err != nil {
t.Actual.Error = err.Error()
if ctx.Err() == context.DeadlineExceeded {
t.logger.Error("Task %s timed out after %v", t.Name, t.Timeout)
return fmt.Errorf("task %s timed out after %v", t.Name, t.Timeout)
if err := t.executeCommand(); err != nil {
lastErr = err
if attempt < t.Retries {
t.logger.Debug("Retrying task %s after failure", t.Name)
time.Sleep(1 * time.Second)
continue
}
t.logger.Error("Error executing task %s: %v", t.Name, err)
return fmt.Errorf("task %s failed after %d attempts: %w", t.Name, t.Retries, err)
}

for _, assert := range t.Asserts {
if err := assert(t); err != nil {
t.logger.Error("Assertion failed for task %s: %v", t.Name, err)
if attempt < t.Retries {
t.logger.Debug("Retrying task %s after failure", t.Name)
time.Sleep(1 * time.Second)
continue
}
fmt.Println("Command ", t.Command, strings.Join(t.Args, " "))
return fmt.Errorf("assertion failed for task %s: %w", t.Name, err)
}
}
break
return nil // Success, exit retry loop
}

if t.PostRun != nil {
Expand All @@ -133,6 +106,39 @@ func (t *Task) Run() error {
return fmt.Errorf("post-run hook failed for workflow %s: %v", t.Name, err)
}
}
return lastErr
}

// executeCommand handles a single execution attempt
func (t *Task) executeCommand() error {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()

cmd := exec.CommandContext(ctx, t.Command, t.Args...)
cmd.Env = append(os.Environ(), t.Env...)

t.logger.Debug("Command: %s", t.Command+" "+strings.Join(t.Args, " "))
output, err := cmd.CombinedOutput()
t.Actual.Output = string(output)
t.Actual.ExitCode = getExitCode(err)

if err != nil {
t.Actual.Error = err.Error()
if ctx.Err() == context.DeadlineExceeded {
t.logger.Error("Task %s timed out after %v", t.Name, t.Timeout)
return fmt.Errorf("task %s timed out after %v", t.Name, t.Timeout)
}
t.logger.Error("Error executing task %s: %v", t.Name, err)
}

// Run assertions
for _, assert := range t.Asserts {
if err := assert(t); err != nil {
t.logger.Error("Assertion failed for task %s: %v", t.Name, err)
return err
}
}

return nil
}

Expand Down

0 comments on commit 840b7bf

Please sign in to comment.