|
| 1 | +package ci |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | +) |
| 9 | + |
| 10 | +type githubWorkflow struct { |
| 11 | + Name string `yaml:"name"` |
| 12 | + On workflowTriggers `yaml:"on"` |
| 13 | + Jobs map[string]job `yaml:"jobs"` |
| 14 | +} |
| 15 | + |
| 16 | +type workflowTriggers struct { |
| 17 | + Push *pushTrigger `yaml:"push,omitempty"` |
| 18 | +} |
| 19 | + |
| 20 | +type pushTrigger struct { |
| 21 | + Branches []string `yaml:"branches,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +type job struct { |
| 25 | + RunsOn string `yaml:"runs-on"` |
| 26 | + Steps []step `yaml:"steps"` |
| 27 | +} |
| 28 | + |
| 29 | +type step struct { |
| 30 | + Name string `yaml:"name,omitempty"` |
| 31 | + Uses string `yaml:"uses,omitempty"` |
| 32 | + Run string `yaml:"run,omitempty"` |
| 33 | + With map[string]string `yaml:"with,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +func NewGitHubWorkflow(conf CIConfig) *githubWorkflow { |
| 37 | + runsOn := "ubuntu-latest" |
| 38 | + |
| 39 | + pushTrigger := newPushTrigger(conf.Branch()) |
| 40 | + |
| 41 | + var steps []step |
| 42 | + checkoutCode := newStep("Checkout code"). |
| 43 | + withUses("actions/checkout@v4") |
| 44 | + steps = append(steps, *checkoutCode) |
| 45 | + |
| 46 | + setupK8Context := newStep("Setup Kubernetes context"). |
| 47 | + withUses("azure/k8s-set-context@v4"). |
| 48 | + withActionConfig("method", "kubeconfig"). |
| 49 | + withActionConfig("kubeconfig", newSecret(conf.KubeconfigSecret())) |
| 50 | + steps = append(steps, *setupK8Context) |
| 51 | + |
| 52 | + installFuncCli := newStep("Install func cli"). |
| 53 | + withUses("gauron99/knative-func-action@main"). |
| 54 | + withActionConfig("version", "knative-v1.19.1"). |
| 55 | + withActionConfig("name", "func") |
| 56 | + steps = append(steps, *installFuncCli) |
| 57 | + |
| 58 | + deployFunc := newStep("Deploy function"). |
| 59 | + withRun("func deploy --registry=" + newVariable(conf.RegistryUrlVar()) + " -v") |
| 60 | + steps = append(steps, *deployFunc) |
| 61 | + |
| 62 | + return &githubWorkflow{ |
| 63 | + Name: conf.WorkflowName(), |
| 64 | + On: pushTrigger, |
| 65 | + Jobs: map[string]job{ |
| 66 | + "deploy": { |
| 67 | + RunsOn: runsOn, |
| 68 | + Steps: steps, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func newPushTrigger(branch string) workflowTriggers { |
| 75 | + result := workflowTriggers{ |
| 76 | + Push: &pushTrigger{Branches: []string{branch}}, |
| 77 | + } |
| 78 | + |
| 79 | + return result |
| 80 | +} |
| 81 | + |
| 82 | +func newStep(name string) *step { |
| 83 | + return &step{Name: name} |
| 84 | +} |
| 85 | + |
| 86 | +func (s *step) withUses(u string) *step { |
| 87 | + s.Uses = u |
| 88 | + return s |
| 89 | +} |
| 90 | + |
| 91 | +func (s *step) withRun(r string) *step { |
| 92 | + s.Run = r |
| 93 | + return s |
| 94 | +} |
| 95 | + |
| 96 | +func (s *step) withActionConfig(key, value string) *step { |
| 97 | + if s.With == nil { |
| 98 | + s.With = make(map[string]string) |
| 99 | + } |
| 100 | + |
| 101 | + s.With[key] = value |
| 102 | + |
| 103 | + return s |
| 104 | +} |
| 105 | + |
| 106 | +func newSecret(key string) string { |
| 107 | + return fmt.Sprintf("${{ secrets.%s }}", key) |
| 108 | +} |
| 109 | + |
| 110 | +func newVariable(key string) string { |
| 111 | + return fmt.Sprintf("${{ vars.%s }}", key) |
| 112 | +} |
| 113 | + |
| 114 | +func (gw *githubWorkflow) Export(path string, w WorkflowWriter) error { |
| 115 | + raw, err := gw.toYaml() |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + return w.Write(path, raw) |
| 121 | +} |
| 122 | + |
| 123 | +func (gw *githubWorkflow) toYaml() ([]byte, error) { |
| 124 | + var buf bytes.Buffer |
| 125 | + encoder := yaml.NewEncoder(&buf) |
| 126 | + encoder.SetIndent(2) |
| 127 | + |
| 128 | + if err := encoder.Encode(gw); err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + encoder.Close() |
| 132 | + |
| 133 | + return buf.Bytes(), nil |
| 134 | +} |
0 commit comments