Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add CI for oceanbase cli #570

Merged
merged 14 commits into from
Sep 25, 2024
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ jobs:
run: go build -o bin/manager cmd/operator/main.go

- name: go build oceanbase-dashboard
run: go build -o bin/oceanbase-dashboard cmd/dashboard/main.go
run: go build -o bin/oceanbase-dashboard cmd/dashboard/main.go

- name: go build oceanbase-cli
run: go build -o bin/obocli cmd/cli/main.go

37 changes: 37 additions & 0 deletions .github/workflows/release-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: release cli

on:
push:
tags:
- "cli-[0-9]+.[0-9]+.[0-9]+"

env:
tagName: ${{ github.ref_name }}
GO_VERSION : "1.22"
BinaryName: "obocli"

jobs:
release-oceanbase-cli:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set Version variables
id: set_version_vars
run: |
echo "version=$(echo $tagName | grep -P '(\d*\.\d*\.\d*)' --only-matching)" >> $GITHUB_OUTPUT

- name: Set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }} # The Go version to download (if necessary) and use.

- name: go mod vendor
run: go mod vendor

- run: ./scripts/create-cli-release.sh "${tag}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ env.tagName }}

12 changes: 5 additions & 7 deletions internal/cli/generated/bindata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions make/cli.mk
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
##@ cli

PROJECT=oceanbase-cli
PROCESSOR=4
PWD ?= $(shell pwd)
BINARY_NAME ?= obocli
CLI_VERSION ?= 0.1.0
GOARCH ?=$(shell uname -m)
GOOS ?= $(shell uname -s | tr LD ld)

# If GOARCH not specified, set GOARCH based on the detected architecture
ifeq ($(GOARCH),x86_64)
GOARCH = amd64
else ifeq ($(GOARCH),aarch64)
GOARCH = arm64
endif

# build args
CLI_BUILD := GO11MODULE=ON CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build

# build dir for obocli
BUILD_DIR?=bin/

.PHONY: cli-build
cli-build: cli-bindata-gen cli-dep-install # Build oceanbase-cli
go build -o bin/obocli cmd/cli/main.go
cli-build: cli-dep-install cli-bindata-gen # Build oceanbase-cli
@echo "Building $(BINARY_NAME) for $(GOOS)/$(GOARCH)..."
$(CLI_BUILD) -o $(BUILD_DIR)$(BINARY_NAME) cmd/cli/main.go

.PHONY: cli-bindata-gen
cli-bindata-gen: # Generate bindata
go-bindata -o internal/cli/generated/bindata/bindata.go -pkg bindata internal/assets/cli-templates/...

.PHONY: cli-clean
cli-clean: # Clean build
rm -rf bin/obocli
rm -rf $(RELEASE_DIR)/$(BINARY_NAME)
go clean -i ./...

.PHONY : cli-dep-install
cli-dep-install: # Install oceanbase-cli deps
go install github.com/spf13/cobra
go install github.com/go-bindata/go-bindata/[email protected]+incompatible

.PHONY : cli-run
cli-run: ## Run oceanbase-cli in dev mode
go run ./cmd/cli/main.go

go run -p 4 ./cmd/cli/main.go
89 changes: 89 additions & 0 deletions scripts/create-cli-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail

if [[ -z "${1-}" ]]; then
echo "Usage: $0 TAG"
echo "TAG: the tag to build or release, e.g. cli-0.0.1"
exit 1
fi

git_tag=$1
echo "release tag: $git_tag"

# Build the release binaries for every OS/arch combination.
# It builds compressed artifacts on $release_dir.
function build_binary {
binary_name="obocli"
echo "build $binary_name binaries"
version=$1

release_dir=$2
echo "build release artifacts to $release_dir"

# Create tmp dir for make binaries
mkdir -p "output"

# Note: windows not supported yet
platforms=("linux" "darwin")
arch_list=("amd64" "arm64")
for os in "${platforms[@]}"; do
for arch in "${arch_list[@]}"; do
echo "Building $os-$arch"
make cli-build GOOS=$os GOARCH=$arch BUILD_DIR=output/
if [ $? -ne 0 ]; then
echo "Build failed for $os-$arch"
exit 1
fi
# Compress as tar.gz format
tar cvfz "${release_dir}/${binary_name}_${version}_${os}_${arch}.tar.gz" -C output $binary_name
rm output/$binary_name
done
done

# Create checksum.txt
pushd "${release_dir}"
for release in *; do
echo "generate checksum: $release"
sha256sum "$release" >>checksums.txt
done
popd

rmdir output
}

function create_release {
git_tag=$1

# This is expected to match $module.
module=${git_tag%-*}

# This is the version of cli
version=${git_tag##*-}

additional_release_artifacts_arg=""

# Build cli binaries for all supported platforms
if [[ "$module" == "cli" ]]; then
release_artifact_dir=$(mktemp -d)
build_binary "$version" "$release_artifact_dir"

additional_release_artifacts_arg=("$release_artifact_dir"/*)

# Create github releases
gh release create "$git_tag" \
--title "$git_tag" \
--notes "$git_tag" \
--draft "${additional_release_artifacts_arg[@]}"

return
fi

# Create github releases
gh release create "$git_tag" \
--title "$git_tag" \
--draft
}

create_release "$git_tag"
Loading