From a23184c63e9c3ebb896a99d925e0c25a027939ed Mon Sep 17 00:00:00 2001 From: Navid Yaghoobi Date: Sat, 23 Sep 2023 16:36:43 +1000 Subject: [PATCH] multiarch container image build Signed-off-by: Navid Yaghoobi --- .github/workflows/build-and-publish.yaml | 41 +++++++++++++----------- Containerfile | 11 ++++--- Makefile | 22 +++++++++++++ 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build-and-publish.yaml b/.github/workflows/build-and-publish.yaml index 51844421..5b4477e2 100644 --- a/.github/workflows/build-and-publish.yaml +++ b/.github/workflows/build-and-publish.yaml @@ -28,7 +28,10 @@ jobs: - name: Build binary run: | - make binary-remote + make binary-remote-amd64 + make binary-remote-s390x + make binary-remote-ppc64le + make binary-remote-arm64 - name: Get image tags id: image_tag @@ -37,29 +40,31 @@ jobs: VERSION=$(grep 'VERSION=' VERSION | awk -F= '{print $2'}) REVISION=$(grep 'REVISION=' VERSION | awk -F= '{print $2'}) if [[ "${REVISION}" =~ "dev." ]] ; then - echo "develop" + echo "${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAMESPACE }}:develop" else - echo "v${VERSION} latest" + echo "${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAMESPACE }}:v${VERSION},${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAMESPACE }}:latest" fi - - name: Build container image - uses: redhat-actions/buildah-build@v2 - id: build_image - with: - image: ${{ env.IMAGE_NAMESPACE }} - tags: "${{ steps.image_tag.outputs.IMAGE_TAG }}" - containerfiles: | - ./Containerfile + - name: Set up qemu + uses: docker/setup-qemu-action@v3 + + - name: Set up buildx + uses: docker/setup-buildx-action@v3 - - name: Publish container image - id: push_to_quay - uses: redhat-actions/push-to-registry@v2 + - name: Login to Quay.io + uses: docker/login-action@v3 with: - image: ${{ steps.build_image.outputs.image }} - tags: ${{ steps.build_image.outputs.tags }} registry: ${{ env.IMAGE_REGISTRY }} username: ${{ secrets.REGISTRY_USERNAME }} password: ${{ secrets.REGISTRY_PASSWORD }} - - name: Print image url - run: echo "Image pushed to ${{ steps.push_to_quay.outputs.registry-paths }}" + - name: Build container image + uses: docker/build-push-action@v5 + id: build_image + with: + context: . + platforms: linux/amd64,linux/s390x,linux/ppc64le,linux/arm64, + push: true + provenance: false + file: "Containerfile" + tags: ${{ steps.image_tag.outputs.IMAGE_TAG }} diff --git a/Containerfile b/Containerfile index 5c60c6cd..059276ed 100644 --- a/Containerfile +++ b/Containerfile @@ -1,10 +1,11 @@ -ARG ARCH="amd64" -ARG OS="linux" -FROM quay.io/prometheus/busybox-${OS}-${ARCH}:latest - +FROM --platform=${TARGETPLATFORM} quay.io/prometheus/busybox-${TARGETOS}-${TARGETARCH}:latest LABEL maintainer="Navid Yaghoobi " -COPY ./bin/remote/prometheus-podman-exporter /bin/podman_exporter +ARG TARGETPLATFORM +ARG TARGETOS +ARG TARGETARCH + +COPY ./bin/remote/prometheus-podman-exporter-${TARGETARCH} /bin/podman_exporter EXPOSE 9882 USER nobody diff --git a/Makefile b/Makefile index ee07f87a..afe003fa 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,29 @@ binary-remote: ## Build prometheus-podman-exporter for remote connection @mkdir -p $(BIN)/remote @export CGO_ENABLED=0 && $(GO) build $(BUILDFLAGS) --tags "remote containers_image_openpgp" -ldflags="-X '$(PKG_PATH)/cmd.buildVersion=$(VERSION)' -X '$(PKG_PATH)/cmd.buildRevision=$(REVISION)' -X '$(PKG_PATH)/cmd.buildBranch=$(BRANCH)'" -o $(BIN)/remote/$(TARGET) +.PHONY: binary-remote-amd64 +binary-remote-amd64: ## Build amd64 prometheus-podman-exporter for remote connection + @mkdir -p $(BIN)/remote + @echo "building amd64" + @export CGO_ENABLED=0 && GOARCH=amd64 $(GO) build $(BUILDFLAGS) --tags "remote containers_image_openpgp" -ldflags="-X '$(PKG_PATH)/cmd.buildVersion=$(VERSION)' -X '$(PKG_PATH)/cmd.buildRevision=$(REVISION)' -X '$(PKG_PATH)/cmd.buildBranch=$(BRANCH)'" -o $(BIN)/remote/$(TARGET)-amd64 + +.PHONY: binary-remote-s390x +binary-remote-s390x: ## Build s390x prometheus-podman-exporter for remote connection + @mkdir -p $(BIN)/remote + @echo "building s390x" + @export CGO_ENABLED=0 && GOARCH=s390x $(GO) build $(BUILDFLAGS) --tags "remote containers_image_openpgp" -ldflags="-X '$(PKG_PATH)/cmd.buildVersion=$(VERSION)' -X '$(PKG_PATH)/cmd.buildRevision=$(REVISION)' -X '$(PKG_PATH)/cmd.buildBranch=$(BRANCH)'" -o $(BIN)/remote/$(TARGET)-s390x +.PHONY: binary-remote-ppc64le +binary-remote-ppc64le: ## Build ppc64le prometheus-podman-exporter for remote connection + @mkdir -p $(BIN)/remote + @echo "building ppc64le" + @export CGO_ENABLED=0 && GOARCH=ppc64le $(GO) build $(BUILDFLAGS) --tags "remote containers_image_openpgp" -ldflags="-X '$(PKG_PATH)/cmd.buildVersion=$(VERSION)' -X '$(PKG_PATH)/cmd.buildRevision=$(REVISION)' -X '$(PKG_PATH)/cmd.buildBranch=$(BRANCH)'" -o $(BIN)/remote/$(TARGET)-ppc64le + +.PHONY: binary-remote-arm64 +binary-remote-arm64: ## Build arm64 prometheus-podman-exporter for remote connection + @mkdir -p $(BIN)/remote + @echo "building arm64" + @export CGO_ENABLED=0 && GOARCH=arm64 $(GO) build $(BUILDFLAGS) --tags "remote containers_image_openpgp" -ldflags="-X '$(PKG_PATH)/cmd.buildVersion=$(VERSION)' -X '$(PKG_PATH)/cmd.buildRevision=$(REVISION)' -X '$(PKG_PATH)/cmd.buildBranch=$(BRANCH)'" -o $(BIN)/remote/$(TARGET)-arm64 .PHONY: $(TARGET) $(TARGET): $(SRC)