Skip to content

Commit

Permalink
feat: use goreleaser to build binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
orangedeng committed Oct 21, 2024
1 parent 4c1db38 commit 4d17a53
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 124 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ jobs:
- name: Commitsar check
uses: aevea/commitsar@v0.20.2
- name: Build to test
env:
SKIP_COMPRESS: "true"
run: make ci
86 changes: 46 additions & 40 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- release/v*
- main
tags:
- 'v*.*.*' # Matches any tag that starts with 'v' and follows semantic versioning
- "v*.*.*" # Matches any tag that starts with 'v' and follows semantic versioning

jobs:
build-and-deploy:
Expand All @@ -16,68 +16,74 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: CI
env:
CROSS: push
SKIP_PACKAGE: "true"
run: make ci
- name: Prepare for packaging image
run: cp dist/* package/
# aliyun image to test the docker build is ok
- name: Login to Aliyun ACR
uses: docker/login-action@v3
with:
registry: registry.cn-shenzhen.aliyuncs.com
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_TOKEN }}
if: ${{ vars.ALIYUN == 'true' }}
- name: Aliyun image docker meta
if: ${{ vars.ALIYUN == 'true' }}
id: aliyun-meta
uses: docker/metadata-action@v5
with:
images: ${{ vars.REPO || 'cnrancher' }}/${{ vars.IMAGE || 'kube-explorer' }}
tags: |
type=ref,event=tag
type=ref,event=branch,suffix=-head
- name: Build to Aliyun
uses: docker/build-push-action@v6
with:
labels: ${{ steps.aliyun-meta.outputs.labels }}
tags: "${{ steps.aliyun-meta.outputs.tags }}"
context: package
push: true
if: ${{ vars.ALIYUN == 'true' }}

# docker hub multi-arch image
- name: Login to Dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: CI
if: startsWith(github.ref, 'refs/heads/')
env:
CROSS: push
run: make github_ci

- name: CI
if: startsWith(github.ref, 'refs/tags/')
env:
CROSS: tag
run: |
make github_ci
make release-note
- name: Prepare for packaging image
run: cp dist/* package/
- name: Set docker iamge name
id: image-name
env:
REPO_OVERRIDE: ${{ vars.REPO || 'cnrancher' }}
IMAGE_OVERRIDE: ${{ vars.IMAGE || 'kube-explorer' }}
run: |
tag_name=latest;
if [[ ${GITHUB_REF} == refs/heads/release/* ]]; then tag_name=${GITHUB_REF#refs/heads/release/}-head; fi;
if [[ ${GITHUB_REF} == refs/tags/* ]]; then tag_name=${GITHUB_REF#refs/tags/}; fi;
echo "image_name=${REPO_OVERRIDE}/${IMAGE_OVERRIDE}:${tag_name}" >> $GITHUB_OUTPUT;
-
name: Set up QEMU
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ vars.REPO || 'cnrancher' }}/${{ vars.IMAGE || 'kube-explorer' }}
tags: |
type=ref,event=tag
type=ref,event=branch,suffix=-head
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build to Dockerhub
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64/v8
tags: "${{ steps.image-name.outputs.image_name }}"
context: package
push: true
- name: Build to Aliyun
uses: docker/build-push-action@v6
with:
tags: registry.cn-shenzhen.aliyuncs.com/${{ steps.image-name.outputs.image_name }}
labels: ${{ steps.meta.outputs.labels }}
tags: "${{ steps.meta.outputs.tags }}"
context: package
push: true
if: ${{ vars.ALIYUN == 'true' }}

- name: Make release note
if: startsWith(github.ref, 'refs/tags/')
run: |
make release-note
- name: Release
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
files: dist/kube-explorer-*
body_path: dist/release-note
draft: true
draft: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
/internal/ui/ui/
**/Dockerfile.dapper*
!**/Dockerfile.dapper

dist/
74 changes: 74 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 2

dist: bin

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy

builds:
- id: prod
env:
- CGO_ENABLED=0
targets:
- "darwin_amd64"
- "darwin_arm64"
- "linux_amd64"
- "linux_arm64"
- "linux_arm"
- "windows_amd64"
flags:
- -tags=embed
binary: '{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}'
ldflags: |
{{ if ne .Os "darwin" }}
-extldflags -static -s
{{ else }}
-s -w
{{ end }}
-X github.com/cnrancher/kube-explorer/internal/version.Version={{ .Env.VERSION }}
-X github.com/cnrancher/kube-explorer/internal/version.GitCommit={{ .Env.COMMIT }}
-X github.com/cnrancher/kube-explorer/internal/config.APIUIVersion={{ .Env.CATTLE_API_UI_VERSION }}
no_unique_dist_dir: true
- id: dev
env:
- CGO_ENABLED=0
targets:
- "linux_amd64"
flags:
- -tags=embed
binary: '{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}'
ldflags: |
{{ if ne .Os "darwin" }}
-extldflags -static -s
{{ else }}
-s -w
{{ end }}
-X github.com/cnrancher/kube-explorer/internal/version.Version={{ .Env.VERSION }}
-X github.com/cnrancher/kube-explorer/internal/version.GitCommit={{ .Env.COMMIT }}
-X github.com/cnrancher/kube-explorer/internal/config.APIUIVersion={{ .Env.CATTLE_API_UI_VERSION }}
no_unique_dist_dir: true
upx:
- compress: "5"
ids:
- prod
enabled: true
goos:
- linux
- windows

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
7 changes: 4 additions & 3 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM goreleaser/goreleaser:v2.3.2 as goreleaser
FROM aevea/release-notary:0.9.2 as tools

FROM registry.suse.com/bci/golang:1.23
Expand All @@ -8,8 +9,7 @@ ENV HOST_ARCH=${DAPPER_HOST_ARCH} ARCH=${DAPPER_HOST_ARCH}
ENV https_proxy=${PROXY} \
http_proxy=${PROXY}

RUN zypper -n install ca-certificates git-core wget curl unzip tar vim less file xz
RUN zypper install -y -f docker
RUN zypper -n install ca-certificates git-core wget curl unzip tar vim less file xz cosign docker

ENV UPX_VERSION 4.2.1
RUN curl -sL https://github.com/upx/upx/releases/download/v${UPX_VERSION}/upx-${UPX_VERSION}-${ARCH}_linux.tar.xz | tar xvJf - --strip-components=1 -C /tmp && \
Expand All @@ -18,11 +18,12 @@ RUN curl -sL https://github.com/upx/upx/releases/download/v${UPX_VERSION}/upx-${
RUN if [ "${ARCH}" == "amd64" ]; then \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.61.0; \
fi
COPY --from=goreleaser /usr/bin/goreleaser /usr/bin/goreleaser
COPY --from=tools /app/release-notary /usr/local/bin/
ENV CATTLE_DASHBOARD_UI_VERSION="v2.9.2-kube-explorer-ui-rc1"
ENV CATTLE_API_UI_VERSION="1.1.11"

ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS GOPROXY SKIP_COMPRESS GITHUB_REPOSITORY GITHUB_TOKEN GITHUB_REF
ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS GOPROXY GITHUB_TOKEN GITHUB_REF GITHUB_REF_NAME BUILD_TARGET SKIP_PACKAGE
ENV DAPPER_SOURCE /go/src/github.com/cnrancher/kube-explorer
ENV DAPPER_OUTPUT ./bin ./dist
ENV DAPPER_DOCKER_SOCKET true
Expand Down
76 changes: 9 additions & 67 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,19 @@ cd "$(dirname $0)/.."

rm -rf ./bin/* ./dist/*

OS_ARCH_ARG_LINUX="amd64 arm arm64"
OS_ARCH_ARG_DARWIN="amd64 arm64"
OS_ARCH_ARG_WINDOWS="amd64"
BUILD_TARGET="${BUILD_TARGET:-dev}"

LD_INJECT_VALUES="-X github.com/cnrancher/kube-explorer/internal/version.Version=$VERSION
-X github.com/cnrancher/kube-explorer/internal/version.GitCommit=$COMMIT
-X github.com/cnrancher/kube-explorer/internal/config.APIUIVersion=$CATTLE_API_UI_VERSION"
if [ -n "$CROSS" ]; then
BUILD_TARGET="prod"
fi

[ "$(uname)" != "Darwin" ] && LINKFLAGS="-extldflags -static -s"
BUILD_ARG="${BUILD_ARG:-} --skip validate --id ${BUILD_TARGET}"

case "$CROSS" in
"push")
for ARCH in ${OS_ARCH_ARG_LINUX}; do
OUTPUT_BIN="bin/kube-explorer-linux-$ARCH"
echo "Building binary for linux/$ARCH..."
GOARCH=$ARCH GOOS=linux CGO_ENABLED=0 go build -tags embed \
-ldflags \
"$LD_INJECT_VALUES $LINKFLAGS" \
-o ${OUTPUT_BIN}
done
;;
"tag")
for ARCH in ${OS_ARCH_ARG_LINUX}; do
OUTPUT_BIN="bin/kube-explorer-linux-$ARCH"
echo "Building binary for linux/$ARCH..."
GOARCH=$ARCH GOOS=linux CGO_ENABLED=0 go build -tags embed \
-ldflags \
"$LD_INJECT_VALUES $LINKFLAGS" \
-o ${OUTPUT_BIN}
done

for ARCH in ${OS_ARCH_ARG_DARWIN}; do
OUTPUT_BIN="bin/kube-explorer-darwin-$ARCH"
echo "Building binary for darwin/$ARCH..."
GOARCH=$ARCH GOOS=darwin CGO_ENABLED=0 go build -tags embed \
-ldflags \
"$LD_INJECT_VALUES" \
-o ${OUTPUT_BIN}
done
mkdir -p "./bin"

for ARCH in ${OS_ARCH_ARG_WINDOWS}; do
OUTPUT_BIN="bin/kube-explorer-windows-$ARCH.exe"
echo "Building binary for windows/$ARCH..."
GOARCH=$ARCH GOOS=windows CGO_ENABLED=0 go build -tags embed \
-ldflags \
"$LD_INJECT_VALUES" \
-o ${OUTPUT_BIN}
done
;;
*)
# only build one for current platform
CGO_ENABLED=0 go build -tags embed \
-ldflags \
"$LD_INJECT_VALUES $LINKFLAGS" \
-o "bin/kube-explorer-$(uname | tr '[:upper:]' '[:lower:]')-${ARCH}"
;;
esac
# upx is handled by goreleaser
VERSION=${VERSION} COMMIT=${COMMIT} goreleaser build $BUILD_ARG

mkdir -p "./bin"
mkdir -p "./dist"

for f in ./bin/*; do
filename=$(basename "$f")
if [[ $filename != *darwin* && -z "$SKIP_COMPRESS" ]]; then
if upx -o "./dist/$filename" "$f"; then
echo "UPX done for $filename!"
else
echo "UPX failed for $filename, copying original file."
cp "$f" "./dist/$filename"
fi
else
cp "$f" "./dist/$filename"
fi
done
cp -r bin/kube-explorer-* dist/
4 changes: 3 additions & 1 deletion scripts/ci
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ cd $(dirname $0)
./download
./validate
./build
./package
if [ -z "${SKIP_PACKAGE}" ]; then
./package
fi
9 changes: 0 additions & 9 deletions scripts/github_ci

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/version
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash

if [[ ${GITHUB_REF} == refs/tags/* ]]; then
DRONE_TAG=${DRONE_TAG:-${GITHUB_REF#refs/tags/}}
GIT_TAG=${GIT_TAG:-${GITHUB_REF_NAME}}
fi

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
DIRTY="-dirty"
fi

COMMIT=$(git rev-parse --short HEAD)
GIT_TAG=${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)}
GIT_TAG=${GIT_TAG:-$(git tag -l --contains HEAD | head -n 1)}

if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then
VERSION=$GIT_TAG
Expand Down

0 comments on commit 4d17a53

Please sign in to comment.