Skip to content

Commit

Permalink
improve docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
LeKovr committed Jul 7, 2023
1 parent 2476654 commit 6f3274c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
# (ARMv7 requires too much resourses) platforms: linux/amd64, linux/arm/v7, linux/arm64
# we do not use github hardware for multiarch builds
# we use "make docker-multi" instead
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
Expand Down
24 changes: 15 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,33 @@ linters-settings:
suggest-new: true
misspell:
locale: US
goheader:
values:
const:
OWNER_COMPANY: KHS Films
MAINTAINER_COMPANY: Xelaj Software
LICENSE_URL: https://github.com/xelaj/mtproto/blob/master/LICENSE
template-path: docs/.license_header.txt

gofumpt:
lang-version: "1.20"
extra-rules: true
forbidigo:
forbid:
- context\.WithCancel$
- ^print.*$
- panic
errorlint:
errorf-multi: true
linters:
disable-all: true
enable:
- bodyclose
- depguard
# - depguard
- dogsled
- dupl
- errcheck
# - errorlint
# - forbidigo
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- gofumpt
- goimports
- gomnd
- goprintffuncname
Expand All @@ -80,6 +85,7 @@ linters:
- lll
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
Expand Down
84 changes: 57 additions & 27 deletions .woodpecker.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
kind: pipeline
name: ci

steps:
- name: fetch tags
image: alpine/git
commands:
- git fetch --tags
- git describe --tags --always > .version

- name: test app
image: ghcr.io/dopos/golang-alpine:v1.18.6-alpine3.16.2
commands:
- go test -tags test -covermode=atomic -coverprofile=coverage.txt ./...
- go vet ./...
- apk add --no-cache curl bash git
- curl -sS https://codecov.io/bash | bash
- curl -sSL https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter format-coverage coverage.txt -t gocov -p $${DRONE_REPO_LINK#https://}
- ./cc-test-reporter upload-coverage
environment:
CGO_ENABLED: 0
CODECOV_TOKEN:
from_secret: my-codecov-token
CC_TEST_REPORTER_ID:
from_secret: my-codeclimate-token

# lint this file
# go run github.com/woodpecker-ci/woodpecker/cmd/cli lint

variables:
# - &build_plugin 'woodpeckerci/plugin-docker-buildx'
- &build_plugin 'plugins/docker'
- &golint_img 'golangci/golangci-lint:v1.53-alpine'
- &golang_img 'ghcr.io/dopos/golang-alpine:v1.19.7-alpine3.17.2'
- base_settings: &base_buildx_settings
registry: it.elfire.ru
repo: it.elfire.ru/${CI_REPO_OWNER}/${CI_REPO_NAME}

clone:
git:
image: woodpeckerci/plugin-git
settings:
lfs: false
tags: true

pipeline:

test:
image: *golang_img
commands: make test

lint:
image: *golint_img
commands: golangci-lint run

build:
image: *golang_img
commands:
- make build-standalone

publish-dryrun:
image: *build_plugin
volumes:
- /var/run/docker.sock:/var/run/docker.sock
settings:
<<: *base_buildx_settings
dry_run: true

publish:
image: *build_plugin
volumes:
- /var/run/docker.sock:/var/run/docker.sock
settings:
<<: *base_buildx_settings
auto_tag: true
custom_labels: org.opencontainers.image.version=${CI_COMMIT_TAG##v}
username: ${CI_REPO_OWNER}
password:
from_secret: cb_token
when:
event: tag
ref: refs/tags/v*
41 changes: 20 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
ARG GOLANG_VERSION
ARG GOLANG_IMAGE=ghcr.io/dopos/golang-alpine
ARG GOLANG_VERSION=v1.19.7-alpine3.17.2
ARG APP=webtail

# FROM golang:$GOLANG_VERSION as builder
#FROM ghcr.io/dopos/golang-alpine:v1.16.10-alpine3.14.2 as builder
FROM golang:1.19.7-alpine3.17 as builder
ARG TARGETARCH
FROM --platform=$BUILDPLATFORM ${GOLANG_IMAGE}:${GOLANG_VERSION} as build

# for docker.io/golang:1.18-alpine
RUN apk --update add git
ARG APP

WORKDIR /opt/app
COPY . /src/$APP
WORKDIR /src/$APP

# Cached layer
COPY ./go.mod ./go.sum ./

RUN echo "Build for arch $TARGETARCH"

# Sources dependent layer
COPY ./ ./
RUN CGO_ENABLED=0 go test -timeout 10m -tags test -covermode=atomic -coverprofile=coverage.out ./...
RUN CGO_ENABLED=0 go build -ldflags "-X main.version=`git describe --tags --always`" -a ./cmd/webtail
ARG GOPROXY TARGETOS TARGETARCH
RUN --mount=type=cache,id=gobuild,target=/root/.cache/go-build \
--mount=type=cache,id=gomod,target=/go/pkg \
make build-standalone

FROM scratch

MAINTAINER Alexey Kovrizhkin <[email protected]>
LABEL org.opencontainers.image.description "Tail [log]files via web[sockets]"
LABEL org.opencontainers.image.title="webtail" \
org.opencontainers.image.description="Tail [log]files via web[sockets]" \
org.opencontainers.image.authors="[email protected]" \
org.opencontainers.image.licenses="MIT"

VOLUME /data
WORKDIR /
COPY --from=builder /opt/app/webtail .

ARG APP

COPY --from=build /src/$APP/$APP /app
EXPOSE 8080
ENTRYPOINT ["/webtail"]
ENTRYPOINT [ "/app" ]
48 changes: 36 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

SHELL = /bin/sh
PRG ?= $(shell basename $$PWD)

PRG_DEST ?= $(PRG)
# -----------------------------------------------------------------------------
# Build config

GO ?= go
# not supported in BusyBox v1.26.2
SOURCES = $(shell find . -maxdepth 3 -mindepth 1 -name '*.go' -printf '%p\n')
SOURCES = $(shell find . -maxdepth 3 -mindepth 1 -path ./var -prune -o -name '*.go')
APP_VERSION ?= $(shell git describe --tags --always)
GOLANG_VERSION = 1.15.5-alpine3.12
GOLANG_VERSION = v1.19.7-alpine3.17.2

TARGETOS ?= linux
TARGETARCH ?= amd64
LDFLAGS := -s -w -extldflags '-static'

OS ?= linux
ARCH ?= amd64
Expand Down Expand Up @@ -59,15 +62,19 @@ all: help

## Run lint
lint:
@which golint > /dev/null || go install golang.org/x/lint/golint@latest
@golint ./...

## Run golangci-lint
ci-lint:
@golangci-lint run ./...

## Run vet
vet:
$(GO) vet ./...
@$(GO) vet ./...

## Run tests
test: coverage.out
test: lint vet coverage.out

coverage.out: $(SOURCES)
$(GO) test -tags test -race -covermode=atomic -coverprofile=$@ ./...
Expand All @@ -93,9 +100,10 @@ $(PRG): $(SOURCES)
"-X main.version=$(APP_VERSION)" ./cmd/$@

## Build like docker image from scratch
build-standalone: lint vet test
GOOS=linux CGO_ENABLED=0 $(GO) build -a -v -o $(PRG) -ldflags \
"-X main.version=$(APP_VERSION)" ./cmd/$(PRG)
build-standalone: test
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
$(GO) build -a -o $(PRG_DEST) -ldflags "${LDFLAGS} -X main.version=$(APP_VERSION)" \
./cmd/$(PRG)

## build and run in foreground
run: build
Expand All @@ -118,8 +126,7 @@ buildall: lint vet
for a in "$(ALLARCH)" ; do \
echo "** $${a%/*} $${a#*/}" ; \
P=$(PRG)-$${a%/*}_$${a#*/} ; \
GOOS=$${a%/*} GOARCH=$${a#*/} $(GO) build -o $$P -ldflags \
"-X main.version=$(APP_VERSION)" ./cmd/$(PRG) ; \
$(MAKE) -s build-standalone TARGETOS=$${a%/*} TARGETARCH=$${a#*/} PRG_DEST=$$P ; \
done

## create disro files
Expand All @@ -131,7 +138,7 @@ dist: clean buildall
echo "** $${a%/*} $${a#*/}" ; \
P=$(PRG)-$${a%/*}_$${a#*/} ; \
zip "$(DIRDIST)/$$P.zip" "$$P" README.md README.ru.md screenshot.png; \
rm "$$P" ; \
rm "$$P" ; \
done


Expand Down Expand Up @@ -200,6 +207,23 @@ ghcr:
docker tag $(DOCKER_IMAGE):$$v $(DOCKER_IMAGE):latest && \
docker push $(DOCKER_IMAGE):latest

# linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386,
# linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

ALLARCH_DOCKER ?= "linux/arm/v7,linux/arm64"

OWN_HUB ?= it.elfire.ru

buildkit.toml:
@echo [registry."$(OWN_HUB)"] > $@
@echo ca=["/etc/docker/certs.d/$(OWN_HUB)/ca.crt"] >> $@

use-own-hub: buildkit.toml
@docker buildx create --use --config $<

docker-multi:
time docker buildx build --platform $(ALLARCH_DOCKER) -t $(DOCKER_IMAGE):$(APP_VERSION) --push .

# This code handles group header and target comment with one or two lines only
## list Makefile targets
## (this is default target)
Expand Down

0 comments on commit 6f3274c

Please sign in to comment.