Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold a new action with --init (-i) flag #45

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions actions/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type RunnerConfig struct {
EventPath string // path to JSON file to use for event.json in containers, relative to WorkingDir
ReuseContainers bool // reuse containers to maintain state
ForcePull bool // force pulling of the image, if already present
Init bool // init a new action
InitRepo string // git repo to pull template from
}

type environmentApplier interface {
Expand Down
60 changes: 59 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -12,6 +13,7 @@ import (
gitignore "github.com/sabhiram/go-gitignore"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
git "gopkg.in/src-d/go-git.v4"
)

// Execute is the entry point to running the CLI
Expand All @@ -26,20 +28,23 @@ func Execute(ctx context.Context, version string) {
Version: version,
SilenceUsage: true,
}

rootCmd.Flags().BoolP("watch", "w", false, "watch the contents of the local repo and run when files change")
rootCmd.Flags().BoolP("list", "l", false, "list actions")
rootCmd.Flags().StringP("action", "a", "", "run action")
rootCmd.Flags().BoolVarP(&runnerConfig.Init, "init", "i", false, "init the local action")
rootCmd.Flags().StringVarP(&runnerConfig.InitRepo, "init-repo", "", "https://github.com/nektos/act", "init template repository")
rootCmd.Flags().BoolVarP(&runnerConfig.ReuseContainers, "reuse", "r", false, "reuse action containers to maintain state")
rootCmd.Flags().StringVarP(&runnerConfig.EventPath, "event", "e", "", "path to event JSON file")
rootCmd.Flags().BoolVarP(&runnerConfig.ForcePull, "pull", "p", false, "pull docker image(s) if already present")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&runnerConfig.Dryrun, "dryrun", "n", false, "dryrun mode")
rootCmd.PersistentFlags().StringVarP(&runnerConfig.WorkflowPath, "file", "f", "./.github/main.workflow", "path to workflow file")
rootCmd.PersistentFlags().StringVarP(&runnerConfig.WorkingDir, "directory", "C", ".", "working directory")

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}

}

func setupLogging(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -69,6 +74,15 @@ func newRunCommand(runnerConfig *actions.RunnerConfig) func(*cobra.Command, []st
}

func parseAndRun(cmd *cobra.Command, runnerConfig *actions.RunnerConfig) error {
// check if we should scaffold a new action
init, err := cmd.Flags().GetBool("init")
if err != nil {
return err
}
if init {
return initAction(runnerConfig)
}

// create the runner
runner, err := actions.NewRunner(runnerConfig)
if err != nil {
Expand Down Expand Up @@ -156,6 +170,50 @@ func watchAndRun(ctx context.Context, fn func() error) error {
return err
}

func initAction(config *actions.RunnerConfig) error {
// use the default event name if we don't have one
if config.EventName == "" {
config.EventName = "push"
}

log.Printf("Setting up a new action for %s event\n", config.EventName)
baseDir := filepath.Dir(config.WorkflowPath)

// check if workflow directory exists, skip setup if it's already configured
if _, err := os.Stat(baseDir); err == nil {
log.Println("Workspace directory already exists, skipping")
return nil
}

// prepare clone repository
repoCloneDir, err := ioutil.TempDir("", "act")
if err != nil {
log.Println("Can't get temp directory for clone:", err)
return err
}
defer os.RemoveAll(repoCloneDir)

// clone repository
_, err = git.PlainClone(repoCloneDir, false, &git.CloneOptions{
URL: config.InitRepo,
})
if err != nil {
log.Println("Can't clone repository:", err)
return err
}

// copy template
templateDir := filepath.Join(repoCloneDir, "templates/"+config.EventName)
if err := common.CopyDir(templateDir, baseDir); err != nil {
log.Println("Can't copy template:", err)
return err
}

log.Printf("Done. You can now run `act %s` to test things out!", config.EventName)

return nil
}

func drawGraph(runner actions.Runner) error {
eventNames := runner.ListEvents()
for _, eventName := range eventNames {
Expand Down
18 changes: 8 additions & 10 deletions common/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,29 @@ import (
)

// CopyFile copy file
func CopyFile(source string, dest string) (err error) {
func CopyFile(source string, dest string) error {
sourcefile, err := os.Open(source)
if err != nil {
return err
}

defer sourcefile.Close()

destfile, err := os.Create(dest)
if err != nil {
return err
}

defer destfile.Close()

_, err = io.Copy(destfile, sourcefile)
if err == nil {
sourceinfo, err := os.Stat(source)
if err != nil {
_ = os.Chmod(dest, sourceinfo.Mode())
}
if _, err = io.Copy(destfile, sourcefile); err != nil {
return err
}

sourceinfo, err := os.Stat(source)
if err != nil {
return err
}

return
return os.Chmod(dest, sourceinfo.Mode())
}

// CopyDir recursive copy of directory
Expand Down
3 changes: 3 additions & 0 deletions templates/push/action/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM alpine:latest
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
3 changes: 3 additions & 0 deletions templates/push/action/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo "Hello World"
8 changes: 8 additions & 0 deletions templates/push/main.workflow
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
workflow "basic workflow" {
on = "push"
resolves = ["example"]
}

action "example" {
uses = "./.github/action"
}