Sample project Magefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build mage | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path" | |
"github.com/cabify/lana-common/build" | |
"github.com/magefile/mage/mg" | |
"github.com/magefile/mage/sh" | |
"github.com/magefile/mage/target" | |
) | |
const ( | |
repo = "github.com/cabify/lana-b2c-web" | |
name = "b2c-web" | |
buildImage = "gcr.io/lana-dev/golang:1.10-alpine" | |
) | |
var ( | |
ports = []string{"8011"} | |
cmds = []string{"b2c-web"} | |
) | |
// Build the project's commands. | |
func Build() error { | |
// mg.Deps(InstallVendor) | |
for _, cmd := range cmds { | |
err := buildCmd(cmd) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func buildCmd(cmd string) error { | |
changed, err := target.Dir("./"+cmd, ".") | |
if os.IsNotExist(err) || (err == nil && changed) { | |
err = sh.Run("go", "build", "./cmds/"+cmd) | |
} | |
return err | |
} | |
// Generate protobuf source files | |
func Protocol() error { | |
p := "./protocol" | |
args := []string{ | |
"--go_out=" + p, | |
"--proto_path=" + path.Join(os.Getenv("GOPATH"), "src", "github.com", "cabify", "lana-common", "protocol"), | |
"--twirp_out=" + p, | |
"--proto_path=" + p, | |
} | |
files, _ := ioutil.ReadDir(p) | |
for _, file := range files { | |
if path.Ext(file.Name()) == ".proto" { | |
args = append(args, path.Join(p, file.Name())) | |
} | |
} | |
err := sh.Run("protoc", args...) | |
return err | |
} | |
// Start up all the processes in development mode. | |
func Run() { | |
mg.Deps(Build) | |
sh.RunV("./start.sh") | |
} | |
// DockerBuild uses docker to build local binary | |
func DockerBuild() error { | |
for _, cmd := range cmds { | |
if err := dockerBuild(cmd); err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
// dockerBuild creates a binary suitable for using on a docker | |
// build. To make things faster, we set the architecture. | |
func dockerBuild(name string) error { | |
changed, err := target.Dir("./"+name, ".") | |
if os.IsNotExist(err) || (err == nil && changed) { | |
err := sh.Run("env", | |
"GOOS=linux", "GOARCH=amd64", | |
"go", "build", "./cmds/"+name) | |
return err | |
} | |
return nil | |
} | |
// dockerBuildComplete uses the base docker image to run the | |
// binary | |
func dockerBuildComplete(name string) error { | |
changed, err := target.Dir("./"+name, ".") | |
if os.IsNotExist(err) || (err == nil && changed) { | |
err := sh.Run("docker", "run", "--rm", | |
"-v", fmt.Sprintf("%v:/go:cached", os.Getenv("GOPATH")), | |
"-w", "/go/src/"+repo, | |
buildImage, | |
"go", "build", "./cmds/"+name, | |
) | |
return err | |
} | |
return nil | |
} | |
// DockerRun runs an interactive session with the binary, in the docker environment. | |
func DockerRun() error { | |
mg.Deps(DockerBuild) | |
params := []string{"run", "--rm", | |
"--name", name, | |
} | |
for _, port := range ports { | |
params = append(params, "-p", port+":"+port) | |
} | |
params = append(params, | |
"--network", "lana-local", | |
"-v", fmt.Sprintf("%v:/go:cached", os.Getenv("GOPATH")), | |
"-w", "/go/src/"+repo, | |
"-it", // Interactve! | |
buildImage, | |
"sh", "-c", "./start.sh", | |
) | |
return sh.Run("docker", params...) | |
} | |
func DockerBuildImage() error { | |
err := sh.Run("docker", "build", "-t", "lanadev/"+name, ".") | |
return err | |
} | |
// Ensure we have all vendor dependencies installed. | |
func InstallVendor() error { | |
err := sh.Run("dep", "ensure", "-v") | |
return err | |
} | |
// Release generates a version number, tags the repo, and pushes. | |
func Release() error { | |
return build.Release() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment