Makefile revisited

Makefiles

A little rant about Make, was created in April 1976 and was implemented using C, first used on Unix and the file format is Makefile, I find make and Makefiles to complement my build flow using Golang, Docker and various other projects, it is really nice to have a directory layout with a Makefile, where you know exactly how and what is needed to build the project.

These last years I've spent coding Golang and preferred tool to orchestrate build and deploy is make.

My Makefiles normally start with variables in the beginning, setting the SHELL, various project related environmental variables and then we begin to flesh out the targets and dependencies.

I've come to the conclusion on the preferred way to compile and dockerize using Makefiles, and below I will show a simple skeleton

    SHELL ?= /bin/sh
    GIT_COMMIT = $(shell git rev-list HEAD -1)
    RELEASE?=branch
    TAG?=projectName

    ## BELOW TARGETS TO BE RUN
    filename_x64:
        go build \
            -trimpath \
            -race \
            -ldflags "-X main.CommitID=$(GIT_COMMIT)" \
            -o $@ }
            ./cmd/app
    build: filename_x64

    .PHONY=strip
    strip: build
        strip -s filename_x64
    
    .PHONY=pack
    pack: build strip
        upx -q filename_x64

    .PHONY=clean
    clean:
        rm -rf filename_x64

    .PHONY=docker-build
    docker-build: build
        @docker build \
            --compress \
            --rm \
            -t $(TAG) \
            -t $(TAG):$(RELEASE) \
            .
    
    .PHONY=docker-push
    docker-push: docker-build
        @docker push -t $(TAG)
    

Another tool of trade is the Gitlab CI/CD, I'm very happy with, below you will see an example of a .gitlab-ci.yml project file.

These two snippets are useful, but it is up to viewers discretion to understand what is happening and also why didn't we have a make test target? I think it is up to you how to best test the application, and nothing another can impose on you or your project.

    stages:
    - test
    - build
    - docker
    - deploy
    .test: &test:
    stage: test
    image: ubuntu:16.04
    script:
    - make test
    test:app:
    <<: *test