Files
github-actions/plugin.go
Roman Vaníček 5de3d0579f
All checks were successful
continuous-integration/drone/push Build is passing
Test if docker API 1.25 has Mounts. Upgrade act so that --container-options is supported
2023-07-28 20:58:49 +02:00

190 lines
4.4 KiB
Go

package plugin
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"bufio"
"path"
"context"
"runtime"
"github.com/drone-plugins/drone-github-actions/daemon"
"github.com/drone-plugins/drone-github-actions/utils"
"github.com/pkg/errors"
"time"
"github.com/opencontainers/selinux/go-selinux"
docker "github.com/docker/docker/client"
)
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
}
if err := utils.CreateWorkflowFile(workflowFile, p.Action.Uses,
p.Action.With, p.Action.Env); err != nil {
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,
"--detect-event",
}
// 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")
}
if p.Daemon.Disabled {
hostWorkDirPath, guestWorkDirPath, err := getWorkDirPath(p, context.Background())
if err != nil {
return errors.Wrap(err, "failed to locate working directory on the host. You may use DinD instead by disabling daemon_off.")
}
bindModifiers := ""
if runtime.GOOS == "darwin" {
bindModifiers = ":delegated"
}
if selinux.GetEnabled() {
bindModifiers = ":z"
}
cmdArgs = append(cmdArgs, "--container-options", fmt.Sprintf("--volume=%s:%s%s", hostWorkDirPath, guestWorkDirPath, bindModifiers))
} else {
cmdArgs = append(cmdArgs, "-b")
}
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 getWorkDirPath(p Plugin, ctx context.Context) (string, string, error) {
// Connect to the docker
docker, err := docker.NewClient("unix://" + daemon.StdDockerSocketPath, "v1.25", nil, nil)
if err != nil {
return "", "", errors.Wrap(err, "failed to create docker client")
}
defer docker.Close()
// Determine our container identifier
cntrId, err := getContainerId()
if err != nil {
return "", "", errors.Wrap(err, "failed to get our container id")
}
// Find the proper mount
cwd, err := os.Getwd()
if err != nil {
return "", "", errors.Wrap(err, "failed to locate the working directory in the container")
}
cntr, err := docker.ContainerInspect(ctx, cntrId)
if err != nil {
return "", "", errors.Wrap(err, "failed to inspect ourselves as a container")
}
for _, i := range cntr.Mounts {
if i.Destination == cwd {
return i.Source, i.Destination, nil
}
}
time.Sleep(86400 * time.Second)
return "", "", errors.New("mount point with working directory not found")
}
func getContainerId() (string, error) {
file, err := os.Open("/proc/self/cgroup")
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if !scanner.Scan() {
return "", errors.New("cgroup is empty")
}
// The file name is the container identifier
// Example: 12:rdma:/docker/a33940bf95ec6c57b6d79a3edfa784a1364ce840fb92c9a7f3951b9f7b0ccccb/docker/330d9b057fdd54b224cd38daf370524f7e8c567ccf95f33a2388f02c0b854982
cgroup := scanner.Text()
result := path.Base(cgroup)
if result == "." {
return "", errors.New("cgroup is unexpected")
}
return result, nil
}