From f36a8792c5c8ba248b41be82c9f72d22281fdf44 Mon Sep 17 00:00:00 2001 From: Antoine Grondin Date: Mon, 4 Nov 2024 14:00:34 +0900 Subject: [PATCH] build(humanlog): make the pro build publish without goreleaser --- .github/workflows/release-dev-pro.yml | 44 --------- .github/workflows/release-dev.yml | 90 ++++++++++++++---- pkg/localstorage/queryable.go | 2 - script/build.sh | 115 +++++++++++++++++++++++ script/publish.sh | 128 ++++++++++++++++++++++++++ 5 files changed, 313 insertions(+), 66 deletions(-) delete mode 100644 .github/workflows/release-dev-pro.yml create mode 100755 script/build.sh create mode 100755 script/publish.sh diff --git a/.github/workflows/release-dev-pro.yml b/.github/workflows/release-dev-pro.yml deleted file mode 100644 index 857c96b7..00000000 --- a/.github/workflows/release-dev-pro.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Create a dev pro release - -on: - workflow_dispatch: - push: - branches: - - master - -permissions: - contents: write - -jobs: - build: - name: "build" - strategy: - matrix: - go: [stable] - os: - - ubuntu-latest - - macos-latest - arch: - - arm64 - - amd64 - runs-on: ${{ matrix.os }} - env: - GOPRIVATE: "github.com/humanlogio/humanlog-pro" - GH_ACCESS_TOKEN_FOR_HUMANLOGPRO: ${{ secrets.GH_ACCESS_TOKEN_FOR_HUMANLOGPRO }} - GOARCH: ${{ matrix.arch }} - CGO_ENABLED: 1 - steps: - - name: Set up Go 1 - uses: actions/setup-go@v5 - with: - go-version: ${{ matrix.go }} - cache: true - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - run: git config --global --add safe.directory '*' - - run: git config --global url.https://$GH_ACCESS_TOKEN_FOR_HUMANLOGPRO@github.com/.insteadOf https://github.com/ - - run: go install github.com/goware/modvendor@latest - - run: go mod vendor - - run: modvendor -copy="**/*.a **/*.h" -v - - run: go build -tags pro -o /tmp/humanlog-${{ matrix.os }}-${{ matrix.arch }} ./cmd/humanlog diff --git a/.github/workflows/release-dev.yml b/.github/workflows/release-dev.yml index 0f39dbcc..29145d1f 100644 --- a/.github/workflows/release-dev.yml +++ b/.github/workflows/release-dev.yml @@ -10,41 +10,91 @@ permissions: contents: write jobs: - create_a_dev_release: - name: create a dev release - runs-on: ubuntu-latest + build: + strategy: + matrix: + targets: + - name: linux-amd64 + runner: ubuntu-latest + tags: pro + goos: linux + goarch: amd64 + - name: linux-arm64 + runner: ubuntu-latest + goos: linux + goarch: arm64 + goenvs: CC=aarch64-linux-gnu-gcc CXX=x86_64-linux-gnu-g++ + preinstall: sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu + - name: darwin-amd64 + runner: macos-latest + tags: pro + goos: darwin + goarch: amd64 + - name: darwin-arm64 + runner: macos-latest + tags: pro + goos: darwin + goarch: arm64 + name: ${{ matrix.targets.name }} + runs-on: ${{ matrix.targets.runner }} env: GOPRIVATE: "github.com/humanlogio/humanlog-pro" GH_ACCESS_TOKEN_FOR_HUMANLOGPRO: ${{ secrets.GH_ACCESS_TOKEN_FOR_HUMANLOGPRO }} + CGO_ENABLED: 1 + GOBIN: /usr/local/bin/ steps: + - name: Set up Go 1 + uses: actions/setup-go@v5 + with: + go-version: stable + cache: true - uses: actions/checkout@v3 with: fetch-depth: 0 + - if: ${{ matrix.targets.preinstall != '' }} + run: ${{ matrix.targets.preinstall }} + - run: echo "${HOME}/.humanlog/bin" >> $GITHUB_PATH + - run: curl https://humanlog.io/install_apictl.sh | bash - run: git config --global --add safe.directory '*' - run: git config --global url.https://$GH_ACCESS_TOKEN_FOR_HUMANLOGPRO@github.com/.insteadOf https://github.com/ - - run: git fetch --force --tags - - uses: actions/setup-go@v3 - with: - go-version: ">=1.23" - cache: true - - run: echo "${HOME}/.humanlog/bin" >> $GITHUB_PATH - - run: curl https://humanlog.dev/install_apictl.sh | bash - run: go install github.com/goware/modvendor@latest - run: go mod vendor - run: modvendor -copy="**/*.a **/*.h" -v - - uses: goreleaser/goreleaser-action@v6 - with: - distribution: goreleaser - version: v2 - args: release --verbose --clean --draft --skip validate --config .goreleaser-dev.yaml + - run: mkdir -p dist/${{ matrix.targets.goos }}-${{ matrix.targets.goarch }} + - run: ${{ matrix.targets.goenvs }} ./script/build.sh env: - GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }} - AWS_ACCESS_KEY_ID: ${{ secrets.S3_BINARIES_BUCKET_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_BINARIES_BUCKET_ACCESS_KEY }} - - run: ./script/create_version_artifacts.sh + HUMANLOG_GOTAGS: ${{ matrix.targets.tags }} + GOOS: ${{ matrix.targets.goos }} + GOARCH: ${{ matrix.targets.goarch }} + - uses: actions/upload-artifact@v4 + with: + name: humanlog-bins-${{ matrix.targets.goos }}-${{ matrix.targets.goarch }} + path: dist/* + + publish: + name: "publish" + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - run: echo "${HOME}/.humanlog/bin" >> $GITHUB_PATH + - run: curl https://humanlog.io/install_apictl.sh | bash + - uses: actions/upload-artifact/merge@v4 + with: + name: humanlog-bins + pattern: humanlog-bins-* + separate-directories: true + delete-merged: true + - uses: actions/download-artifact@v4 + with: + name: humanlog-bins + - run: ./script/publish.sh env: CHANNEL: "dev" - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} API_URL: "https://api.humanlog.dev" HMAC_KEY_ID: ${{ secrets.DEV_HMAC_KEY_ID }} HMAC_PRIVATE_KEY: ${{ secrets.DEV_HMAC_PRIVATE_KEY }} + AWS_ACCESS_KEY_ID: ${{ secrets.S3_BINARIES_BUCKET_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_BINARIES_BUCKET_ACCESS_KEY }} diff --git a/pkg/localstorage/queryable.go b/pkg/localstorage/queryable.go index a6ef7eff..f1b16f76 100644 --- a/pkg/localstorage/queryable.go +++ b/pkg/localstorage/queryable.go @@ -3,7 +3,6 @@ package localstorage import ( "context" "fmt" - "log" "log/slog" "time" @@ -16,7 +15,6 @@ type StorageBuilder func(ctx context.Context, ll *slog.Logger, cfg map[string]in var registry = make(map[string]StorageBuilder) func RegisterStorage(name string, builder StorageBuilder) { - log.Printf("registered storage engine %q", name) _, ok := registry[name] if ok { panic(fmt.Sprintf("already used: %q", name)) diff --git a/script/build.sh b/script/build.sh new file mode 100755 index 00000000..c00db665 --- /dev/null +++ b/script/build.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash + +set -euox pipefail + +root=$(git rev-parse --show-toplevel) + +gotags="${HUMANLOG_GOTAGS:-}" +goos="${GOOS:-$(go env GOOS)}" +goarch="${GOARCH:-$(go env GOARCH)}" +is_prod_build="${HUMANLOG_IS_PROD_BUILD:-}" +output_dir="${HUMANLOG_OUTDIR:-${root}/dist}" +output_bin="${HUMANLOG_OUTBIN:-humanlog}" +output_path="${output_dir}/${output_bin}" + +function latest_git_tag() { + git describe --tags $(git rev-list --tags --max-count=1) +} + +function git_commit_timestamp() { + git show --no-patch --format=%ct +} + +function git_short_commit() { + git rev-parse --short HEAD +} + +function current_version() { + apictl version to-json --version $(latest_git_tag) | + apictl version math build set $(git_short_commit) +} + +function next_patch_version() { + current_version | + apictl version math patch add 1 | + apictl version math pre add next | + apictl version math pre add $(git_commit_timestamp) +} + +function get_major() { + jq -r '.major // 0' +} + +function get_minor() { + jq -r '.minor // 0' +} + +function get_patch() { + jq -r '.patch // 0' +} + +function get_pre() { + jq -r '.prereleases // [] | join(".")' +} + +function get_build() { + jq -r '.build // ""' +} + +function main() { + local major="" + local minor="" + local patch="" + local pre="" + local build="" + local version="" + if [[ "${is_prod_build}" != "true" ]]; then + echo "setting version to $(next_patch_version | apictl version from-json)" + major=$(next_patch_version | get_major) + minor=$(next_patch_version | get_minor) + patch=$(next_patch_version | get_patch) + pre=$(next_patch_version | get_pre) + build=$(next_patch_version | get_build) + version=$(next_patch_version | apictl version from-json) + else + major=$(current_version | get_major) + minor=$(current_version | get_minor) + patch=$(current_version | get_patch) + build=$(current_version | get_build) + version=$(current_version | apictl version from-json) + fi + local tarname="humanlog_${version}_${goos}_${goarch}.tar.gz" + + local flags="-o ${output_path} -trimpath" + if [[ ! -z "${gotags}" ]]; then + flags+=" -tags ${gotags}" + fi + + ldflags="-s -w" + + if [[ "${goos}" == "linux" ]]; then + ldflags+=$" -extldflags '-lc -lrt -lpthread --static'" + fi + + ldflags+=" -X main.versionMajor=${major}" + ldflags+=" -X main.versionMinor=${minor}" + ldflags+=" -X main.versionPatch=${patch}" + ldflags+=" -X main.versionBuild=${build}" + if [[ "${is_prod_build}" != "true" ]]; then + ldflags+=" -X main.versionPrerelease=${pre}" + ldflags+=" -X main.defaultApiAddr=https://api.humanlog.dev" + ldflags+=" -X main.defaultBaseSiteAddr=https://humanlog.dev" + else + ldflags+=" -X main.defaultApiAddr=https://api.humanlog.io" + ldflags+=" -X main.defaultBaseSiteAddr=https://humanlog.io" + fi + + flags+=" -ldflags=\"${ldflags}\"" + + eval go build ${flags} ./cmd/humanlog + + cd ${output_dir} + tar -zcvf ${tarname} humanlog +} + +main diff --git a/script/publish.sh b/script/publish.sh new file mode 100755 index 00000000..6af3c2dc --- /dev/null +++ b/script/publish.sh @@ -0,0 +1,128 @@ +#!/usr/bin/env bash + +set -euox pipefail + +root=$(git rev-parse --show-toplevel) + +is_prod_build="${IS_PROD_BUILD:-}" +api_url="${API_URL:-"https://api.humanlog.io"}" +channel="${CHANNEL:-main}" +project="${PROJECT:-humanlog}" +bucket="${BUCKET:-humanlog-binaries}" +region="${BUCKET_REGION:-sfo3}" +endpoint="${BUCKET_ENDPOINT:-https://humanlog-binaries.sfo3.digitaloceanspaces.com}" +cdn_endpoint="${BUCKET_CDN_ENDPOINT:-https://humanlog-binaries.sfo3.cdn.digitaloceanspaces.com}" + +function main() { + local version=$(get_version | apictl version from-json) + local major=$(get_version | get_major) + local minor=$(get_version | get_minor) + local patch=$(get_version | get_patch) + local build=$(get_version | get_build) + local pre="" + if [[ "${is_prod_build}" != "true" ]]; then + pre=$(get_version | get_pre) + fi + + local bucketdir="${project}/${channel}/$(git_commit_timestamp)/$(git_short_commit)" + + for dir in $(find . -type d -name "humanlog-bins-*-*"); do + local os=$(echo "${dir}" | sed 's/\.\/humanlog-bins-\(.*\)-\(.*\)/\1/') + local arch=$(echo "${dir}" | sed 's/\.\/humanlog-bins-\(.*\)-\(.*\)/\2/') + + local filename="humanlog_${version}_${os}_${arch}.tar.gz" + local bucketpath="${bucketdir}/${filename}" + local download_url="${cdn_endpoint}/${bucket}/${bucketpath}" + local sha256sum=$(get_sha256sum ${dir}/${filename}) + + local extra_flags="--build ${build}" + if [[ "${pre}" != "" ]]; then + extra_flags+=" --pre ${pre}" + fi + + apictl --api.url ${api_url} create s3-artifact \ + --filepath ${dir}/${filename} \ + --s3.access_key $AWS_ACCESS_KEY_ID \ + --s3.secret_key $AWS_SECRET_ACCESS_KEY \ + --s3.endpoint ${endpoint} \ + --s3.region ${region} \ + --s3.bucket ${bucket} \ + --s3.use_path_style=true \ + --s3.directory ${bucketpath} + + apictl --api.url ${api_url} create version-artifact \ + --project ${project} \ + --major ${major} \ + --minor ${minor} \ + --patch ${patch} \ + --sha256 ${sha256sum} \ + --url ${download_url} \ + --os ${os} \ + --arch ${arch} \ + --sig "no-signature" ${extra_flags} + done + + apictl --api.url ${api_url} create published-version \ + --project ${project} \ + --channel ${channel} \ + --version ${version} +} + +function latest_git_tag() { + git describe --tags $(git rev-list --tags --max-count=1) +} + +function git_commit_timestamp() { + git show --no-patch --format=%ct +} + +function git_short_commit() { + git rev-parse --short HEAD +} + +function get_version() { + if [[ "${is_prod_build}" != "true" ]]; then + next_patch_version + else + current_version + fi +} + +function current_version() { + apictl version to-json --version $(latest_git_tag) | + apictl version math build set $(git_short_commit) +} + +function next_patch_version() { + current_version | + apictl version math patch add 1 | + apictl version math pre add next | + apictl version math pre add $(git_commit_timestamp) +} + +function get_major() { + jq -r '.major // 0' +} + +function get_minor() { + jq -r '.minor // 0' +} + +function get_patch() { + jq -r '.patch // 0' +} + +function get_pre() { + jq -r '.prereleases // [] | join(".")' +} + +function get_build() { + jq -r '.build // ""' +} + +function get_sha256sum() { + local filename=${1} + shasum -a 256 ${filename} | cut -d " " -f 1 +} + +main