Add versioning to dockron binary
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
IamTheFij 2019-06-28 10:24:41 -07:00
parent 6e74d3b93f
commit aa35a8271c
4 changed files with 44 additions and 13 deletions

View File

@ -34,7 +34,7 @@ steps:
image: golang:1.11
commands:
- go get -u github.com/golang/dep/cmd/dep
- make build-all-static
- make build-linux-static
- name: push image - arm
image: plugins/docker

2
.gitignore vendored
View File

@ -26,6 +26,6 @@ _testmain.go
# Output
dockron
dockron-linux-*
dockron-*
# deps
vendor/

View File

@ -1,4 +1,7 @@
DOCKER_TAG ?= dockron-dev-${USER}
GIT_TAG_NAME := $(shell git tag -l --contains HEAD)
GIT_SHA := $(shell git rev-parse HEAD)
VERSION := $(if $(GIT_TAG_NAME),$(GIT_TAG_NAME),$(GIT_SHA))
.PHONY: default
default: build
@ -14,23 +17,38 @@ run: vendor
# Output target
dockron: vendor
go build -o dockron
@echo Version: $(VERSION)
go build -ldflags '-X "main.version=${VERSION}"' -o dockron
# Alias for building
.PHONY: build
build: dockron
dockron-darwin-amd64: vendor
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 \
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
-o dockron-darwin-amd64
dockron-linux-amd64: vendor
GOARCH=amd64 CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o dockron-linux-amd64
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
-o dockron-linux-amd64
dockron-linux-arm: vendor
GOARCH=arm CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o dockron-linux-arm
GOOS=linux GOARCH=arm CGO_ENABLED=0 \
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
-o dockron-linux-arm
dockron-linux-arm64: vendor
GOARCH=arm64 CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o dockron-linux-arm64
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 \
go build -ldflags '-X "main.version=${VERSION}"' -a -installsuffix nocgo \
-o dockron-linux-arm64
.PHONY: build-linux-static
build-linux-static: dockron-linux-amd64 dockron-linux-arm dockron-linux-arm64
.PHONY: build-all-static
build-all-static: dockron-linux-amd64 dockron-linux-arm dockron-linux-arm64
build-all-static: dockron-darwin-amd64 build-linux-static
# Cleans all build artifacts
.PHONY: clean

25
main.go
View File

@ -7,15 +7,21 @@ import (
"github.com/docker/docker/client"
"github.com/robfig/cron"
"golang.org/x/net/context"
"os"
"strings"
"time"
)
// WatchInterval is the duration we should sleep until polling Docker
var DefaultWatchInterval = (1 * time.Minute)
var (
// defaultWatchInterval is the duration we should sleep until polling Docker
defaultWatchInterval = (1 * time.Minute)
// SchedLabel is the string label to search for cron expressions
var SchedLabel = "dockron.schedule"
// schedLabel is the string label to search for cron expressions
schedLabel = "dockron.schedule"
// version of dockron being run
version = "dev"
)
// ContainerStartJob represents a scheduled container task
// It contains a reference to a client, the schedule to run on, and the
@ -48,7 +54,7 @@ func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
}
for _, container := range containers {
if val, ok := container.Labels[SchedLabel]; ok {
if val, ok := container.Labels[schedLabel]; ok {
jobName := strings.Join(container.Names, "/")
jobs = append(jobs, ContainerStartJob{
Schedule: val,
@ -81,9 +87,16 @@ func main() {
// Read interval for polling Docker
var watchInterval time.Duration
flag.DurationVar(&watchInterval, "watch", DefaultWatchInterval, "Interval used to poll Docker for changes")
flag.DurationVar(&watchInterval, "watch", defaultWatchInterval, "Interval used to poll Docker for changes")
var showVersion = flag.Bool("version", false, "Display the version of dockron and exit")
flag.Parse()
// Print version if asked
if *showVersion {
fmt.Println("Dockron version:", version)
os.Exit(0)
}
// Create a Cron
c := cron.New()