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

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  // ActionRunsUsing is the type of runner for the action
    12  type ActionRunsUsing string
    13  
    14  func (a *ActionRunsUsing) UnmarshalYAML(unmarshal func(interface{}) error) error {
    15  	var using string
    16  	if err := unmarshal(&using); err != nil {
    17  		return err
    18  	}
    19  
    20  	// Force input to lowercase for case insensitive comparison
    21  	format := ActionRunsUsing(strings.ToLower(using))
    22  	switch format {
    23  	case ActionRunsUsingNode20, ActionRunsUsingNode16, ActionRunsUsingNode12, ActionRunsUsingDocker, ActionRunsUsingComposite:
    24  		*a = format
    25  	default:
    26  		return fmt.Errorf(fmt.Sprintf("The runs.using key in action.yml must be one of: %v, got %s", []string{
    27  			ActionRunsUsingComposite,
    28  			ActionRunsUsingDocker,
    29  			ActionRunsUsingNode12,
    30  			ActionRunsUsingNode16,
    31  			ActionRunsUsingNode20,
    32  		}, format))
    33  	}
    34  	return nil
    35  }
    36  
    37  const (
    38  	// ActionRunsUsingNode12 for running with node12
    39  	ActionRunsUsingNode12 = "node12"
    40  	// ActionRunsUsingNode16 for running with node16
    41  	ActionRunsUsingNode16 = "node16"
    42  	// ActionRunsUsingNode20 for running with node20
    43  	ActionRunsUsingNode20 = "node20"
    44  	// ActionRunsUsingDocker for running with docker
    45  	ActionRunsUsingDocker = "docker"
    46  	// ActionRunsUsingComposite for running composite
    47  	ActionRunsUsingComposite = "composite"
    48  )
    49  
    50  // ActionRuns are a field in Action
    51  type ActionRuns struct {
    52  	Using      ActionRunsUsing   `yaml:"using"`
    53  	Env        map[string]string `yaml:"env"`
    54  	Main       string            `yaml:"main"`
    55  	Pre        string            `yaml:"pre"`
    56  	PreIf      string            `yaml:"pre-if"`
    57  	Post       string            `yaml:"post"`
    58  	PostIf     string            `yaml:"post-if"`
    59  	Image      string            `yaml:"image"`
    60  	Entrypoint string            `yaml:"entrypoint"`
    61  	Args       []string          `yaml:"args"`
    62  	Steps      []Step            `yaml:"steps"`
    63  }
    64  
    65  // 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.
    66  type Action struct {
    67  	Name        string            `yaml:"name"`
    68  	Author      string            `yaml:"author"`
    69  	Description string            `yaml:"description"`
    70  	Inputs      map[string]Input  `yaml:"inputs"`
    71  	Outputs     map[string]Output `yaml:"outputs"`
    72  	Runs        ActionRuns        `yaml:"runs"`
    73  	Branding    struct {
    74  		Color string `yaml:"color"`
    75  		Icon  string `yaml:"icon"`
    76  	} `yaml:"branding"`
    77  }
    78  
    79  // 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.
    80  type Input struct {
    81  	Description string `yaml:"description"`
    82  	Required    bool   `yaml:"required"`
    83  	Default     string `yaml:"default"`
    84  }
    85  
    86  // 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.
    87  type Output struct {
    88  	Description string `yaml:"description"`
    89  	Value       string `yaml:"value"`
    90  }
    91  
    92  // ReadAction reads an action from a reader
    93  func ReadAction(in io.Reader) (*Action, error) {
    94  	a := new(Action)
    95  	err := yaml.NewDecoder(in).Decode(a)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	// set defaults
   101  	if a.Runs.PreIf == "" {
   102  		a.Runs.PreIf = "always()"
   103  	}
   104  	if a.Runs.PostIf == "" {
   105  		a.Runs.PostIf = "always()"
   106  	}
   107  
   108  	return a, nil
   109  }