forked from Ivasoft/github-actions
Merge pull request #4 from drone-plugins/secret_env
Added support for passing GITHUB_TOKEN
This commit is contained in:
@@ -30,7 +30,7 @@ docker build \
|
|||||||
|
|
||||||
## Plugin step usage
|
## Plugin step usage
|
||||||
|
|
||||||
Provide uses, with & env of github action to use in plugin step settings.
|
Provide uses, with & env of github action to use in plugin step settings. Provide GITHUB_TOKEN as environment variable if it is required for an action.
|
||||||
|
|
||||||
```console
|
```console
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
16
plugin.go
16
plugin.go
@@ -11,9 +11,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
workflowFile = "/tmp/workflow.yml"
|
|
||||||
webhookFile = "/tmp/webhook"
|
|
||||||
envFile = "/tmp/action.env"
|
envFile = "/tmp/action.env"
|
||||||
|
secretFile = "/tmp/action.secrets"
|
||||||
|
workflowFile = "/tmp/workflow.yml"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
secrets = []string{"GITHUB_TOKEN"}
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -42,11 +46,19 @@ func (p Plugin) Exec() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := utils.CreateEnvAndSecretFile(envFile, secretFile, secrets); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
cmdArgs := []string{
|
cmdArgs := []string{
|
||||||
"-W",
|
"-W",
|
||||||
workflowFile,
|
workflowFile,
|
||||||
"-P",
|
"-P",
|
||||||
fmt.Sprintf("ubuntu-latest=%s", p.Action.Image),
|
fmt.Sprintf("ubuntu-latest=%s", p.Action.Image),
|
||||||
|
"--secret-file",
|
||||||
|
secretFile,
|
||||||
|
"--env-file",
|
||||||
|
envFile,
|
||||||
"-b",
|
"-b",
|
||||||
"--detect-event",
|
"--detect-event",
|
||||||
}
|
}
|
||||||
|
|||||||
55
utils/env.go
Normal file
55
utils/env.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateEnvAndSecretFile(envFile, secretFile string, secrets []string) error {
|
||||||
|
envVars := getEnvVars()
|
||||||
|
|
||||||
|
actionEnvVars := make(map[string]string)
|
||||||
|
for key, val := range envVars {
|
||||||
|
if !strings.HasPrefix(key, "PLUGIN_") && !Exists(secrets, key) {
|
||||||
|
actionEnvVars[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
secretEnvVars := make(map[string]string)
|
||||||
|
for _, secretName := range secrets {
|
||||||
|
if os.Getenv(secretName) != "" {
|
||||||
|
secretEnvVars[secretName] = os.Getenv(secretName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := godotenv.Write(actionEnvVars, envFile); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to write environment variables file")
|
||||||
|
}
|
||||||
|
if err := godotenv.Write(secretEnvVars, secretFile); 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user