Skip to content

Commit a9c6d46

Browse files
authored
Add pipeline to publish ghcr multi-arch image (zalando#2268)
Refactor operator image build process Add a pipeline to build and publish arm64/amd64 image in ghcr on every pushed tag
1 parent fc86c44 commit a9c6d46

File tree

5 files changed

+104
-10
lines changed

5 files changed

+104
-10
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Publish multiarch postgres-operator image on ghcr.io
2+
3+
env:
4+
REGISTRY: ghcr.io
5+
IMAGE_NAME: ${{ github.repository }}
6+
7+
on:
8+
push:
9+
tags:
10+
- '*'
11+
jobs:
12+
publish:
13+
name: Build, test and push image
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v3
21+
22+
- uses: actions/setup-go@v2
23+
with:
24+
go-version: "^1.18.9"
25+
26+
- name: Run unit tests
27+
run: make deps mocks test
28+
29+
- name: Define image name
30+
id: image
31+
run: |
32+
IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF/refs\/tags\//}"
33+
echo "NAME=$IMAGE" >> $GITHUB_OUTPUT
34+
35+
- name: Set up QEMU
36+
uses: docker/setup-qemu-action@v2
37+
38+
- name: Set up Docker Buildx
39+
uses: docker/setup-buildx-action@v2
40+
41+
- name: Login to GHCR
42+
uses: docker/login-action@v2
43+
with:
44+
registry: ${{ env.REGISTRY }}
45+
username: ${{ github.actor }}
46+
password: ${{ secrets.GITHUB_TOKEN }}
47+
48+
- name: Build and push multiarch image to ghcr
49+
uses: docker/build-push-action@v3
50+
with:
51+
context: .
52+
file: docker/Dockerfile
53+
push: true
54+
build-args: BASE_IMAGE=alpine:3.15
55+
tags: "${{ steps.image.outputs.NAME }}"
56+
platforms: linux/amd64,linux/arm64

.github/workflows/run_e2e.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ jobs:
1919
run: make deps mocks
2020
- name: Code generation
2121
run: make codegen
22-
- name: Compile
23-
run: make linux
2422
- name: Run unit tests
2523
run: make test
2624
- name: Run end-2-end tests

Makefile

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,13 @@ linux: ${SOURCES}
6060
macos: ${SOURCES}
6161
GOOS=darwin GOARCH=amd64 CGO_ENABLED=${CGO_ENABLED} go build -o build/macos/${BINARY} ${BUILD_FLAGS} -ldflags "$(LDFLAGS)" $^
6262

63-
docker-context: scm-source.json linux
64-
mkdir -p docker/build/
65-
cp build/linux/${BINARY} scm-source.json docker/build/
66-
67-
docker: ${DOCKERDIR}/${DOCKERFILE} docker-context
63+
docker: ${DOCKERDIR}/${DOCKERFILE} scm-source.json
6864
echo `(env)`
6965
echo "Tag ${TAG}"
7066
echo "Version ${VERSION}"
7167
echo "CDP tag ${CDP_TAG}"
7268
echo "git describe $(shell git describe --tags --always --dirty)"
73-
cd "${DOCKERDIR}" && docker build --rm -t "$(IMAGE):$(TAG)$(CDP_TAG)$(DEBUG_FRESH)$(DEBUG_POSTFIX)" -f "${DOCKERFILE}" .
69+
docker build --rm -t "$(IMAGE):$(TAG)$(CDP_TAG)$(DEBUG_FRESH)$(DEBUG_POSTFIX)" -f "${DOCKERDIR}/${DOCKERFILE}" --build-arg VERSION="${VERSION}" .
7470

7571
indocker-race:
7672
docker run --rm -v "${GOPATH}":"${GOPATH}" -e GOPATH="${GOPATH}" -e RACE=1 -w ${PWD} golang:1.18.9 bash -c "make linux"

docker/Dockerfile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
FROM registry.opensource.zalan.do/library/alpine-3.15:latest
1+
ARG BASE_IMAGE=registry.opensource.zalan.do/library/alpine-3.15:latest
2+
ARG VERSION=latest
3+
4+
FROM ubuntu:20.04 as builder
5+
6+
ARG VERSION
7+
8+
COPY . /go/src/github.com/zalando/postgres-operator
9+
WORKDIR /go/src/github.com/zalando/postgres-operator
10+
11+
ENV OPERATOR_LDFLAGS="-X=main.version=${VERSION}"
12+
RUN bash docker/build_operator.sh
13+
14+
FROM ${BASE_IMAGE}
215
LABEL maintainer="Team ACID @ Zalando <[email protected]>"
316

417
# We need root certificates to deal with teams api over https
518
RUN apk --no-cache add curl
619
RUN apk --no-cache add ca-certificates
720

8-
COPY build/* /
21+
COPY --from=builder /go/src/github.com/zalando/postgres-operator/build/* /
922

1023
RUN addgroup -g 1000 pgo
1124
RUN adduser -D -u 1000 -G pgo -g 'Postgres Operator' pgo

docker/build_operator.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
export DEBIAN_FRONTEND=noninteractive
4+
5+
arch=$(dpkg --print-architecture)
6+
7+
set -ex
8+
9+
# Install dependencies
10+
11+
apt-get update
12+
apt-get install -y wget
13+
14+
(
15+
cd /tmp
16+
wget -q "https://storage.googleapis.com/golang/go1.18.9.linux-${arch}.tar.gz" -O go.tar.gz
17+
tar -xf go.tar.gz
18+
mv go /usr/local
19+
ln -s /usr/local/go/bin/go /usr/bin/go
20+
go version
21+
)
22+
23+
# Build
24+
25+
export PATH="$PATH:$HOME/go/bin"
26+
export GOPATH="$HOME/go"
27+
mkdir -p build
28+
cp scm-source.json build/
29+
30+
GO111MODULE=on go mod vendor
31+
CGO_ENABLED=0 go build -o build/postgres-operator -v -ldflags "$OPERATOR_LDFLAGS" cmd/main.go

0 commit comments

Comments
 (0)