Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Github Git标记名称作为Go via Travis CI中的版本_Github_Go_Makefile_Travis Ci - Fatal编程技术网

Github Git标记名称作为Go via Travis CI中的版本

Github Git标记名称作为Go via Travis CI中的版本,github,go,makefile,travis-ci,Github,Go,Makefile,Travis Ci,本质上,我想做的是将git标记名(来自github发行版)嵌入到我的GO代码中的版本字符串中 如果我使用这个代码 package main import ( "flag" "fmt" ) var version string func main() { var verFlag bool flag.BoolVar(&verFlag, "version", false,

本质上,我想做的是将git标记名(来自github发行版)嵌入到我的GO代码中的版本字符串中

如果我使用这个代码

package main
    
import (
    "flag"
    "fmt"
)
    
var version string
    
func main() {

    var verFlag bool
    flag.BoolVar(&verFlag, "version", false, "Returns the version number")

    var confPath string
    flag.StringVar(&confPath, "conf", "conf.yml", "Location on config file")

    flag.Parse()
    
    // if the user wants to see the version
    if verFlag {
        fmt.Printf("%s", version)
        return
    }
}
在内置TRAVIS CI期间,将
-ldflags'-X main.VERSION'
中的“VERSION”设置为
$TRAVIS_TAG
的最佳方法是什么

此外,Travis CI设置可在生成中使用的环境变量,例如标记生成或运行生成后部署

TRAVIS_标签:如果当前构建为标签,则包括标签的名称

我最后的努力是使用这个make文件:

GOFLAGS ?= $(GOFLAGS:)

TAG := $(VERSION)
ifeq ($(TAG),)
  BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
  DT := $(shell date '+%F::%T')
  VSN := $(BRANCH)-$(DT)
else
  VSN := $(TAG)
endif

ENV := $(shell printenv)

GOFLAGS = -ldflags '-X main.version $(VSN)'

default:
    all

all: 
    test
    install

install:
    get-deps
    @go build $(GOFLAGS) *.go

test:
    @go test $(GOFLAGS) ./...

get-deps:
    @go get ./...

clean:
    @go clean $(GOFLAGS) -i ./
使用此travis配置文件

language: go
script: make VERSION=$TRAVIS_TAG
go:
- 1.4.1
deploy:
  provider: releases
  api_key:
    secure: reallylongsecurestring
  file: releasebin
  skip_cleanup: true
  on:
    tags: true

然而,无论我尝试了什么样的变体,并且我已经尝试了这个示例的许多不同风格,似乎最终出现在我的github版本中的二进制文件不包含标记名。观察到的是生成文件中未设置版本字符串时产生的分支和日期版本字符串。我已经在我的开发机器上尝试了这个make文件,并在Travis中将环境变量设置为预期值,它可以按预期工作。我也很清楚,我不希望这种情况发生在提交构建上,而只是在创建标记时从github ie发布的版本上发生。

所以我自己解决了这个问题

我在上面的例子中遇到的问题是Travis文件。我似乎需要在“script”参数周围加引号。为了完整起见,这里是我的新Travis文件

language: go
script: 
 - "make VERSION=$TRAVIS_TAG"
go:
 - 1.4.1
deploy:
  provider: releases
  api_key:
    secure: reallylongsecurestring
  file: releasebin
  skip_cleanup: true
  on:
    tags: true