1
1
mirror of https://github.com/drone-plugins/github-actions synced 2024-11-15 02:56:25 +01:00
github.com-drone-plugins-gi.../plugin.go

99 lines
2.0 KiB
Go
Raw Permalink Normal View History

2021-09-27 07:18:58 +02:00
package plugin
import (
"fmt"
2021-10-17 20:32:56 +02:00
"io/ioutil"
2021-09-27 07:18:58 +02:00
"os"
"os/exec"
"strings"
"github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/utils"
2021-10-17 20:32:56 +02:00
"github.com/pkg/errors"
2021-09-27 07:18:58 +02:00
)
const (
2021-10-17 20:32:56 +02:00
envFile = "/tmp/action.env"
secretFile = "/tmp/action.secrets"
workflowFile = "/tmp/workflow.yml"
eventPayloadFile = "/tmp/event.json"
2021-10-07 13:29:35 +02:00
)
var (
secrets = []string{"GITHUB_TOKEN"}
2021-09-27 07:18:58 +02:00
)
type (
Action struct {
2021-10-17 20:32:56 +02:00
Uses string
With map[string]string
Env map[string]string
Image string
EventPayload string // Webhook event payload
Verbose bool
2021-09-27 07:18:58 +02:00
}
Plugin struct {
Action Action
Daemon daemon.Daemon // Docker daemon configuration
}
)
// Exec executes the plugin step
func (p Plugin) Exec() error {
if err := daemon.StartDaemon(p.Daemon); err != nil {
return err
}
if err := utils.CreateWorkflowFile(workflowFile, p.Action.Uses,
p.Action.With, p.Action.Env); err != nil {
return err
}
2021-10-07 13:29:35 +02:00
if err := utils.CreateEnvAndSecretFile(envFile, secretFile, secrets); err != nil {
return err
}
2021-09-27 07:18:58 +02:00
cmdArgs := []string{
"-W",
workflowFile,
"-P",
fmt.Sprintf("ubuntu-latest=%s", p.Action.Image),
2021-10-07 13:29:35 +02:00
"--secret-file",
secretFile,
"--env-file",
envFile,
2021-10-07 11:56:37 +02:00
"-b",
2021-09-27 07:18:58 +02:00
"--detect-event",
}
2021-10-17 20:32:56 +02:00
if p.Action.EventPayload != "" {
if err := ioutil.WriteFile(eventPayloadFile, []byte(p.Action.EventPayload), 0644); err != nil {
return errors.Wrap(err, "failed to write event payload to file")
}
cmdArgs = append(cmdArgs, "--eventpath", eventPayloadFile)
}
2021-09-27 07:18:58 +02:00
if p.Action.Verbose {
cmdArgs = append(cmdArgs, "-v")
}
cmd := exec.Command("act", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
// trace writes each command to stdout with the command wrapped in an xml
// tag so that it can be extracted and displayed in the logs.
func trace(cmd *exec.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}