Repository navigation
Expand file tree
/
Copy pathworkflow.go
More file actions
77 lines (66 loc) · 1.84 KB
/
Copy pathworkflow.go
File metadata and controls
77 lines (66 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package github
import (
"bytes"
"os"
"path/filepath"
"webtyp.com/deploy"
"webtyp.com/ghaction"
)
const (
workflowDir = ".github/workflows"
workflowFileName = "deploy.yml"
workflowRelPath = ".github/workflows/deploy.yml"
workflowGitRef = "origin/main:.github/workflows/deploy.yml"
workflowName = "Deploy"
workflowJobName = "deploy"
defaultBranch = "main"
permContents = "contents"
permRead = "read"
workflowHeader = "# GENERATED by webtyp from config/deploy.go — do not edit. Change config/deploy.go and run Provision from the webtyp DEPLOY tab.\n"
)
func render(p deploy.Plan, job deploy.Job) []byte {
var env []ghaction.KeyValue
for _, s := range job.Secrets {
env = append(env, ghaction.KeyValue{Key: s, Value: "${{ secrets." + s + " }}"})
}
var with []ghaction.KeyValue
for _, kv := range job.With {
with = append(with, ghaction.KeyValue{Key: kv.Name, Value: kv.Value})
}
w := ghaction.Workflow{
Name: workflowName,
On: ghaction.On{
Push: &ghaction.Push{Branches: []string{defaultBranch}},
WorkflowDispatch: &ghaction.WorkflowDispatch{},
},
Permissions: map[string]string{
permContents: permRead,
},
Jobs: map[string]ghaction.Job{
workflowJobName: {
RunsOn: ghaction.DefaultRunsOn,
Env: env,
Steps: []ghaction.WorkflowStep{
ghaction.CheckoutStep(),
{
Uses: job.Uses,
With: with,
},
},
},
},
}
return append([]byte(workflowHeader), w.Render()...)
}
func (c *CI) Provision(p deploy.Plan, job deploy.Job) error {
path := filepath.Join(c.rootDir, workflowDir, workflowFileName)
content := render(p, job)
existing, err := os.ReadFile(path)
if err == nil && bytes.Equal(existing, content) {
return nil
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
return os.WriteFile(path, content, 0644)
}