Skip to content

Commit

Permalink
Build multiarch images for server (#821)
Browse files Browse the repository at this point in the history
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Anbraten <anton@ju60.de>
  • Loading branch information
3 people authored Mar 1, 2022
1 parent 09e6460 commit da99f47
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 44 deletions.
53 changes: 30 additions & 23 deletions .woodpecker/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,71 @@ depends_on:
- web

pipeline:
###############
# S e r v e r #
###############
build-web:
image: node:16-alpine
commands:
- cd web/
- yarn install --frozen-lockfile
- yarn build

###############
# S e r v e r #
###############

# TODO: needed until https://github.com/woodpecker-ci/woodpecker/pull/635
build-server:
image: golang:1.17
cross-compile-server:
image: techknowlogick/xgo:go-1.17.x
commands:
- make release-server
- apt update
- apt install -y tree
- make cross-compile-server
environment:
PLATFORMS: linux|arm/v7;linux|arm64/v8;linux|amd64;linux|ppc64le
TAGS: bindata sqlite sqlite_unlock_notify

publish-server-dryrun:
image: plugins/docker
image: woodpeckerci/plugin-docker-buildx
group: docker
secrets: [docker_username, docker_password]
settings:
dry_run: true
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server
tag: next
dockerfile: docker/Dockerfile.server.multiarch
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
tag: test
when:
event: pull_request

publish-server-alpine-dryrun:
image: plugins/docker
image: woodpeckerci/plugin-docker-buildx
group: docker
secrets: [ docker_username, docker_password ]
settings:
dry_run: true
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server.alpine
dockerfile: docker/Dockerfile.server.alpine.multiarch
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
tag: next-alpine
when:
event: pull_request

publish-next-server:
image: plugins/docker
image: woodpeckerci/plugin-docker-buildx
group: docker
secrets: [docker_username, docker_password]
settings:
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server
dockerfile: docker/Dockerfile.server.multiarch
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
tag: next
when:
branch: ${CI_REPO_DEFAULT_BRANCH}
event: push

publish-next-server-alpine:
image: plugins/docker
image: woodpeckerci/plugin-docker-buildx
group: docker
secrets: [ docker_username, docker_password ]
settings:
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server.alpine
dockerfile: docker/Dockerfile.server.alpine.multiarch
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
tag: next-alpine
when:
branch: ${CI_REPO_DEFAULT_BRANCH}
Expand Down Expand Up @@ -94,23 +99,25 @@ pipeline:

release-server:
group: docker
image: plugins/docker
image: woodpeckerci/plugin-docker-buildx
secrets: [docker_username, docker_password]
settings:
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server
dockerfile: docker/Dockerfile.server.multiarch
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
# remove 'latest' on older version branches to avoid accidental downgrade
tag: [latest, "${CI_COMMIT_TAG}"]
when:
event: tag

release-server-alpine:
group: docker
image: plugins/docker
image: woodpeckerci/plugin-docker-buildx
secrets: [ docker_username, docker_password ]
settings:
repo: woodpeckerci/woodpecker-server
dockerfile: docker/Dockerfile.server.alpine
dockerfile: docker/Dockerfile.server.alpine.multiarch
platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le
# remove 'latest-alpine' on older version branches to avoid accidental downgrade
tag: [latest-alpine, "${CI_COMMIT_TAG}-alpine"]
when:
Expand Down
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ ifeq ($(BUILD_VERSION),next)
endif

LDFLAGS := -s -w -extldflags "-static" -X github.com/woodpecker-ci/woodpecker/version.Version=${BUILD_VERSION}
CGO_CFLAGS ?=

HAS_GO = $(shell hash go > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
ifeq ($(HAS_GO), GO)
XGO_VERSION ?= go-1.17.x
CGO_CFLAGS ?= $(shell $(GO) env CGO_CFLAGS)
endif

# If the first argument is "in_docker"...
ifeq (in_docker,$(firstword $(MAKECMDGOALS)))
Expand Down Expand Up @@ -117,6 +124,31 @@ build: build-agent build-server build-cli

release-frontend: build-frontend

check-xgo:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) install src.techknowlogick.com/xgo@latest; \
fi

cross-compile-server:
$(foreach platform,$(subst ;, ,$(PLATFORMS)),\
TARGETOS=$(firstword $(subst |, ,$(platform))) \
TARGETARCH_XGO=$(subst arm64/v8,arm64,$(subst arm/v7,arm-7,$(word 2,$(subst |, ,$(platform))))) \
TARGETARCH_BUILDX=$(subst arm64/v8,arm64,$(subst arm/v7,arm,$(word 2,$(subst |, ,$(platform))))) \
make release-server-xgo || exit 1; \
)
tree dist

release-server-xgo: check-xgo
@echo "Building for:"
@echo "os:$(TARGETOS)"
@echo "arch orgi:$(TARGETARCH)"
@echo "arch (xgo):$(TARGETARCH_XGO)"
@echo "arch (buildx):$(TARGETARCH_BUILDX)"

CGO_CFLAGS="$(CGO_CFLAGS)" xgo -go $(XGO_VERSION) -dest ./dist/server/$(TARGETOS)-$(TARGETARCH_XGO) -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external $(LDFLAGS)' -targets '$(TARGETOS)/$(TARGETARCH_XGO)' -out woodpecker-server -pkg cmd/server .
mkdir -p ./dist/server/$(TARGETOS)/$(TARGETARCH_BUILDX)
mv /build/woodpecker-server-$(TARGETOS)-$(TARGETARCH_XGO) ./dist/server/$(TARGETOS)/$(TARGETARCH_BUILDX)/woodpecker-server

release-server:
# compile
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -ldflags '${LDFLAGS}' -o dist/server/linux_amd64/woodpecker-server github.com/woodpecker-ci/woodpecker/cmd/server
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.agent.alpine.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN apk add -U --no-cache ca-certificates
ENV GODEBUG=netdns=go
EXPOSE 3000

COPY --from=build src/dist/woodpecker-agent /bin/
COPY --from=build /src/dist/woodpecker-agent /bin/

HEALTHCHECK CMD ["/bin/woodpecker-agent", "ping"]
ENTRYPOINT ["/bin/woodpecker-agent"]
2 changes: 1 addition & 1 deletion docker/Dockerfile.agent.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ EXPOSE 3000
# copy certs from golang:1.16 image
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
# copy agent binary
COPY --from=build src/dist/woodpecker-agent /bin/
COPY --from=build /src/dist/woodpecker-agent /bin/

HEALTHCHECK CMD ["/bin/woodpecker-agent", "ping"]
ENTRYPOINT ["/bin/woodpecker-agent"]
2 changes: 1 addition & 1 deletion docker/Dockerfile.cli.alpine.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ FROM alpine:3.14
RUN apk add -U --no-cache ca-certificates
ENV GODEBUG=netdns=go

COPY --from=build src/dist/woodpecker-cli /bin/
COPY --from=build /src/dist/woodpecker-cli /bin/

HEALTHCHECK CMD ["/bin/woodpecker-cli", "ping"]
ENTRYPOINT ["/bin/woodpecker-cli"]
2 changes: 1 addition & 1 deletion docker/Dockerfile.cli.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ENV GODEBUG=netdns=go
# copy certs from golang:1.16 image
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
# copy cli binary
COPY --from=build src/dist/woodpecker-cli /bin/
COPY --from=build /src/dist/woodpecker-cli /bin/

HEALTHCHECK CMD ["/bin/woodpecker-cli", "ping"]
ENTRYPOINT ["/bin/woodpecker-cli"]
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# docker build --rm -f docker/Dockerfile.server.alpine -t woodpeckerci/woodpecker-server .

FROM alpine:3.14
ARG TARGETOS TARGETARCH
RUN apk add -U --no-cache ca-certificates

EXPOSE 8000 9000 80 443

ENV GODEBUG=netdns=go
ENV WOODPECKER_DATABASE_DATASOURCE=/var/lib/woodpecker/woodpecker.sqlite
ENV WOODPECKER_DATABASE_DRIVER=sqlite3
ENV XDG_CACHE_HOME=/var/lib/woodpecker
EXPOSE 8000 9000 80 443

ADD dist/server/linux_amd64/woodpecker-server /bin/
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/

ENTRYPOINT ["/bin/woodpecker-server"]
18 changes: 7 additions & 11 deletions docker/Dockerfile.server → docker/Dockerfile.server.multiarch
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# docker build --rm -f docker/Dockerfile.server -t woodpeckerci/woodpecker-server .

# use golang image to copy ssl certs later
FROM golang:1.16
FROM golang:1.16 AS certs

FROM scratch

# copy certs from golang:1.16 image
COPY --from=0 /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

EXPOSE 8000 9000 80 443

ARG TARGETOS TARGETARCH
ENV GODEBUG=netdns=go
ENV WOODPECKER_DATABASE_DATASOURCE=/var/lib/woodpecker/woodpecker.sqlite
ENV WOODPECKER_DATABASE_DRIVER=sqlite3
ENV XDG_CACHE_HOME=/var/lib/woodpecker
EXPOSE 8000 9000 80 443

ADD dist/server/linux_amd64/woodpecker-server /bin/
# copy certs from golang:1.16 image
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
# copy server binary
COPY dist/server/${TARGETOS}/${TARGETARCH}/woodpecker-server /bin/

ENTRYPOINT ["/bin/woodpecker-server"]

0 comments on commit da99f47

Please sign in to comment.