2024-09-01 17:38:53 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2024-09-02 15:13:59 +00:00
|
|
|
driver "courgette/internal/driver"
|
2024-09-01 17:38:53 +00:00
|
|
|
logger "courgette/internal/logging"
|
2024-09-02 15:13:59 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2024-09-01 17:38:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ActionsManager struct {
|
|
|
|
git CliClient
|
2024-09-02 15:13:59 +00:00
|
|
|
// Action cache location.
|
|
|
|
cacheRoot string
|
2024-09-01 17:38:53 +00:00
|
|
|
}
|
|
|
|
|
2024-09-03 02:55:29 +00:00
|
|
|
func NewActionsManager(cacheRoot string) *ActionsManager {
|
|
|
|
return &ActionsManager{
|
2024-09-02 15:13:59 +00:00
|
|
|
git: GitClient{},
|
|
|
|
cacheRoot: cacheRoot,
|
2024-09-01 17:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefetches and caches the action defined by <actionName> at <destination>.
|
|
|
|
//
|
|
|
|
// <actionName> is expected to be the full url of the repository where
|
|
|
|
// the action lives.
|
2024-09-02 15:13:59 +00:00
|
|
|
func (a ActionsManager) PrefetchAction(actionName string) error {
|
|
|
|
destination := filepath.Join(a.cacheRoot, GetActionKey(actionName))
|
2024-09-01 17:38:53 +00:00
|
|
|
logger.Info("Prefetching action: %s to %s", actionName, destination)
|
|
|
|
|
|
|
|
return a.git.Clone(actionName, destination)
|
|
|
|
}
|
2024-09-02 15:13:59 +00:00
|
|
|
|
|
|
|
func (a ActionsManager) UseAction(actionName string, containerId string, options driver.CommandOptions) error {
|
|
|
|
actionKey := GetActionKey(actionName)
|
|
|
|
hostActionRoot := filepath.Join(a.cacheRoot, actionKey)
|
|
|
|
actionDefPath := filepath.Join(hostActionRoot, "action.yml")
|
|
|
|
actionDef, _ := GetDefinitionFromFile(actionDefPath)
|
|
|
|
|
|
|
|
containerActionRoot := filepath.Join("/cache", actionKey)
|
|
|
|
actionMain := filepath.Join(containerActionRoot, actionDef.Runs.Main)
|
|
|
|
command := fmt.Sprintf("node %s", actionMain)
|
|
|
|
|
|
|
|
result := driver.PodmanDriver{}.Exec(containerId, command, options)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.ExitCode != 0 {
|
|
|
|
return errors.New("Action used returned non-zero exit code.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|