From ff907463617210b8a30955293f26331e9e3d13af Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sat, 14 Jan 2023 22:42:00 +0100 Subject: [PATCH] refactor: use buildx for multiarch container builds (#156) --- .dockerignore | 2 - .drone.jsonnet | 350 ------------------ .drone.yml | 142 ++----- .../Dockerfile.amd64 => Dockerfile.multiarch | 13 +- Makefile | 9 +- docker/Dockerfile.arm64 | 12 - docker/manifest-quay.tmpl | 18 - docker/manifest.tmpl | 18 - 8 files changed, 42 insertions(+), 522 deletions(-) delete mode 100644 .dockerignore delete mode 100644 .drone.jsonnet rename docker/Dockerfile.amd64 => Dockerfile.multiarch (69%) delete mode 100644 docker/Dockerfile.arm64 delete mode 100644 docker/manifest-quay.tmpl delete mode 100644 docker/manifest.tmpl diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 92d6c40..0000000 --- a/.dockerignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!dist/ diff --git a/.drone.jsonnet b/.drone.jsonnet deleted file mode 100644 index 741aa91..0000000 --- a/.drone.jsonnet +++ /dev/null @@ -1,350 +0,0 @@ -local PipelineTest = { - kind: 'pipeline', - name: 'test', - platform: { - os: 'linux', - arch: 'amd64', - }, - steps: [ - { - name: 'deps', - image: 'golang:1.19', - commands: [ - 'make deps', - ], - volumes: [ - { - name: 'godeps', - path: '/go', - }, - ], - }, - { - name: 'lint', - image: 'golang:1.19', - commands: [ - 'make lint', - ], - volumes: [ - { - name: 'godeps', - path: '/go', - }, - ], - }, - { - name: 'test', - image: 'golang:1.19', - commands: [ - 'make test', - ], - volumes: [ - { - name: 'godeps', - path: '/go', - }, - ], - }, - ], - volumes: [ - { - name: 'godeps', - temp: {}, - }, - ], - trigger: { - ref: ['refs/heads/main', 'refs/tags/**', 'refs/pull/**'], - }, -}; - - -local PipelineBuildBinaries = { - kind: 'pipeline', - name: 'build-binaries', - platform: { - os: 'linux', - arch: 'amd64', - }, - steps: [ - { - name: 'build', - image: 'techknowlogick/xgo:go-1.19.x', - commands: [ - 'ln -s /drone/src /source', - 'make release', - ], - }, - { - name: 'executable', - image: 'alpine', - commands: [ - '$(find dist/ -executable -type f -iname ${DRONE_REPO_NAME}-linux-amd64) --help', - ], - }, - { - name: 'changelog-generate', - image: 'thegeeklab/git-chglog', - commands: [ - 'git fetch -tq', - 'git-chglog --no-color --no-emoji -o CHANGELOG.md ${DRONE_TAG:---next-tag unreleased unreleased}', - ], - }, - { - name: 'changelog-format', - image: 'thegeeklab/alpine-tools', - commands: [ - 'prettier CHANGELOG.md', - 'prettier -w CHANGELOG.md', - ], - }, - { - name: 'publish', - image: 'plugins/github-release', - settings: { - overwrite: true, - api_key: { - from_secret: 'github_token', - }, - files: ['dist/*'], - title: '${DRONE_TAG}', - note: 'CHANGELOG.md', - }, - when: { - ref: [ - 'refs/tags/**', - ], - }, - }, - ], - depends_on: [ - 'test', - ], - trigger: { - ref: ['refs/heads/main', 'refs/tags/**', 'refs/pull/**'], - }, -}; - -local PipelineBuildContainer(arch='amd64') = { - kind: 'pipeline', - name: 'build-container-' + arch, - platform: { - os: 'linux', - arch: arch, - }, - steps: [ - { - name: 'build', - image: 'golang:1.19', - commands: [ - 'make build', - 'ls -l dist/drone-github-comment', - ], - }, - { - name: 'dryrun', - image: 'thegeeklab/drone-docker-buildx:20', - settings: { - dry_run: true, - dockerfile: 'docker/Dockerfile.' + arch, - repo: 'thegeeklab/${DRONE_REPO_NAME}', - }, - depends_on: ['build'], - when: { - ref: ['refs/pull/**'], - }, - }, - { - name: 'publish-dockerhub', - image: 'thegeeklab/drone-docker-buildx:20', - settings: { - auto_tag: true, - auto_tag_suffix: arch, - dockerfile: 'docker/Dockerfile.' + arch, - repo: 'thegeeklab/${DRONE_REPO_NAME}', - username: { from_secret: 'docker_username' }, - password: { from_secret: 'docker_password' }, - }, - when: { - ref: ['refs/heads/main', 'refs/tags/**'], - }, - depends_on: ['dryrun'], - }, - { - name: 'publish-quay', - image: 'thegeeklab/drone-docker-buildx:20', - settings: { - auto_tag: true, - auto_tag_suffix: arch, - dockerfile: 'docker/Dockerfile.' + arch, - registry: 'quay.io', - repo: 'quay.io/thegeeklab/${DRONE_REPO_NAME}', - username: { from_secret: 'quay_username' }, - password: { from_secret: 'quay_password' }, - }, - when: { - ref: ['refs/heads/main', 'refs/tags/**'], - }, - depends_on: ['dryrun'], - }, - ], - depends_on: [ - 'test', - ], - trigger: { - ref: ['refs/heads/main', 'refs/tags/**', 'refs/pull/**'], - }, -}; - -local PipelineDocs = { - kind: 'pipeline', - name: 'docs', - platform: { - os: 'linux', - arch: 'amd64', - }, - concurrency: { - limit: 1, - }, - steps: [ - { - name: 'markdownlint', - image: 'thegeeklab/markdownlint-cli', - commands: [ - "markdownlint 'docs/content/**/*.md' 'README.md' 'CONTRIBUTING.md'", - ], - }, - { - name: 'spellcheck', - image: 'thegeeklab/alpine-tools', - commands: [ - "spellchecker --files '_docs/**/*.md' 'README.md' 'CONTRIBUTING.md' -d .dictionary -p spell indefinite-article syntax-urls --no-suggestions", - ], - environment: { - FORCE_COLOR: true, - NPM_CONFIG_LOGLEVEL: 'error', - }, - }, - { - name: 'publish', - image: 'plugins/gh-pages', - settings: { - username: { from_secret: 'github_username' }, - password: { from_secret: 'github_token' }, - pages_directory: '_docs/', - target_branch: 'docs', - }, - when: { - ref: ['refs/heads/main'], - }, - }, - ], - depends_on: [ - 'build-binaries', - 'build-container-amd64', - 'build-container-arm64', - ], - trigger: { - ref: ['refs/heads/main', 'refs/tags/**', 'refs/pull/**'], - }, -}; - -local PipelineNotifications = { - kind: 'pipeline', - name: 'notifications', - platform: { - os: 'linux', - arch: 'amd64', - }, - steps: [ - { - image: 'plugins/manifest', - name: 'manifest-dockerhub', - settings: { - ignore_missing: true, - auto_tag: true, - username: { from_secret: 'docker_username' }, - password: { from_secret: 'docker_password' }, - spec: 'docker/manifest.tmpl', - }, - when: { - status: ['success'], - }, - }, - { - image: 'plugins/manifest', - name: 'manifest-quay', - settings: { - ignore_missing: true, - auto_tag: true, - username: { from_secret: 'quay_username' }, - password: { from_secret: 'quay_password' }, - spec: 'docker/manifest-quay.tmpl', - }, - when: { - status: ['success'], - }, - }, - { - name: 'pushrm-dockerhub', - image: 'chko/docker-pushrm:1', - environment: { - DOCKER_PASS: { - from_secret: 'docker_password', - }, - DOCKER_USER: { - from_secret: 'docker_username', - }, - PUSHRM_FILE: 'README.md', - PUSHRM_SHORT: 'Drone plugin to add comments to GitHub Issues and Pull Requests', - PUSHRM_TARGET: 'thegeeklab/${DRONE_REPO_NAME}', - }, - when: { - status: ['success'], - }, - }, - { - name: 'pushrm-quay', - image: 'chko/docker-pushrm:1', - environment: { - APIKEY__QUAY_IO: { - from_secret: 'quay_token', - }, - PUSHRM_FILE: 'README.md', - PUSHRM_TARGET: 'quay.io/thegeeklab/${DRONE_REPO_NAME}', - }, - when: { - status: ['success'], - }, - }, - { - name: 'matrix', - image: 'thegeeklab/drone-matrix', - settings: { - homeserver: { from_secret: 'matrix_homeserver' }, - roomid: { from_secret: 'matrix_roomid' }, - template: 'Status: **{{ build.Status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.Link }}){{#if build.Branch}} ({{ build.Branch }}){{/if}} by {{ commit.Author }}
Message: {{ commit.Message.Title }}', - username: { from_secret: 'matrix_username' }, - password: { from_secret: 'matrix_password' }, - }, - when: { - status: ['success', 'failure'], - }, - }, - ], - depends_on: [ - 'docs', - ], - trigger: { - ref: ['refs/heads/main', 'refs/tags/**'], - status: ['success', 'failure'], - }, -}; - -[ - PipelineTest, - PipelineBuildBinaries, - PipelineBuildContainer(arch='amd64'), - PipelineBuildContainer(arch='arm64'), - PipelineDocs, - PipelineNotifications, -] diff --git a/.drone.yml b/.drone.yml index 5e14f64..4e080b3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -98,39 +98,42 @@ depends_on: --- kind: pipeline -name: build-container-amd64 +name: build-container platform: os: linux arch: amd64 steps: - - name: build - image: golang:1.19 - commands: - - make build - - ls -l dist/drone-github-comment - - name: dryrun image: thegeeklab/drone-docker-buildx:20 settings: - dockerfile: docker/Dockerfile.amd64 + dockerfile: Dockerfile.multiarch dry_run: true + platforms: + - linux/amd64 + - linux/arm64 + - linux/arm/v7 + - linux/arm/v6 + provenance: false repo: thegeeklab/${DRONE_REPO_NAME} when: ref: - refs/pull/** - depends_on: - - build - name: publish-dockerhub image: thegeeklab/drone-docker-buildx:20 settings: auto_tag: true - auto_tag_suffix: amd64 - dockerfile: docker/Dockerfile.amd64 + dockerfile: Dockerfile.multiarch password: from_secret: docker_password + platforms: + - linux/amd64 + - linux/arm64 + - linux/arm/v7 + - linux/arm/v6 + provenance: false repo: thegeeklab/${DRONE_REPO_NAME} username: from_secret: docker_username @@ -145,83 +148,15 @@ steps: image: thegeeklab/drone-docker-buildx:20 settings: auto_tag: true - auto_tag_suffix: amd64 - dockerfile: docker/Dockerfile.amd64 - password: - from_secret: quay_password - registry: quay.io - repo: quay.io/thegeeklab/${DRONE_REPO_NAME} - username: - from_secret: quay_username - when: - ref: - - refs/heads/main - - refs/tags/** - depends_on: - - dryrun - -trigger: - ref: - - refs/heads/main - - refs/tags/** - - refs/pull/** - -depends_on: - - test - ---- -kind: pipeline -name: build-container-arm64 - -platform: - os: linux - arch: arm64 - -steps: - - name: build - image: golang:1.19 - commands: - - make build - - ls -l dist/drone-github-comment - - - name: dryrun - image: thegeeklab/drone-docker-buildx:20 - settings: - dockerfile: docker/Dockerfile.arm64 - dry_run: true - repo: thegeeklab/${DRONE_REPO_NAME} - when: - ref: - - refs/pull/** - depends_on: - - build - - - name: publish-dockerhub - image: thegeeklab/drone-docker-buildx:20 - settings: - auto_tag: true - auto_tag_suffix: arm64 - dockerfile: docker/Dockerfile.arm64 - password: - from_secret: docker_password - repo: thegeeklab/${DRONE_REPO_NAME} - username: - from_secret: docker_username - when: - ref: - - refs/heads/main - - refs/tags/** - depends_on: - - dryrun - - - name: publish-quay - image: thegeeklab/drone-docker-buildx:20 - settings: - auto_tag: true - auto_tag_suffix: arm64 - dockerfile: docker/Dockerfile.arm64 + dockerfile: Dockerfile.multiarch password: from_secret: quay_password + platforms: + - linux/amd64 + - linux/arm64 + - linux/arm/v7 + - linux/arm/v6 + provenance: false registry: quay.io repo: quay.io/thegeeklab/${DRONE_REPO_NAME} username: @@ -288,8 +223,7 @@ trigger: depends_on: - build-binaries - - build-container-amd64 - - build-container-arm64 + - build-container --- kind: pipeline @@ -300,34 +234,6 @@ platform: arch: amd64 steps: - - name: manifest-dockerhub - image: plugins/manifest - settings: - auto_tag: true - ignore_missing: true - password: - from_secret: docker_password - spec: docker/manifest.tmpl - username: - from_secret: docker_username - when: - status: - - success - - - name: manifest-quay - image: plugins/manifest - settings: - auto_tag: true - ignore_missing: true - password: - from_secret: quay_password - spec: docker/manifest-quay.tmpl - username: - from_secret: quay_username - when: - status: - - success - - name: pushrm-dockerhub image: chko/docker-pushrm:1 environment: @@ -383,6 +289,6 @@ depends_on: --- kind: signature -hmac: 5a0a818ba8191d680b8967878a9077af607b6be0810dfc864a64b3da642e9e92 +hmac: 45bc4bc9fd29b6c2b6ea84f10d7f97e1d253296e03396cea4f60cba2aa627f9d ... diff --git a/docker/Dockerfile.amd64 b/Dockerfile.multiarch similarity index 69% rename from docker/Dockerfile.amd64 rename to Dockerfile.multiarch index 6a4a2bf..6f7ffb0 100644 --- a/docker/Dockerfile.amd64 +++ b/Dockerfile.multiarch @@ -1,3 +1,13 @@ +FROM --platform=$BUILDPLATFORM golang:1.19@sha256:bb9811fad43a7d6fd2173248d8331b2dcf5ac9af20976b1937ecd214c5b8c383 as build + +ARG TARGETOS +ARG TARGETARCH + +ADD . /src +WORKDIR /src + +RUN make build + FROM alpine:3.17@sha256:f271e74b17ced29b915d351685fd4644785c6d1559dd1f2d4189a5e851ef753a LABEL maintainer="Robert Kaussow " @@ -7,6 +17,5 @@ LABEL org.opencontainers.image.url="https://github.com/thegeeklab/drone-github-c LABEL org.opencontainers.image.source="https://github.com/thegeeklab/drone-github-comment" LABEL org.opencontainers.image.documentation="https://github.com/thegeeklab/drone-github-comment" -ADD dist/drone-github-comment /bin/ - +COPY --from=build /src/dist/drone-github-comment /bin/drone-github-comment ENTRYPOINT ["/bin/drone-github-comment"] diff --git a/Makefile b/Makefile index 0dc982b..f7e4cad 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,13 @@ XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest GENERATE ?= XGO_VERSION := go-1.19.x -XGO_TARGETS ?= linux/amd64,linux/arm64 +XGO_TARGETS ?= linux/amd64,linux/arm-6,linux/arm-7,linux/arm64 +TARGETOS ?= linux +TARGETARCH ?= amd64 +ifneq ("$(TARGETVARIANT)","") +GOARM ?= $(subst v,,$(TARGETVARIANT)) +endif TAGS ?= netgo ifndef VERSION @@ -69,7 +74,7 @@ test: build: $(DIST)/$(EXECUTABLE) $(DIST)/$(EXECUTABLE): $(SOURCES) - $(GO) build -v -tags '$(TAGS)' -ldflags '-extldflags "-static" $(LDFLAGS)' -o $@ ./cmd/$(EXECUTABLE) + GOOS=${TARGETOS} GOARCH=${TARGETARCH} GOARM=$(GOARM) $(GO) build -v -tags '$(TAGS)' -ldflags '-extldflags "-static" $(LDFLAGS)' -o $@ ./cmd/$(EXECUTABLE) $(DIST_DIRS): mkdir -p $(DIST_DIRS) diff --git a/docker/Dockerfile.arm64 b/docker/Dockerfile.arm64 deleted file mode 100644 index 005c966..0000000 --- a/docker/Dockerfile.arm64 +++ /dev/null @@ -1,12 +0,0 @@ -FROM arm64v8/alpine:3.17@sha256:41d876d4e44348d1c27445fdb0e64592e0eb926d4dbbcf09a3526dee7e628329 - -LABEL maintainer="Robert Kaussow " -LABEL org.opencontainers.image.authors="Robert Kaussow " -LABEL org.opencontainers.image.title="drone-github-comment" -LABEL org.opencontainers.image.url="https://github.com/thegeeklab/drone-github-comment" -LABEL org.opencontainers.image.source="https://github.com/thegeeklab/drone-github-comment" -LABEL org.opencontainers.image.documentation="https://github.com/thegeeklab/drone-github-comment" - -ADD dist/drone-github-comment /bin/ - -ENTRYPOINT ["/bin/drone-github-comment"] diff --git a/docker/manifest-quay.tmpl b/docker/manifest-quay.tmpl deleted file mode 100644 index 461fd59..0000000 --- a/docker/manifest-quay.tmpl +++ /dev/null @@ -1,18 +0,0 @@ -image: quay.io/thegeeklab/drone-github-comment:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} -{{#if build.tags}} -tags: -{{#each build.tags}} - - {{this}} -{{/each}} -{{/if}} -manifests: - - image: quay.io/thegeeklab/drone-github-comment:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}amd64 - platform: - architecture: amd64 - os: linux - - - image: quay.io/thegeeklab/drone-github-comment:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}arm64 - platform: - architecture: arm64 - os: linux - variant: v8 diff --git a/docker/manifest.tmpl b/docker/manifest.tmpl deleted file mode 100644 index 7d78add..0000000 --- a/docker/manifest.tmpl +++ /dev/null @@ -1,18 +0,0 @@ -image: thegeeklab/drone-github-comment:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} -{{#if build.tags}} -tags: -{{#each build.tags}} - - {{this}} -{{/each}} -{{/if}} -manifests: - - image: thegeeklab/drone-github-comment:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}amd64 - platform: - architecture: amd64 - os: linux - - - image: thegeeklab/drone-github-comment:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}arm64 - platform: - architecture: arm64 - os: linux - variant: v8