Skip to content

Commit

Permalink
feat: export workflow to a file in run directory
Browse files Browse the repository at this point in the history
  • Loading branch information
aweris committed Oct 18, 2023
1 parent 1cfe2a4 commit 2925983
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ghx/context/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ func (c *Context) UnsetWorkflow(result RunResult) {
if err := fs.WriteJSONFile(filepath.Join(dir, "workflow_run.json"), report); err != nil {
log.Errorf("failed to write workflow run", "error", err, "workflow", c.Execution.WorkflowRun.Workflow.Name)
}

// copy file to the workflow run directory
var (
src = c.Execution.WorkflowRun.Workflow.Path
dst = filepath.Join(dir, "workflow.yaml")
)

// copy the workflow file to the workflow run directory to keep the workflow file as it is to prevent potential
// changes when marshaling the workflow file again from context
if err := fs.CopyFile(src, dst); err != nil {
log.Errorf("failed to write workflow", "error", err, "workflow", c.Execution.WorkflowRun.Workflow.Name)
}
}

// SetJob sets the given job to the execution context.
Expand Down
38 changes: 38 additions & 0 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fs

import (
"encoding/json"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -114,3 +115,40 @@ func ReadYAMLFile[T any](file string, val *T) error {

return yaml.Unmarshal(data, val)
}

// CopyFile copies the given file from src to dst.
func CopyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
return err
}

err = out.Sync()
if err != nil {
return err
}

s, err := os.Stat(src)
if err != nil {
return err
}

err = os.Chmod(dst, s.Mode())
if err != nil {
return err
}

return nil
}

0 comments on commit 2925983

Please sign in to comment.