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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失