Skip to content

Commit

Permalink
chore: add cross build binary support
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshadhin committed Dec 14, 2022
1 parent 4128fe1 commit 9406c21
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ config.yml
data
it/*.db
cmd/migrations

dist/
107 changes: 107 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
before:
hooks:
- go mod download
- go generate ./cmd

builds:
- id: ot-recorder
flags:
- -tags=osusergo,netgo,sqlite_omit_load_extension
ldflags:
- -extldflags=-static
- -s -w
- -X ot-recorder/app.Version={{.Version}}
- -X ot-recorder/app.BuildTime={{.Date}}
env:
- CGO_ENABLED=1
goos:
- linux
goarch:
- amd64
- arm64
- arm
overrides:
- goos: linux
goarch: arm
goarm: "6"
env:
- CC=arm-linux-gnueabi-gcc
- goos: linux
goarch: arm64
env:
- CC=aarch64-linux-gnu-gcc
- goos: linux
goarch: amd64
env:
- cc=gcc

checksum:
name_template: 'checksums.txt'

snapshot:
name_template: "{{ incpatch .Version }}-next"

archives:
- name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
files:
- LICENSE

changelog:
sort: asc
use: github
filters:
exclude:
- '^test:'
- '^chore'
- 'merge conflict'
- 'Merge pull request'
- 'Merge remote-tracking branch'
- 'Merge branch'
- 'go mod tidy'

dockers:
- image_templates:
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-amd64"
dockerfile: Dockerfile_gr
use: buildx
build_flag_templates:
- "--pull"
- "--build-arg=BUILD_VERSION={{.Version}}"
- "--build-arg=BUILD_TIME={{.Date}}"
- "--platform=linux/amd64"
- image_templates:
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-arm64"
dockerfile: Dockerfile_gr
use: buildx
build_flag_templates:
- "--pull"
- "--build-arg=BUILD_VERSION={{.Version}}"
- "--build-arg=BUILD_TIME={{.Date}}"
- "--platform=linux/arm64"
goarch: arm64
- image_templates:
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-arm"
dockerfile: Dockerfile_gr
use: buildx
build_flag_templates:
- "--pull"
- "--build-arg=BUILD_VERSION={{.Version}}"
- "--build-arg=BUILD_TIME={{.Date}}"
- "--platform=linux/arm/v6"
goarch: arm

docker_manifests:
- name_template: "hrshadhin/{{ .ProjectName }}:{{ .Version }}"
image_templates:
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-amd64"
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-arm64"
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-arm"
- name_template: "hrshadhin/{{ .ProjectName }}:latest"
image_templates:
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-amd64"
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-arm64"
- "hrshadhin/{{ .ProjectName }}:{{ .Version }}-arm"

release:
footer: |
**Full Changelog**: https://github.com/hrshadhin/ot-recorder/compare/{{ .PreviousTag }}...{{ .Tag }}
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ ADD . .

# Build the Go app
ARG BUILD_VERSION=0.0.1
ARG BUILD_TIME=00000000-000000
ARG BUILD_TIME=2022-12-13T000000Z
RUN go generate ./cmd && GOOS=linux GOARCH=amd64 go build -ldflags "-w -s -X ot-recorder/app.Version=$BUILD_VERSION -X ot-recorder/app.BuildTime=$BUILD_TIME" -o ot-recorder .

# Minimal image for running the application
FROM alpine as final
ARG BUILD_VERSION=0.0.1
ARG BUILD_TIME=2022-12-13T000000Z

LABEL org.opencontainers.image.source="https://github.com/hrshadhin/ot-recoder" \
LABEL org.opencontainers.image.created=$BUILD_TIME \
org.opencontainers.image.version=$BUILD_VERSION \
org.opencontainers.image.source="https://github.com/hrshadhin/ot-recoder" \
org.opencontainers.image.url="https://github.com/hrshadhin/ot-recoder" \
org.opencontainers.image.title="A note taking applications"
org.opencontainers.image.name="ot-recorder" \
org.opencontainers.image.title="OwnTracks Recorder" \
org.opencontainers.image.description="Store and access data published by OwnTracks apps"

# Install/Create dependent tools,location,directory
RUN apk add --no-cache curl tini && \
RUN apk add --no-cache curl tini tzdata && \
rm -rf /var/cache/apk/* && \
mkdir /persist && chown -R 1000:1000 /persist

ENV ZONEINFO=/usr/share/zoneinfo

# Import the compiled executable from the first stage.
COPY --from=builder /build/ot-recorder /app/ot-recorder

Expand Down
34 changes: 34 additions & 0 deletions Dockerfile_gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM alpine

ARG BUILD_VERSION=0.0.1
ARG BUILD_TIME=2022-12-13T000000Z

LABEL org.opencontainers.image.created=$BUILD_TIME \
org.opencontainers.image.version=$BUILD_VERSION \
org.opencontainers.image.source="https://github.com/hrshadhin/ot-recoder" \
org.opencontainers.image.url="https://github.com/hrshadhin/ot-recoder" \
org.opencontainers.image.name="ot-recorder" \
org.opencontainers.image.title="OwnTracks Recorder" \
org.opencontainers.image.description="Store and access data published by OwnTracks apps"

# Install/Create dependent tools,location,directory
RUN apk add --no-cache curl tini tzdata && \
rm -rf /var/cache/apk/* && \
mkdir /persist && chown -R 1000:1000 /persist

ENV ZONEINFO=/usr/share/zoneinfo

## Open port
EXPOSE 8000

## Perform any further action as an unprivileged location.
USER 1000
WORKDIR /app
COPY --chmod=0755 ot-recorder ot-recorder


HEALTHCHECK --interval=1m --timeout=1s --retries=3 --start-period=2s CMD ["curl", "--fail", "http://localhost:8000/health"]

## Run the compiled binary.
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/app/ot-recorder","--config","/app/config.yml", "serve"]
File renamed without changes.
16 changes: 9 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env bash

BUILD_VERSION := $(shell git describe --always --tags)
BUILD_TIME=$(shell date '+%Y%m%d-%H%M%S')
DOCKER_IMAGE_NAME="hrshadhin/ot-recoder"
BINARY_NAME=ot-recoder
BUILD_TIME=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
DOCKER_IMAGE_NAME="hrshadhin/ot-recorder"
BINARY_NAME=ot-recorder
BIN_OUT_DIR=bin
GO_VERSION=1.19

export PATH=$(shell go env GOPATH)/bin:$(shell echo $$PATH)

Expand Down Expand Up @@ -45,7 +46,8 @@ dl-deps: ## Get dependencies

build: clean ## Build binary
go generate ./cmd
go build -v -ldflags="-w -s -X ot-recorder/app.Version=${BUILD_VERSION} -X ot-recorder/app.BuildTime=${BUILD_TIME}" -o $(BIN_OUT_DIR)/$(BINARY_NAME)
go build -v -tags osusergo,netgo,sqlite_omit_load_extension \
-ldflags="-extldflags=-static -w -s -X ot-recorder/app.Version=${BUILD_VERSION} -X ot-recorder/app.BuildTime=${BUILD_TIME}" -o $(BIN_OUT_DIR)/$(BINARY_NAME)

version: ## Check binary version
./$(BIN_OUT_DIR)/$(BINARY_NAME) --version
Expand All @@ -65,7 +67,7 @@ migrate-down: ## Revert migration
./$(BIN_OUT_DIR)/$(BINARY_NAME) migrate down

docker-build: ## Build docker image
docker build --build-arg BUILD_VERSION=${BUILD_VERSION} --build-arg BUILD_TIME=${BUILD_TIME} --tag ${DOCKER_IMAGE_NAME} .
docker build --build-arg GO_VERSION=${GO_VERSION} --build-arg BUILD_VERSION=${BUILD_VERSION} --build-arg BUILD_TIME=${BUILD_TIME} --tag ${DOCKER_IMAGE_NAME} .

docker-push: ## Push docker image
docker push
Expand All @@ -78,5 +80,5 @@ docker-run: ## Run docker image with sqlite
-v $$(pwd)/data:/persist \
$(DOCKER_IMAGE_NAME):latest

docker-migrate: ## Run migrations inside dorker
docker exec ot-recoder /app/ot-recoder migrate up
docker-migrate: ## Run migrations inside docker
docker exec ot-recoder /app/ot-recorder migrate up
2 changes: 1 addition & 1 deletion _deploy/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ sudo chown -R 1000:1000 data
docker-compose up -d

# run migrations
docker-compose exec owntracks-recoder /app/ot-recoder migrate up
docker-compose exec owntracks-recorder /app/ot-recorder migrate up
```
4 changes: 2 additions & 2 deletions _deploy/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: "3.5"

services:
owntracks-recoder:
image: hrshadhin/ot-recoder
container_name: owntracks-recoder
image: hrshadhin/ot-recorder
container_name: owntracks-recorder
restart: unless-stopped
volumes:
- ${PWD}/config.yml:/app/config.yml
Expand Down
4 changes: 2 additions & 2 deletions app/server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func printBanner() {
log.Println("| | | \\ \\ /\\ / / '_ \\| || '__/ _` |/ __| |/ / __| | |_) / _ \\/ __|")
log.Println("| |_| |\\ V V /| | | | || | | (_| | (__| <\\__ \\ | _ < __/ (__ _")
log.Println(" \\___/ \\_/\\_/ |_| |_|_||_| \\__,_|\\___|_|\\_\\___/ |_| \\_\\___|\\___(_)")
log.Println(" ")
log.Printf(" Version: %-18s Build time: %-18s ", app.Version, app.BuildTime)
log.Println(" ")
log.Printf(" Version: %-10s Build time: %-10s", app.Version, app.BuildTime)
log.Println("<<=")
}

0 comments on commit 9406c21

Please sign in to comment.