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.../utils/workflow.go

76 lines
1.4 KiB
Go
Raw Normal View History

2021-09-27 07:18:58 +02:00
package utils
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type workflow struct {
Name string `yaml:"name"`
On string `yaml:"on"`
Jobs map[string]job `yaml:"jobs"`
}
type job struct {
Name string `yaml:"name"`
RunsOn string `yaml:"runs-on"`
Steps []step `yaml:"steps"`
}
type step struct {
Uses string `yaml:"uses"`
With map[string]string `yaml:"with"`
Env map[string]string `yaml:"env"`
}
const (
workflowEvent = "push"
workflowName = "drone-github-action"
jobName = "action"
runsOnImage = "ubuntu-latest"
)
func CreateWorkflowFile(ymlFile string, action string,
with map[string]string, env map[string]string) error {
j := job{
Name: jobName,
RunsOn: runsOnImage,
Steps: []step{
{
Uses: action,
With: with,
Env: env,
},
},
}
wf := &workflow{
Name: workflowName,
On: getWorkflowEvent(),
Jobs: map[string]job{
jobName: j,
},
}
out, err := yaml.Marshal(&wf)
if err != nil {
return errors.Wrap(err, "failed to create action workflow yml")
}
if err = ioutil.WriteFile(ymlFile, out, 0644); err != nil {
return errors.Wrap(err, "failed to write yml workflow file")
}
return nil
}
func getWorkflowEvent() string {
buildEvent := os.Getenv("DRONE_BUILD_EVENT")
if buildEvent == "push" || buildEvent == "pull_request" || buildEvent == "tag" {
return buildEvent
}
return "custom"
}