Skip to content

Commit 9b0c99a

Browse files
Merge pull request #710 from joelanford/goreleaser-multi-arch
opm: add multi-arch builds with goreleaser
2 parents 78f27b3 + a629d38 commit 9b0c99a

File tree

7 files changed

+277
-6
lines changed

7 files changed

+277
-6
lines changed

.github/workflows/goreleaser.yaml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: goreleaser
2+
on:
3+
push:
4+
branches:
5+
- 'master'
6+
tags:
7+
- 'v[0-9]+.[0-9]+.[0-9]+'
8+
pull_request: {}
9+
defaults:
10+
run:
11+
shell: bash
12+
jobs:
13+
goreleaser:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
with:
18+
# GoReleaser requires fetch-depth: 0 for
19+
# changelog generation to work correctly.
20+
fetch-depth: 0
21+
22+
- uses: actions/setup-go@v2
23+
with:
24+
go-version: '~1.16'
25+
26+
- name: "Install linux cross-compilers"
27+
run: sudo apt-get install -y gcc-aarch64-linux-gnu gcc-s390x-linux-gnu gcc-powerpc64le-linux-gnu gcc-mingw-w64-x86-64
28+
29+
- name: "Install yq"
30+
run: |
31+
sudo wget https://github.com/mikefarah/yq/releases/download/v4.10.0/yq_linux_amd64 -O /usr/local/bin/yq
32+
sudo chmod +x /usr/local/bin/yq
33+
34+
- name: "Disable image pushes for pull requests"
35+
if: github.event_name == 'pull_request'
36+
run: |
37+
yq eval '.dockers[].skip_push=true' -i .goreleaser.yaml
38+
yq eval '.docker_manifests[].skip_push=true' -i .goreleaser.yaml
39+
40+
- name: "Disable the Github release upload for non-tag builds"
41+
if: startsWith(github.ref, 'refs/tags') != true
42+
run: |
43+
yq eval '.release.disable=true' -i .goreleaser.yaml
44+
45+
- name: "Set the image tag"
46+
run: |
47+
if [[ $GITHUB_REF == refs/tags/* ]]; then
48+
# Release tags.
49+
echo IMAGE_TAG="${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
50+
elif [[ $GITHUB_REF == refs/heads/* ]]; then
51+
# Branch build.
52+
echo IMAGE_TAG="$(echo "${GITHUB_REF#refs/heads/}" | sed -r 's|/+|-|g')" >> $GITHUB_ENV
53+
elif [[ $GITHUB_REF == refs/pull/* ]]; then
54+
# PR build.
55+
echo IMAGE_TAG="pr-$(echo "${GITHUB_REF}" | sed -E 's|refs/pull/([^/]+)/?.*|\1|')" >> $GITHUB_ENV
56+
else
57+
echo IMAGE_TAG="$(git describe --tags --always)" >> $GITHUB_ENV
58+
fi
59+
60+
- name: "Login to Quay"
61+
if: github.event_name != 'pull_request'
62+
uses: docker/login-action@v1
63+
with:
64+
username: ${{ secrets.QUAY_USERNAME }}
65+
password: ${{ secrets.QUAY_PASSWORD }}
66+
registry: quay.io
67+
68+
69+
- name: "Run GoReleaser"
70+
run: make release
71+
env:
72+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73+
RELEASE_ARGS: release --rm-dist ${{ !startsWith(github.ref, 'refs/tags') && '--skip-validate' || '' }}

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ tags
181181
# Build results
182182
[Dd]ebug/
183183
[Dd]ebugPublic/
184-
[Rr]elease/
185-
[Rr]eleases/
186184
x64/
187185
x86/
188186
bld/

.goreleaser.yaml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
builds:
2+
- id: linux-amd64
3+
main: ./cmd/opm
4+
binary: opm
5+
goos:
6+
- linux
7+
goarch:
8+
- amd64
9+
env:
10+
- CC=gcc
11+
mod_timestamp: "{{ .CommitTimestamp }}"
12+
flags: &build-flags
13+
- -tags=json1
14+
asmflags: &build-asmflags
15+
- all=-trimpath={{ .Env.PWD }}
16+
gcflags: &build-gcflags
17+
- all=-trimpath={{ .Env.PWD }}
18+
ldflags: &build-ldflags
19+
- -s -w
20+
- -X {{ .Env.PKG }}/cmd/opm/version.gitCommit={{ .Env.GIT_COMMIT }}
21+
- -X {{ .Env.PKG }}/cmd/opm/version.opmVersion={{ .Env.OPM_VERSION }}
22+
- -X {{ .Env.PKG }}/cmd/opm/version.buildDate={{ .Env.BUILD_DATE }}
23+
- id: linux-arm64
24+
main: ./cmd/opm
25+
binary: opm
26+
goos:
27+
- linux
28+
goarch:
29+
- arm64
30+
env:
31+
- CC=aarch64-linux-gnu-gcc
32+
mod_timestamp: "{{ .CommitTimestamp }}"
33+
flags: *build-flags
34+
asmflags: *build-asmflags
35+
gcflags: *build-gcflags
36+
ldflags: *build-ldflags
37+
- id: linux-ppc64le
38+
main: ./cmd/opm
39+
binary: opm
40+
goos:
41+
- linux
42+
goarch:
43+
- ppc64le
44+
env:
45+
- CC=powerpc64le-linux-gnu-gcc
46+
mod_timestamp: "{{ .CommitTimestamp }}"
47+
flags: *build-flags
48+
asmflags: *build-asmflags
49+
gcflags: *build-gcflags
50+
ldflags: *build-ldflags
51+
- id: linux-s390x
52+
main: ./cmd/opm
53+
binary: opm
54+
goos:
55+
- linux
56+
goarch:
57+
- s390x
58+
env:
59+
- CC=s390x-linux-gnu-gcc
60+
mod_timestamp: "{{ .CommitTimestamp }}"
61+
flags: *build-flags
62+
asmflags: *build-asmflags
63+
gcflags: *build-gcflags
64+
ldflags: *build-ldflags
65+
- id: windows-amd64
66+
main: ./cmd/opm
67+
binary: opm
68+
goos:
69+
- windows
70+
goarch:
71+
- amd64
72+
env:
73+
- CC=x86_64-w64-mingw32-gcc-posix
74+
mod_timestamp: "{{ .CommitTimestamp }}"
75+
flags: *build-flags
76+
asmflags: *build-asmflags
77+
gcflags: *build-gcflags
78+
ldflags: *build-ldflags
79+
archives:
80+
- id: opm
81+
builds:
82+
- linux-amd64
83+
- linux-arm64
84+
- linux-ppc64le
85+
- linux-s390x
86+
- windows-amd64
87+
name_template: "{{ .Binary }}_{{ .Env.OPM_VERSION }}_{{ .Os }}_{{ .Arch }}"
88+
format: binary
89+
dockers:
90+
- image_templates:
91+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
92+
ids: ["linux-amd64"]
93+
goos: linux
94+
goarch: amd64
95+
dockerfile: release/goreleaser.opm.Dockerfile
96+
extra_files: ["nsswitch.conf"]
97+
use: buildx
98+
build_flag_templates:
99+
- --platform=linux/amd64
100+
- image_templates:
101+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
102+
ids: ["linux-arm64"]
103+
goos: linux
104+
goarch: arm64
105+
dockerfile: release/goreleaser.opm.Dockerfile
106+
extra_files: ["nsswitch.conf"]
107+
use: buildx
108+
build_flag_templates:
109+
- --platform=linux/arm64
110+
- image_templates:
111+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
112+
ids: ["linux-ppc64le"]
113+
goos: linux
114+
goarch: ppc64le
115+
dockerfile: release/goreleaser.opm.Dockerfile
116+
extra_files: ["nsswitch.conf"]
117+
use: buildx
118+
build_flag_templates:
119+
- --platform=linux/ppc64le
120+
- image_templates:
121+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
122+
ids: ["linux-s390x"]
123+
goos: linux
124+
goarch: s390x
125+
dockerfile: release/goreleaser.opm.Dockerfile
126+
extra_files: ["nsswitch.conf"]
127+
use: buildx
128+
build_flag_templates:
129+
- --platform=linux/s390x
130+
docker_manifests:
131+
- name_template: "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}"
132+
image_templates:
133+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
134+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-arm64"
135+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-ppc64le"
136+
- "{{ .Env.OPM_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-s390x"
137+
checksum:
138+
name_template: 'checksums.txt'
139+
snapshot:
140+
name_template: "{{ .Env.OPM_VERSION }}"
141+
release:
142+
name_template: "{{ .Env.OPM_VERSION }}"
143+
draft: true
144+
## Disable release steps, since a separate GitHub Action job
145+
## handles binary builds and release uploads
146+
disable: true

Makefile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ GO := GOFLAGS="-mod=vendor" go
22
CMDS := $(addprefix bin/, $(shell ls ./cmd | grep -v opm))
33
OPM := $(addprefix bin/, opm)
44
SPECIFIC_UNIT_TEST := $(if $(TEST),-run $(TEST),)
5-
PKG := github.com/operator-framework/operator-registry
6-
GIT_COMMIT := $(or $(SOURCE_GIT_COMMIT),$(shell git rev-parse --short HEAD))
7-
OPM_VERSION := $(or $(SOURCE_GIT_TAG),$(shell git describe --always --tags HEAD))
8-
BUILD_DATE := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
5+
export PKG := github.com/operator-framework/operator-registry
6+
export GIT_COMMIT := $(or $(SOURCE_GIT_COMMIT),$(shell git rev-parse --short HEAD))
7+
export OPM_VERSION := $(or $(SOURCE_GIT_TAG),$(shell git describe --always --tags HEAD))
8+
export BUILD_DATE := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
99

1010
# define characters
1111
null :=
@@ -114,3 +114,11 @@ clean:
114114
.PHONY: e2e
115115
e2e:
116116
$(GO) run github.com/onsi/ginkgo/ginkgo --v --randomizeAllSpecs --randomizeSuites --race $(if $(TEST),-focus '$(TEST)') $(TAGS) ./test/e2e -- $(if $(SKIPTLS),-skip-tls true)
117+
118+
119+
.PHONY: release
120+
export OPM_IMAGE_REPO ?= quay.io/operator-framework/opm
121+
export IMAGE_TAG ?= $(OPM_VERSION)
122+
release: RELEASE_ARGS?=release --rm-dist --snapshot
123+
release:
124+
./scripts/fetch goreleaser 0.173.2 && ./bin/goreleaser $(RELEASE_ARGS)

release/goreleaser.opm.Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# NOTE: This Dockerfile is used in conjuction with GoReleaser to
2+
# build opm images. See the configurations in .goreleaser.yaml
3+
# and .github/workflows/release.yaml.
4+
5+
FROM --platform=$BUILDPLATFORM ghcr.io/grpc-ecosystem/grpc-health-probe:v0.4.4 as grpc_health_probe
6+
FROM gcr.io/distroless/base:debug
7+
COPY --from=grpc_health_probe /grpc_health_probe /bin/grpc_health_probe
8+
COPY ["nsswitch.conf", "/etc/nsswitch.conf"]
9+
COPY opm /bin/opm
10+
ENTRYPOINT ["/bin/opm"]

scripts/fetch

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT="$( git rev-parse --show-toplevel )"
4+
5+
fetch() {
6+
local tool=$1; shift
7+
local ver=$1; shift
8+
9+
local ver_cmd=""
10+
local tool_fetch_cmd=""
11+
case "$tool" in
12+
"golangci-lint")
13+
ver_cmd="${ROOT}/bin/golangci-lint --version 2>/dev/null | cut -d\" \" -f4"
14+
fetch_cmd="curl -sSfL \"https://raw.githubusercontent.com/golangci/golangci-lint/v${ver}/install.sh\" | sh -s -- -b \"${ROOT}/bin\" \"v${ver}\""
15+
;;
16+
"goreleaser")
17+
ver_cmd="${ROOT}/bin/goreleaser --version 2>/dev/null | grep 'goreleaser version' | cut -d' ' -f3"
18+
fetch_cmd="curl -sSfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- -b \"${ROOT}/bin\" -d \"v${ver}\""
19+
;;
20+
*)
21+
echo "unknown tool $tool"
22+
return 1
23+
;;
24+
esac
25+
26+
if [[ "${ver}" != "$(eval ${ver_cmd})" ]]; then
27+
echo "${tool} missing or not version '${ver}', downloading..."
28+
eval ${fetch_cmd}
29+
fi
30+
}
31+
fetch $@

upstream-opm-builder.Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
##
2+
## Deprecated: release/goreleaser.opm.Dockerfile is used in conjunction with
3+
## GoReleaser to build and push multi-arch images for opm
4+
##
5+
16
FROM golang:1.16-alpine AS builder
27

38
RUN apk update && apk add sqlite build-base git mercurial bash

0 commit comments

Comments
 (0)