Skip to content

Commit

Permalink
magefile: Adopt patterns from Kubernetes Release Engineering repos
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <[email protected]>
  • Loading branch information
justaugustus committed Aug 12, 2023
1 parent e01b736 commit 13df4cd
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/spf13/viper v1.16.0
github.com/trivago/tgo v1.0.7
github.com/uwu-tools/go-jira/v2 v2.0.0-20230801175343-52f822b5cb80
github.com/uwu-tools/magex v0.10.0
golang.org/x/oauth2 v0.11.0
golang.org/x/term v0.11.0
sigs.k8s.io/release-sdk v0.10.3
Expand Down Expand Up @@ -69,7 +70,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/uwu-tools/magex v0.10.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/crypto v0.12.0 // indirect
Expand Down
213 changes: 204 additions & 9 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/magefile/mage/sh"
"sigs.k8s.io/release-utils/mage"

"github.com/uwu-tools/magex/pkg"
)

/*
var (
proj = "gh-jira-issue-sync"
orgPath = "github.com/uwu-tools"
Expand All @@ -41,24 +45,215 @@ func init() {
os.Setenv("GO15VENDOREXPERIMENT", "1")
os.Setenv("CGO_ENABLED", "0")
}
*/

// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
var Default = Verify

const (
binDir = "bin"
moduleName = "github.com/uwu-tools/gh-jira-issue-sync"
scriptDir = "scripts"

// Environment variables.
envLDFLAGS = "GHJIRA_LDFLAGS"
)

// All runs all targets for this repository
func All() error {
if err := Verify(); err != nil {
return err
}

if err := Test(); err != nil {
return err
}

return nil
}

// Test runs various test functions
func Test() error {
// TODO(mage): Support covermode/coverprofile
/*
coverMode := "atomic"
coverProfile := "unit-coverage.out"
return sh.RunV("go", "test", "-v", "-covermode", coverMode, "-coverprofile", coverProfile, "./...")
*/

if err := mage.TestGo(true); err != nil {
return err
}

return nil
}

// Create executable to bin/
// Verify runs repository verification scripts
func Verify() error {
fmt.Println("Ensuring mage is available...")
if err := pkg.EnsureMage(""); err != nil {
return err
}

// TODO(mage): Support verifying headers
/*
fmt.Println("Running copyright header checks...")
if err := mage.VerifyBoilerplate("v0.2.5", binDir, boilerplateDir, false); err != nil {
return err
}
*/

// TODO(mage): Support verifying external dependencies
/*
fmt.Println("Running external dependency checks...")
if err := mage.VerifyDeps("v0.3.0", "", "", true); err != nil {
return err
}
*/

fmt.Println("Running go module linter...")
if err := mage.VerifyGoMod(scriptDir); err != nil {
return err
}

fmt.Println("Running golangci-lint...")
if err := mage.RunGolangCILint("", false); err != nil {
return err
}

if err := Build(); err != nil {
return err
}

return nil
}

// Build runs go build
func Build() error {
return sh.RunV("go", "build", "-o", fmt.Sprintf("bin/%s", proj), "-ldflags", ldFlags, repoPath)
fmt.Println("Running go build...")

ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}

os.Setenv(envLDFLAGS, ldFlag)

if err := mage.VerifyBuild(scriptDir); err != nil {
return err
}

fmt.Println("Binaries available in the output directory.")
return nil
}

// Remove bin
func Clean() error {
return sh.Rm("bin")
func BuildBinaries() error {
fmt.Println("Building binaries with goreleaser...")

ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}

os.Setenv(envLDFLAGS, ldFlag)

return sh.RunV("goreleaser", "release", "--clean")
}

// Run tests
func Test() error {
return sh.RunV("go", "test", "-v", "-covermode", coverMode, "-coverprofile", coverProfile, "./...")
func BuildBinariesSnapshot() error {
fmt.Println("Building binaries with goreleaser in snapshot mode...")

ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}

os.Setenv(envLDFLAGS, ldFlag)

return sh.RunV("goreleaser", "release", "--clean",
"--snapshot", "--skip-sign")
}

// BuildImages build bom image using ko
func BuildImages() error {
fmt.Println("Building images with ko...")

gitVersion := getVersion()

Check failure on line 182 in magefile.go

View workflow job for this annotation

GitHub Actions / test

undefined: getVersion

Check failure on line 182 in magefile.go

View workflow job for this annotation

GitHub Actions / build

undefined: getVersion

Check failure on line 182 in magefile.go

View workflow job for this annotation

GitHub Actions / verify

undefined: getVersion
gitCommit := getCommit()

Check failure on line 183 in magefile.go

View workflow job for this annotation

GitHub Actions / test

undefined: getCommit

Check failure on line 183 in magefile.go

View workflow job for this annotation

GitHub Actions / build

undefined: getCommit

Check failure on line 183 in magefile.go

View workflow job for this annotation

GitHub Actions / verify

undefined: getCommit
ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}
os.Setenv(envLDFLAGS, ldFlag)
os.Setenv("KOCACHE", "/tmp/ko")

if os.Getenv("KO_DOCKER_REPO") == "" {
return errors.New("missing KO_DOCKER_REPO environment variable")
}

return sh.RunV(
"ko",
"build",
"--bare",
"--platform=all",
"--tags", gitVersion,
"--tags", gitCommit,
moduleName,
)
}

// BuildImagesLocal build images locally and not push
func BuildImagesLocal() error {
fmt.Println("Building image with ko for local test...")
if err := mage.EnsureKO(""); err != nil {
return err
}

ldFlag, err := mage.GenerateLDFlags()
if err != nil {
return err
}

os.Setenv(envLDFLAGS, ldFlag)
os.Setenv("KOCACHE", "/tmp/ko")

return sh.RunV(
"ko",
"build",
"--bare",
"--local",
"--platform=linux/amd64",
moduleName,
)
}

func BuildStaging() error {
fmt.Println("Ensuring mage is available...")
if err := pkg.EnsureMage(""); err != nil {
return err
}

if err := mage.EnsureKO(""); err != nil {
return err
}

if err := BuildImages(); err != nil {
return fmt.Errorf("building the images: %w", err)
}

return nil
}

func Clean() {
fmt.Println("Cleaning workspace...")
toClean := []string{"output"}

for _, clean := range toClean {
sh.Rm(clean)
}

fmt.Println("Done.")
}

// Run linter
Expand Down

0 comments on commit 13df4cd

Please sign in to comment.