Skip to content

Use GNU Make to build Golang projects with embedded build metadata

License

Notifications You must be signed in to change notification settings

srimaln91/go-make

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Use GNU Make to build Golang projects with embedded build details

This is a project which is intended to demonstrate how to use Makefiles to build go projects. Build version tag will be detected automatically and updated in the compilation time.

Build Status codecov Go Report Card GoDoc

Install GNU Make

Debian based systems

sudo apt install make

Red Hat based systems

yum install make

How to use

  1. Import Makefile into your project root
  2. Update main function as follows.
package main

import (
    buildMeta "github.com/srimaln91/go-make"
)

func main() {

    // Print binary details and terminate the program when --version flag provided.
    buildMeta.CheckVersion()

    // Optionally we can pass a printer that we can use to control the output
    // It acceptes any type which implements the `Printer` interface
    buildMeta.CheckVersion(buildMeta.PRINTER_SINGLE_LINE)

    // Starting the bootstrpping process
    // bootstrap.Start()
}

Make Commands configured in this project

This project contains a sample Makefile with some build tasks.

# List configured make tasks
make help

# Run unit tests
make test

# Build project
make build

# Run project
make run

# Clean build directory
make clean

Check Binary Version

# Can check binary version by passing --version flag
./build/vx.x.x/go-build-linux-amd64 --version
# Output
+----------------+------------+------------------------------------------+-------------+-------------------------+
| BINARY VERSION | GO VERSION |                GIT COMMIT                |   OS/ARCH   |          BUILT          |
+----------------+------------+------------------------------------------+-------------+-------------------------+
| v0.6.0         | go1.12.9   | c8bf7b40e9d842769b580b704931904197e0b713 | linux/amd64 | 2019-10-05-14:01:35-UTC |
+----------------+------------+------------------------------------------+-------------+-------------------------+

Version tags

  • If the source code is exactly on a build tag, the binary will be created with a clean tag. Like vx.x.x
  • If the source code is modified and have uncommited changes, the build tag would be vx.x.x-dirty (latest build tag - dirty)
  • If the source code has any untagged changes and the working directory is clean, the build tag will be vx.x.x-34fdr54(latest build tag - latest commit SHA)