-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.txt
More file actions
123 lines (95 loc) · 3.1 KB
/
Copy pathexamples.txt
File metadata and controls
123 lines (95 loc) · 3.1 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Signature Recipes
## 1. Multi-Task Pipelines with Captured Git SHA
Chain tasks on the CLI and embed your commit SHA into the binary using `ctx.RunOutput`.
```go
//go:build gobake
package bake_recipe
import (
"fmt"
"github.com/fezcode/gobake"
)
func Run(bake *gobake.Engine) error {
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return err
}
bake.Task("test", "Run tests", func(ctx *gobake.Context) error {
return ctx.Run("go", "test", "./...")
})
bake.TaskWithDeps("build", "Build with embedded git SHA", []string{"test"}, func(ctx *gobake.Context) error {
sha, err := ctx.RunOutput("git", "rev-parse", "--short", "HEAD")
if err != nil {
return err
}
ldflags := fmt.Sprintf("-X main.Version=%s -X main.Commit=%s", bake.Info.Version, sha)
ctx.Log("Building %s @ %s", bake.Info.Version, sha)
return ctx.Run("go", "build", "-ldflags", ldflags, "-o", "bin/app")
})
bake.TaskWithDeps("deploy", "Ship it", []string{"build"}, func(ctx *gobake.Context) error {
ctx.Log("Deploying %s...", bake.Info.Version)
return ctx.Run("scp", "bin/app", "deploy@server:/srv/app")
})
return nil
}
```
Then run the whole pipeline in one breath:
```bash
gobake test build deploy
```
## 2. Cross-Compilation for Multiple Platforms
Build for Windows, Linux, and macOS in a single task.
```go
//go:build gobake
package bake_recipe
import (
"fmt"
"github.com/fezcode/gobake"
)
func Run(bake *gobake.Engine) error {
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return err
}
bake.Task("release", "Build for all platforms", func(ctx *gobake.Context) error {
platforms := []struct {
OS, Arch, Ext string
}{
{"linux", "amd64", ""},
{"windows", "amd64", ".exe"},
{"darwin", "arm64", ""},
}
for _, p := range platforms {
output := fmt.Sprintf("dist/%s-%s-%s%s",
bake.Info.Name, p.OS, p.Arch, p.Ext)
if err := ctx.BakeBinary(p.OS, p.Arch, output, "-ldflags", "-s -w"); err != nil {
return err
}
}
return nil
})
return nil
}
```
## 3. Polyglot Builds with `RunIn`
Drive a JavaScript frontend and a Go backend from a single recipe.
```go
//go:build gobake
package bake_recipe
import (
"github.com/fezcode/gobake"
)
func Run(bake *gobake.Engine) error {
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return err
}
bake.Task("frontend", "Build the SPA", func(ctx *gobake.Context) error {
if err := ctx.RunIn("web", "npm", "ci"); err != nil {
return err
}
return ctx.RunIn("web", "npm", "run", "build")
})
bake.TaskWithDeps("backend", "Embed dist and build server", []string{"frontend"}, func(ctx *gobake.Context) error {
return ctx.BakeBinary("linux", "amd64", "bin/server")
})
return nil
}
```
A single `gobake backend` walks the dependency tree, builds the SPA in `web/`, then compiles the Go server with the embedded assets.