Skip to content

Commit

Permalink
feat: use new version pkg (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp authored Oct 23, 2020
1 parent 40a4c1e commit e5e3fa8
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 15 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ jobs:
steps:
- name: clone
uses: actions/checkout@v2
with:
# ensures we fetch tag history for the repository
fetch-depth: 0

- name: setup
run: |
# setup git tag in Actions environment
echo "GITHUB_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: build
env:
GOOS: linux
CGO_ENABLED: '0'
run: |
go build -a \
-ldflags '-s -w -extldflags "-static"' \
-o release/vela-kubernetes \
github.com/go-vela/vela-kubernetes/cmd/vela-kubernetes
make build-static-ci
- name: publish
uses: elgohr/Publish-Docker-Github-Action@master
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ jobs:
steps:
- name: clone
uses: actions/checkout@v2
with:
# ensures we fetch tag history for the repository
fetch-depth: 0

- name: build
env:
GOOS: linux
CGO_ENABLED: '0'
run: |
go build -a \
-ldflags '-s -w -extldflags "-static"' \
-o release/vela-kubernetes \
github.com/go-vela/vela-kubernetes/cmd/vela-kubernetes
make build-static-ci
- name: publish
uses: elgohr/Publish-Docker-Github-Action@master
Expand Down
41 changes: 40 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
#
# Use of this source code is governed by the LICENSE file in this repository.

# capture the current date we build the application from
BUILD_DATE = $(shell date +%Y-%m-%dT%H:%M:%SZ)

# check if a git commit sha is already set
ifndef GITHUB_SHA
# capture the current git commit sha we build the application from
GITHUB_SHA = $(shell git rev-parse HEAD)
endif

# check if a git tag is already set
ifndef GITHUB_TAG
# capture the current git tag we build the application from
GITHUB_TAG = $(shell git describe --tag --abbrev=0)
endif

# check if a go version is already set
ifndef GOLANG_VERSION
# capture the current go version we build the application from
GOLANG_VERSION = $(shell go version | awk '{ print $$3 }')
endif

# create a list of linker flags for building the golang application
LD_FLAGS = -X github.com/go-vela/vela-kubernetes/version.Commit=${GITHUB_SHA} -X github.com/go-vela/vela-kubernetes/version.Date=${BUILD_DATE} -X github.com/go-vela/vela-kubernetes/version.Go=${GOLANG_VERSION} -X github.com/go-vela/vela-kubernetes/version.Tag=${GITHUB_TAG}

# The `clean` target is intended to clean the workspace
# and prepare the local changes for submission.
#
Expand Down Expand Up @@ -90,6 +114,7 @@ build:
@echo "### Building release/vela-kubernetes binary"
GOOS=linux CGO_ENABLED=0 \
go build -a \
-ldflags '${LD_FLAGS}' \
-o release/vela-kubernetes \
github.com/go-vela/vela-kubernetes/cmd/vela-kubernetes

Expand All @@ -103,7 +128,21 @@ build-static:
@echo "### Building static release/vela-kubernetes binary"
GOOS=linux CGO_ENABLED=0 \
go build -a \
-ldflags '-s -w -extldflags "-static"' \
-ldflags '-s -w -extldflags "-static" ${LD_FLAGS}' \
-o release/vela-kubernetes \
github.com/go-vela/vela-kubernetes/cmd/vela-kubernetes

# The `build-static-ci` target is intended to compile
# the Go source code into a statically linked binary
# when used within a CI environment.
#
# Usage: `make build-static-ci`
.PHONY: build-static-ci
build-static-ci:
@echo
@echo "### Building CI static release/vela-kubernetes binary"
@go build -a \
-ldflags '-s -w -extldflags "-static" ${LD_FLAGS}' \
-o release/vela-kubernetes \
github.com/go-vela/vela-kubernetes/cmd/vela-kubernetes

Expand Down
18 changes: 16 additions & 2 deletions cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package main

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/go-vela/vela-kubernetes/version"

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

Expand All @@ -32,8 +36,9 @@ func main() {

// Plugin Metadata

app.Compiled = time.Now()
app.Action = run
app.Compiled = time.Now()
app.Version = version.New().Semantic()

// Plugin Flags

Expand Down Expand Up @@ -172,6 +177,15 @@ func main() {

// run executes the plugin based off the configuration provided.
func run(c *cli.Context) error {
// capture the version information as pretty JSON
v, err := json.MarshalIndent(version.New(), "", " ")
if err != nil {
return err
}

// output the version information to stdout
fmt.Fprintf(os.Stdout, "%s\n", string(v))

// set the log level for the plugin
switch c.String("log.level") {
case "t", "trace", "Trace", "TRACE":
Expand Down Expand Up @@ -243,7 +257,7 @@ func run(c *cli.Context) error {
}

// validate the plugin
err := p.Validate()
err = p.Validate()
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ go 1.13
require (
cloud.google.com/go v0.60.0 // indirect
cloud.google.com/go/storage v1.10.0 // indirect
github.com/Masterminds/semver v1.5.0
github.com/aws/aws-sdk-go v1.33.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/go-vela/types v0.6.1-0.20201019123446-226d0cc72538
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-getter v1.4.1
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/joho/godotenv v1.3.0
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/spf13/afero v1.3.1
github.com/spf13/afero v1.3.4
github.com/ulikunitz/xz v0.5.7 // indirect
github.com/urfave/cli/v2 v2.2.0
go.opencensus.io v0.22.4 // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/tools v0.0.0-20200702044944-0cc1aa72b347 // indirect
google.golang.org/genproto v0.0.0-20200702021140-07506425bd67 // indirect
google.golang.org/grpc v1.30.0 // indirect
Expand Down
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/aws/aws-sdk-go v1.15.78 h1:LaXy6lWR0YK7LKyuU0QWy2ws/LWTPfYV/UgfiBu4tvY=
github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM=
github.com/aws/aws-sdk-go v1.33.1 h1:yz9XmNzPshz/lhfAZvLfMnIS9HPo8+boGRcWqDVX+T0=
github.com/aws/aws-sdk-go v1.33.1/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 h1:q+sMKdA6L8LyGVudTkpGoC73h6ak2iWSPFiFo/pFOU8=
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3/go.mod h1:5hCug3EZaHXU3FdCA3gJm0YTNi+V+ooA2qNTiVpky4A=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cheggaaa/pb v1.0.27 h1:wIkZHkNfC7R6GI5w7l/PdAdzXzlrbcI3p8OAlnkTsnc=
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
Expand All @@ -62,10 +66,14 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-vela/types v0.6.1-0.20201019123446-226d0cc72538 h1:ck0Ylos/ExUCluEqQgVxKrTZ21nsup0VZT31sWqTU4Y=
github.com/go-vela/types v0.6.1-0.20201019123446-226d0cc72538/go.mod h1:6r6mWIPrTANBpHwAFAIii64VKtzlAzVagbm/wX5bHHk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -150,6 +158,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
Expand All @@ -175,8 +184,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/spf13/afero v1.3.1 h1:GPTpEAuNr98px18yNQ66JllNil98wfRZ/5Ukny8FeQA=
github.com/spf13/afero v1.3.1/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.3.4 h1:8q6vk3hthlpb2SouZcnBVKboxWQWMDNF38bwholZrJc=
github.com/spf13/afero v1.3.4/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down Expand Up @@ -445,6 +454,8 @@ gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qS
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
55 changes: 55 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package version

import (
"fmt"
"runtime"

"github.com/Masterminds/semver"

"github.com/go-vela/types/version"
)

var (
// Arch represents the architecture information for the package.
Arch = runtime.GOARCH
// Commit represents the git commit information for the package.
Commit string
// Compiler represents the compiler information for the package.
Compiler = runtime.Compiler
// Date represents the build date information for the package.
Date string
// Go represents the golang version information for the package.
Go string
// OS represents the operating system information for the package.
OS = runtime.GOOS
// Tag represents the git tag information for the package.
Tag string
)

// New creates a new version object for Vela that is used throughout the application.
func New() *version.Version {
v, err := semver.NewVersion(Tag)
if err != nil {
fmt.Println(fmt.Errorf("unable to parse semantic version for %s: %v", Tag, err))
}

return &version.Version{
Canonical: Tag,
Major: v.Major(),
Minor: v.Minor(),
Patch: v.Patch(),
PreRelease: v.Prerelease(),
Metadata: version.Metadata{
Architecture: Arch,
BuildDate: Date,
Compiler: Compiler,
GitCommit: Commit,
GoVersion: Go,
OperatingSystem: OS,
},
}
}

0 comments on commit e5e3fa8

Please sign in to comment.