Added support for passing GITHUB_TOKEN

This commit is contained in:
Shubham Agrawal
2021-10-07 16:59:35 +05:30
parent 96d8f149da
commit 59bec2f619
2 changed files with 71 additions and 2 deletions

View File

@@ -11,9 +11,13 @@ import (
)
const (
workflowFile = "/tmp/workflow.yml"
webhookFile = "/tmp/webhook"
envFile = "/tmp/action.env"
secretFile = "/tmp/action.secrets"
workflowFile = "/tmp/workflow.yml"
)
var (
secrets = []string{"GITHUB_TOKEN"}
)
type (
@@ -42,11 +46,19 @@ func (p Plugin) Exec() error {
return err
}
if err := utils.CreateEnvAndSecretFile(envFile, secretFile, secrets); err != nil {
return err
}
cmdArgs := []string{
"-W",
workflowFile,
"-P",
fmt.Sprintf("ubuntu-latest=%s", p.Action.Image),
"--secret-file",
secretFile,
"--env-file",
envFile,
"-b",
"--detect-event",
}

57
utils/env.go Normal file
View File

@@ -0,0 +1,57 @@
package utils
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/pkg/errors"
)
func CreateEnvAndSecretFile(envFile, secretFile string, secrets []string) error {
envVars := getEnvVars()
envBuf := ""
for key, val := range envVars {
if !strings.HasPrefix(key, "PLUGIN_") && !Exists(secrets, key) {
envBuf += fmt.Sprintf("%s=%s\n", key, val)
}
}
secretBuf := ""
for _, secretName := range secrets {
if os.Getenv(secretName) != "" {
secretBuf += fmt.Sprintf("%s=%s\n", secretName, os.Getenv(secretName))
}
}
if err := ioutil.WriteFile(envFile, []byte(envBuf), 0644); err != nil {
return errors.Wrap(err, "failed to write environment variables file")
}
if err := ioutil.WriteFile(secretFile, []byte(secretBuf), 0644); err != nil {
return errors.Wrap(err, "failed to write secret variables file")
}
return nil
}
// Return environment variables set in a map format
func getEnvVars() map[string]string {
m := make(map[string]string)
for _, e := range os.Environ() {
if i := strings.Index(e, "="); i >= 0 {
m[e[:i]] = e[i+1:]
}
}
return m
}
func Exists(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}