diff --git a/.drone.yml b/.drone.yml index fbcf626..d7948b3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/.gitignore b/.gitignore index 2116583..3791815 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,6 @@ _testmain.go # Output dockron -dockron-linux-* +dockron-* # deps vendor/ diff --git a/Makefile b/Makefile index 5c9bd9e..aeba60d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/main.go b/main.go index 68d96bc..d5fb033 100644 --- a/main.go +++ b/main.go @@ -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()