-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplugin.go
More file actions
163 lines (138 loc) · 3.94 KB
/
Copy pathplugin.go
File metadata and controls
163 lines (138 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package plugin
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/drone-plugins/drone-github-actions/cloner"
"github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
envFile = "/tmp/action.env"
secretFile = "/tmp/action.secrets"
workflowFile = "/tmp/workflow.yml"
eventPayloadFile = "/tmp/event.json"
)
var (
secrets = []string{"GITHUB_TOKEN"}
)
type (
Action struct {
Uses string
With map[string]string
Env map[string]string
Image string
EventPayload string // Webhook event payload
Actor string
Verbose bool
}
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
}
ctx := context.Background()
repoURL, ref, actionPath, ok := utils.ParseLookup(p.Action.Uses)
if !ok {
logrus.Warnf("Invalid 'uses' format: %s", p.Action.Uses)
}
logrus.Infof("Parsed 'uses' string. Repo: %s, Ref: %s, Path: %s", repoURL, ref, actionPath)
// When the action is pinned to a full commit SHA (e.g. owner/action@<sha>),
// the ref cannot be resolved via refs/heads/* or refs/tags/*. Pass it as the
// sha instead so the cloner checks out the commit directly.
sha := ""
if cloner.IsHash(ref) {
sha = ref
ref = ""
logrus.Infof("Ref is a commit SHA; cloning by sha: %s", sha)
}
// Clone the GH Action repository using `cloner` with parsed repo and ref
clone := cloner.NewCache(cloner.NewDefault())
codedir, cloneErr := clone.Clone(ctx, repoURL, ref, sha)
if cloneErr != nil {
logrus.Warnf("Failed to clone GH Action: %v", cloneErr)
} else {
logrus.Infof("Successfully cloned GH Action to %s", codedir)
}
outputFile := os.Getenv("DRONE_OUTPUT")
outputVars := []string{}
if codedir != "" {
actionDir, err := utils.ActionDir(codedir, actionPath)
if err != nil {
logrus.Warnf("Invalid action path %q: %v", actionPath, err)
} else {
outputVars, err = utils.ParseActionOutputs(actionDir)
if err != nil {
logrus.Warnf("Could not parse action.yml outputs from %s: %v", actionDir, err)
}
}
}
if len(outputVars) == 0 {
logrus.Infof("No outputs were found in action.yml for repo: %s", repoURL)
}
if err := utils.CreateWorkflowFile(workflowFile, p.Action.Uses,
p.Action.With, p.Action.Env, outputFile, outputVars); err != nil {
return err
}
if err := utils.CreateEnvAndSecretFile(envFile, secretFile, secrets); err != nil {
return err
}
outputFilePath := GetDirPath(outputFile)
containerOptions := fmt.Sprintf("-v=%s:%s", outputFilePath, outputFilePath)
cmdArgs := []string{
"-W",
workflowFile,
"-P",
fmt.Sprintf("ubuntu-latest=%s", p.Action.Image),
"--secret-file",
secretFile,
"--env-file",
envFile,
"-b",
"--detect-event",
"--container-options",
fmt.Sprintf("\"%s\"", containerOptions),
}
// optional arguments
if p.Action.Actor != "" {
cmdArgs = append(cmdArgs, "--actor")
cmdArgs = append(cmdArgs, p.Action.Actor)
}
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)
}
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, " "))
}
func GetDirPath(filePath string) string {
return filepath.Dir(filePath)
}