Add versioning to dockron binary
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

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 image: golang:1.11
commands: commands:
- go get -u github.com/golang/dep/cmd/dep - go get -u github.com/golang/dep/cmd/dep
- make build-all-static - make build-linux-static
- name: push image - arm - name: push image - arm
image: plugins/docker image: plugins/docker

2
.gitignore vendored
View File

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

View File

@ -1,4 +1,7 @@
DOCKER_TAG ?= dockron-dev-${USER} 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 .PHONY: default
default: build default: build
@ -14,23 +17,38 @@ run: vendor
# Output target # Output target
dockron: vendor dockron: vendor
go build -o dockron @echo Version: $(VERSION)
go build -ldflags '-X "main.version=${VERSION}"' -o dockron
# Alias for building # Alias for building
.PHONY: build .PHONY: build
build: dockron 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 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 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 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 .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 # Cleans all build artifacts
.PHONY: clean .PHONY: clean

25
main.go
View File

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