dongxia2068 2015-06-09 20:13
浏览 82
已采纳

通过Travis-CI在Go中将Git标签名称作为版本

Essentially what I want to do is embed the git tag name (from a github release) to a version string within my GO code.

If I use this code;

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
    }
}

what's the best way of setting "VERSION" in -ldflags '-X main.version VERSION' to $TRAVIS_TAG during a build in Travis-CI,

Additionally, Travis CI sets environment variables you can use in your build, e.g. to tag the build, or to run post-build deployments.

TRAVIS_TAG: If the current build for a tag, this includes the tag’s name

My last effort was to use this make file:

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 ./

with this travis config file;

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

However no matter what variation I try, and I have tried many different flavours of essentially this example, it seems that the binary that ends up in my github release does not contain the tag name. What is observed is the branch and date version string that results from when no version string is set in the make file. I have tried this make file on my dev machine and set environment variable to the expected in Travis and it works as expected. I am also clear that I dont expect this to happen on commit builds but only on releases from github ie when a tag is created.

  • 写回答

1条回答 默认 最新

  • dpmp9359 2015-06-17 12:37
    关注

    So I fixed this my self.

    My problem in the above example was in the Travis file. I seems I needed quotes around my "script" parameter. For completeness here is my new Travis file;

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器