github.com/nektos/act@v0.2.83/pkg/model/action.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/nektos/act/pkg/schema"
     9  	"gopkg.in/yaml.v3"
    10  )
    11  
    12  // ActionRunsUsing is the type of runner for the action
    13  type ActionRunsUsing string
    14  
    15  func (a *ActionRunsUsing) UnmarshalYAML(unmarshal func(interface{}) error) error {
    16  	var using string
    17  	if err := unmarshal(&using); err != nil {
    18  		return err
    19  	}
    20  
    21  	// Force input to lowercase for case insensitive comparison
    22  	format := ActionRunsUsing(strings.ToLower(using))
    23  	switch format {
    24  	case ActionRunsUsingNode24, ActionRunsUsingNode20, ActionRunsUsingNode16, ActionRunsUsingNode12, ActionRunsUsingDocker, ActionRunsUsingComposite:
    25  		*a = format
    26  	default:
    27  		return fmt.Errorf("The runs.using key in action.yml must be one of: %v, got %s", []string{
    28  			ActionRunsUsingComposite,
    29  			ActionRunsUsingDocker,
    30  			ActionRunsUsingNode12,
    31  			ActionRunsUsingNode16,
    32  			ActionRunsUsingNode20,
    33  			ActionRunsUsingNode24,
    34  		}, format)
    35  	}
    36  	return nil
    37  }
    38  
    39  const (
    40  	// ActionRunsUsingNode12 for running with node12
    41  	ActionRunsUsingNode12 = "node12"
    42  	// ActionRunsUsingNode16 for running with node16
    43  	ActionRunsUsingNode16 = "node16"
    44  	// ActionRunsUsingNode20 for running with node20
    45  	ActionRunsUsingNode20 = "node20"
    46  	// ActionRunsUsingNode24 for running with node24
    47  	ActionRunsUsingNode24 = "node24"
    48  	// ActionRunsUsingDocker for running with docker
    49  	ActionRunsUsingDocker = "docker"
    50  	// ActionRunsUsingComposite for running composite
    51  	ActionRunsUsingComposite = "composite"
    52  )
    53  
    54  func (a ActionRunsUsing) IsNode() bool {
    55  	switch a {
    56  	case ActionRunsUsingNode12, ActionRunsUsingNode16, ActionRunsUsingNode20, ActionRunsUsingNode24:
    57  		return true
    58  	default:
    59  		return false
    60  	}
    61  }
    62  
    63  func (a ActionRunsUsing) IsDocker() bool {
    64  	return a == ActionRunsUsingDocker
    65  }
    66  
    67  func (a ActionRunsUsing) IsComposite() bool {
    68  	return a == ActionRunsUsingComposite
    69  }
    70  
    71  // ActionRuns are a field in Action
    72  type ActionRuns struct {
    73  	Using          ActionRunsUsing   `yaml:"using"`
    74  	Env            map[string]string `yaml:"env"`
    75  	Main           string            `yaml:"main"`
    76  	Pre            string            `yaml:"pre"`
    77  	PreIf          string            `yaml:"pre-if"`
    78  	Post           string            `yaml:"post"`
    79  	PostIf         string            `yaml:"post-if"`
    80  	Image          string            `yaml:"image"`
    81  	PreEntrypoint  string            `yaml:"pre-entrypoint"`
    82  	Entrypoint     string            `yaml:"entrypoint"`
    83  	PostEntrypoint string            `yaml:"post-entrypoint"`
    84  	Args           []string          `yaml:"args"`
    85  	Steps          []Step            `yaml:"steps"`
    86  }
    87  
    88  // Action describes a metadata file for GitHub actions. The metadata filename must be either action.yml or action.yaml. The data in the metadata file defines the inputs, outputs and main entrypoint for your action.
    89  type Action struct {
    90  	Name        string            `yaml:"name"`
    91  	Author      string            `yaml:"author"`
    92  	Description string            `yaml:"description"`
    93  	Inputs      map[string]Input  `yaml:"inputs"`
    94  	Outputs     map[string]Output `yaml:"outputs"`
    95  	Runs        ActionRuns        `yaml:"runs"`
    96  	Branding    struct {
    97  		Color string `yaml:"color"`
    98  		Icon  string `yaml:"icon"`
    99  	} `yaml:"branding"`
   100  }
   101  
   102  func (a *Action) UnmarshalYAML(node *yaml.Node) error {
   103  	// Validate the schema before deserializing it into our model
   104  	if err := (&schema.Node{
   105  		Definition: "action-root",
   106  		Schema:     schema.GetActionSchema(),
   107  	}).UnmarshalYAML(node); err != nil {
   108  		return err
   109  	}
   110  	type ActionDefault Action
   111  	return node.Decode((*ActionDefault)(a))
   112  }
   113  
   114  // Input parameters allow you to specify data that the action expects to use during runtime. GitHub stores input parameters as environment variables. Input ids with uppercase letters are converted to lowercase during runtime. We recommended using lowercase input ids.
   115  type Input struct {
   116  	Description string `yaml:"description"`
   117  	Required    bool   `yaml:"required"`
   118  	Default     string `yaml:"default"`
   119  }
   120  
   121  // Output parameters allow you to declare data that an action sets. Actions that run later in a workflow can use the output data set in previously run actions. For example, if you had an action that performed the addition of two inputs (x + y = z), the action could output the sum (z) for other actions to use as an input.
   122  type Output struct {
   123  	Description string `yaml:"description"`
   124  	Value       string `yaml:"value"`
   125  }
   126  
   127  // ReadAction reads an action from a reader
   128  func ReadAction(in io.Reader) (*Action, error) {
   129  	a := new(Action)
   130  	err := yaml.NewDecoder(in).Decode(a)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	// set defaults
   136  	if a.Runs.PreIf == "" {
   137  		a.Runs.PreIf = "always()"
   138  	}
   139  	if a.Runs.PostIf == "" {
   140  		a.Runs.PostIf = "always()"
   141  	}
   142  
   143  	return a, nil
   144  }