Application auto build versioning

后端 未结 6 830
情话喂你
情话喂你 2020-11-28 00:25

Is it possible to increment a minor version number automatically each time a Go app is compiled?

I would like to set a version number inside my program, with an auto

6条回答
  •  半阙折子戏
    2020-11-28 00:32

    Additionally I would like to post a small example how to use git and a makefile:

    --- Makefile ----
    
    # This how we want to name the binary output
    BINARY=gomake
    
    # These are the values we want to pass for VERSION and BUILD
    # git tag 1.0.1
    # git commit -am "One more change after the tags"
    VERSION=`git describe --tags`
    BUILD=`date +%FT%T%z`
    
    # Setup the -ldflags option for go build here, interpolate the variable values
    LDFLAGS_f1=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f1"
    LDFLAGS_f2=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f2"
    
    # Builds the project
    build:
        go build ${LDFLAGS_f1} -o ${BINARY}_f1
        go build ${LDFLAGS_f2} -o ${BINARY}_f2
    
    # Installs our project: copies binaries
    install:
        go install ${LDFLAGS_f1}
    
    # Cleans our project: deletes binaries
    clean:
        if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
    
    .PHONY: clean install
    

    The make file will create two executables. One is executing function one, the other will take function two as main entry:

    package main
    
    import (
            "fmt"
    )
    
    var (
    
            Version string
            Build   string
            Entry   string
    
            funcs = map[string]func() {
                    "f1":functionOne,"f2":functionTwo,
            }
    
    )
    
    func functionOne() {
        fmt.Println("This is function one")
    }
    
    func functionTwo() {
        fmt.Println("This is function two")
    }
    
    func main() {
    
            fmt.Println("Version: ", Version)
            fmt.Println("Build Time: ", Build)
    
        funcs[Entry]()
    
    }
    

    Then just run:

    make
    

    You will get:

    mab@h2470988:~/projects/go/gomake/3/gomake$ ls -al
    total 2020
    drwxrwxr-x 3 mab mab    4096 Sep  7 22:41 .
    drwxrwxr-x 3 mab mab    4096 Aug 16 10:00 ..
    drwxrwxr-x 8 mab mab    4096 Aug 17 16:40 .git
    -rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f1
    -rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f2
    -rw-rw-r-- 1 mab mab     399 Aug 16 10:21 main.go
    -rw-rw-r-- 1 mab mab     810 Sep  7 22:41 Makefile
    mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f1
    Version:  1.0.1-1-gfb51187
    Build Time:  2016-09-07T22:41:38+0200
    This is function one
    mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f2
    Version:  1.0.1-1-gfb51187
    Build Time:  2016-09-07T22:41:39+0200
    This is function two
    

提交回复
热议问题