Skip to content

Commit

Permalink
Merge pull request #10 from stoneshi-yunify/main
Browse files Browse the repository at this point in the history
add build workflows and support multi-arch build
  • Loading branch information
stoneshi-yunify authored Apr 15, 2024
2 parents 2f39c69 + b0b1fdf commit a8d574d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 28 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Build

on:
workflow_dispatch:
inputs:
tag:
description: "Image Tag"
default: "latest"
push:
branches:
- 'main'
- 'release-*'
tags:
- 'v*'

jobs:
buildx:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Prepare
id: prepare
run: |
VERSION=latest
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
fi
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
VERSION=${{ github.event.inputs.tag }}
fi
echo ::set-output name=version::${VERSION}
- name: Docker meta for kubesphere
id: meta
uses: docker/metadata-action@v3
with:
images: docker.io/kubesphere/storageclass-accessor
tags: ${{ steps.prepare.outputs.version }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Build and push Docker images
uses: docker/build-push-action@v2.4.0
with:
context: .
file: ./Dockerfile
tags: ${{ steps.meta.outputs.tags }}
push: true
load: false
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
17 changes: 3 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
FROM golang:1.16 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
ADD . /workspace/

# Copy the go source
COPY main.go main.go
COPY webhook/ webhook/
COPY client/ client/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
RUN make build-local

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM kubesphere/distroless-static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/bin/manager .
USER 65532:65532

ENTRYPOINT ["/manager"]
27 changes: 13 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
GIT_COMMIT=$(shell git rev-parse HEAD | head -c 7)
IMG ?= kubesphere/storageclass-accessor:${GIT_COMMIT}
IMGLATEST ?= kubesphere/storageclass-accessor:latest
REPO ?= kubesphere
TAG ?= latest

.PHONY: build
build:
go mod tidy && go mod verify && go build -o bin/manager main.go
build-local: ; $(info $(M)...Begin to build storageclass-accessor binary.) @ ## Build storageclass-accessor.
CGO_ENABLED=0 go build -ldflags \
"-X 'main.goVersion=$(shell go version|sed 's/go version //g')' \
-X 'main.gitHash=$(shell git describe --dirty --always --tags)' \
-X 'main.buildTime=$(shell TZ=UTC-8 date +%Y-%m-%d" "%H:%M:%S)'" \
-o bin/manager main.go

.PHONY: docker-build
docker-build: #test ## Build docker image with the manager.
docker build -t ${IMG} .
build-image: ; $(info $(M)...Begin to build storageclass-accessor image.) @ ## Build storageclass-accessor image.
docker build -f Dockerfile -t ${REPO}/storageclass-accessor:${TAG} .
docker push ${REPO}/storageclass-accessor:${TAG}

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}
docker tag ${IMG} ${IMGLATEST}
docker push ${IMGLATEST}
build-cross-image: ; $(info $(M)...Begin to build storageclass-accessor image.) @ ## Build storageclass-accessor image.
docker buildx build -f Dockerfile -t ${REPO}/storageclass-accessor:${TAG} --push --platform linux/amd64,linux/arm64 .

0 comments on commit a8d574d

Please sign in to comment.