diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..98cb158 --- /dev/null +++ b/.github/workflows/build.yaml @@ -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 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index cd43500..4be37ab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/Makefile b/Makefile index c3611ee..dfbb355 100644 --- a/Makefile +++ b/Makefile @@ -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} \ No newline at end of file +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 . \ No newline at end of file