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 <foo@auggie.dev>
  • Loading branch information
justaugustus committed Aug 13, 2023
1 parent 34a5a26 commit e23ad22
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/presubmits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ jobs:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: make license-check

- name: Run linter
- name: Run verify steps
uses: magefile/mage-action@v2
with:
version: v1.14.0
args: lint
args: verify

test:
runs-on: ubuntu-latest
Expand Down
21 changes: 0 additions & 21 deletions git-version

This file was deleted.

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
283 changes: 255 additions & 28 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,281 @@
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"
// Default target to run when none is specified
// If not set, running mage will list available targets
var Default = Verify

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

// Versions.
golangciVersion = "v1.50.1"
coverMode = "atomic"
coverProfile = "unit-coverage.out"
version, _ = sh.Output("./git-version")
repoPath = fmt.Sprintf("%s/%s", orgPath, proj)
ldFlags = fmt.Sprintf("-X %s/cmd.Version=%s", repoPath, version)
minGoVersion = "1.20"

// Environment variables.
envLDFLAGS = "GHJIRA_LDFLAGS"
envMinGoVersion = "MIN_GO_VERSION"

// Test variables.
coverMode = "atomic"
coverProfileFilename = "unit-coverage.out"
)

func init() {
os.Setenv("GO15VENDOREXPERIMENT", "1")
os.Setenv("CGO_ENABLED", "0")
// 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
}

// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
// Test runs various test functions
func Test() error {
// TODO(mage): Upstream should support setting covermode/coverprofile.
return sh.RunV(
"go",
"test",
"-v",
"-covermode", coverMode,
"-coverprofile", coverProfileFilename,
"./...",
)
}

// 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
}
*/

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

// TODO(mage): Support verifying go module.
// TODO(mage): Upstream should support setting minimum version.
/*
os.Setenv(envMinGoVersion, minGoVersion)
fmt.Println("Running go module linter...")
if err := mage.VerifyGoMod(scriptDir); err != nil {
return err
}
*/

fmt.Println("Running golangci-lint...")
if err := mage.RunGolangCILint(golangciVersion, 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",
)
}

// Run linter
func Lint() error {
return mage.RunGolangCILint(golangciVersion, true, "-v")
// BuildImages build bom image using ko
func BuildImages() error {
fmt.Println("Building images with ko...")

gitVersion := getVersion()
gitCommit := 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,
)
}

// Run linter with auto fix enabled
func LintFix() error {
return mage.RunGolangCILint(golangciVersion, true, "--fix")
// 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.")
}

// getBuildDateTime gets the build date and time
func getBuildDateTime() string {
result, _ := sh.Output("git", "log", "-1", "--pretty=%ct")
if result != "" {
sourceDateEpoch := fmt.Sprintf("@%s", result)
date, _ := sh.Output("date", "-u", "-d", sourceDateEpoch, "+%Y-%m-%dT%H:%M:%SZ")
return date
}

date, _ := sh.Output("date", "+%Y-%m-%dT%H:%M:%SZ")
return date
}

// getCommit gets the hash of the current commit
func getCommit() string {
commit, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return commit
}

// getGitState gets the state of the git repository
func getGitState() string {
_, err := sh.Output("git", "diff", "--quiet")
if err != nil {
return "dirty"
}

return "clean"
}

// getVersion gets a description of the commit, e.g. v0.30.1 (latest) or v0.30.1-32-gfe72ff73 (canary)
func getVersion() string {
version, _ := sh.Output("git", "describe", "--tags", "--match=v*")
if version != "" {
return version
}

// repo without any tags in it
return "v0.0.0"
}
Loading

0 comments on commit e23ad22

Please sign in to comment.