Skip to content

Commit

Permalink
Merge pull request #445 from hellofresh/patch/simplified-build
Browse files Browse the repository at this point in the history
EES-574 Simplified application build
  • Loading branch information
vgarvardt authored Aug 13, 2020
2 parents 49b5ce3 + d17035b commit 6df68a4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 34 deletions.
18 changes: 4 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ OK_COLOR=\033[32;01m
ERROR_COLOR=\033[31;01m
WARN_COLOR=\033[33;01m

# The import path is the unique absolute name of your repository.
# All subpackages should always be imported as relative to it.
# If you change this, run `make clean`.
PKG_SRC := github.com/hellofresh/janus
.PHONY: all lint test-unit test-integration test-features build

.PHONY: all clean lint test-unit build

all: clean lint test-unit test-integration test-features lint build
all: test-unit build

build:
@echo "$(OK_COLOR)==> Building... $(NO_COLOR)"
@/bin/sh -c "JANUS_BUILD_ONLY_DEFAULT=$(JANUS_BUILD_ONLY_DEFAULT) PKG_SRC=$(PKG_SRC) VERSION=$(VERSION) ./build/build.sh"
@/bin/sh -c "JANUS_BUILD_ONLY_DEFAULT=$(JANUS_BUILD_ONLY_DEFAULT) VERSION=$(VERSION) ./build/build.sh"

test-unit:
@echo "$(OK_COLOR)==> Running unit tests$(NO_COLOR)"
Expand All @@ -25,17 +20,12 @@ test-integration: _mocks
@go test -cover -tags=integration -coverprofile=coverage.txt -covermode=atomic ./...

test-features: _mocks
@/bin/sh -c "JANUS_BUILD_ONLY_DEFAULT=1 PKG_SRC=$(PKG_SRC) ./build/build.sh"
@/bin/sh -c "JANUS_BUILD_ONLY_DEFAULT=1 ./build/build.sh"
@/bin/sh -c "./build/features.sh"

lint:
@echo "$(OK_COLOR)==> Linting with golangci-lint running in docker container$(NO_COLOR)"
@docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.30.0 golangci-lint run -v

clean:
@echo "$(OK_COLOR)==> Cleaning project$(NO_COLOR)"
@go clean
@rm -rf bin $GOPATH/bin

_mocks:
@/bin/sh -c "./build/mocks.sh"
20 changes: 10 additions & 10 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ rm -f dist/janus*

# Check if VERSION variable set and not empty, otherwise set to default value
if [ -z "$VERSION" ]; then
VERSION="0.0.1-dev"
VERSION="0.0.1-dev"
fi
echo "Building application version $VERSION"

echo "Building default binary"
CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X github.com/hellofresh/janus/cmd.version=${VERSION}" -o "dist/janus" $PKG_SRC
CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/janus"

if [ ! -z "${JANUS_BUILD_ONLY_DEFAULT}" ]; then
echo "Only default binary was requested to build"
Expand All @@ -23,18 +23,18 @@ fi
OS_PLATFORM_ARG=(linux darwin windows freebsd openbsd)
OS_ARCH_ARG=(386 amd64)
for OS in ${OS_PLATFORM_ARG[@]}; do
for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..."
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X github.com/hellofresh/janus/cmd.version=${VERSION}" -o "dist/janus_$OS-$ARCH" $PKG_SRC
done
for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..."
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/janus_$OS-$ARCH"
done
done

# Build arm binaries
OS_PLATFORM_ARG=(linux)
OS_ARCH_ARG=(arm arm64)
for OS in ${OS_PLATFORM_ARG[@]}; do
for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..."
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/janus_$OS-$ARCH" $PKG_SRC
done
for ARCH in ${OS_ARCH_ARG[@]}; do
echo "Building binary for $OS/$ARCH..."
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=0 go build -ldflags "-s -w" -ldflags "-X main.version=${VERSION}" -o "dist/janus_$OS-$ARCH"
done
done
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var configFile string

// NewRootCmd creates a new instance of the root command
func NewRootCmd() *cobra.Command {
func NewRootCmd(version string) *cobra.Command {
ctx := context.Background()

cmd := &cobra.Command{
Expand All @@ -26,8 +26,8 @@ Complete documentation is available at https://hellofresh.gitbooks.io/janus`,
cmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file (default is $PWD/janus.toml)")

cmd.AddCommand(NewCheckCmd(ctx))
cmd.AddCommand(NewVersionCmd(ctx))
cmd.AddCommand(NewServerStartCmd(ctx))
cmd.AddCommand(NewVersionCmd(ctx, version))
cmd.AddCommand(NewServerStartCmd(ctx, version))

return cmd
}
6 changes: 3 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ type ServerStartOptions struct {
}

// NewServerStartCmd creates a new http server command
func NewServerStartCmd(ctx context.Context) *cobra.Command {
func NewServerStartCmd(ctx context.Context, version string) *cobra.Command {
opts := &ServerStartOptions{}

cmd := &cobra.Command{
Use: "start",
Short: "Starts a Janus web server",
RunE: func(cmd *cobra.Command, args []string) error {
return RunServerStart(ctx, opts)
return RunServerStart(ctx, opts, version)
},
}

Expand All @@ -51,7 +51,7 @@ func NewServerStartCmd(ctx context.Context) *cobra.Command {
}

// RunServerStart is the run command to start Janus
func RunServerStart(ctx context.Context, opts *ServerStartOptions) error {
func RunServerStart(ctx context.Context, opts *ServerStartOptions, version string) error {
// all the logging configurations are initialised in initLog() later,
// but we try to initialise Writer (STDIN/STDERR/etc.) as early as possible manually
// to avoid loosing logs in systems heavily relying on them (e.g. running in docker)
Expand Down
4 changes: 1 addition & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"github.com/spf13/cobra"
)

var version = "0.0.0-dev"

// NewVersionCmd creates a new version command
func NewVersionCmd(ctx context.Context) *cobra.Command {
func NewVersionCmd(ctx context.Context, version string) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version information",
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
log "github.com/sirupsen/logrus"
)

var version = "0.0.0-dev"

func main() {
rootCmd := cmd.NewRootCmd()
rootCmd := cmd.NewRootCmd(version)

if err := rootCmd.Execute(); err != nil {
log.WithError(err).Error(err.Error())
Expand Down

0 comments on commit 6df68a4

Please sign in to comment.