Skip to content

Commit

Permalink
story(build): migrate to bazel modules (#85)
Browse files Browse the repository at this point in the history
* build(issue-84): migrated to bazel modules

* build(issue-84): updated github action
  • Loading branch information
Zaba505 authored May 16, 2024
1 parent c0cdd72 commit a52418f
Show file tree
Hide file tree
Showing 20 changed files with 3,407 additions and 1,067 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
common --enable_bzlmod

build --show_timestamps

test --test_output=errors
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.0
7.1.2
94 changes: 70 additions & 24 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,95 @@ name: CI

on:
push:
branches: [ "main" ]
branches:
- main
paths:
- '**.go'
- '**.bazel'
- '.bazelversion'
- '.bazelrc'
- 'go.mod'
- 'go.sum'
- 'MODULE.bazel'
- 'MODULE.bazel.lock'
pull_request:
branches: [ "main" ]
branches:
- main
paths:
- '**.go'
- '**.bazel'
- '.bazelversion'
- '.bazelrc'
- 'go.mod'
- 'go.sum'
- 'MODULE.bazel'
- 'MODULE.bazel.lock'

permissions:
contents: write
packages: write

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout source
uses: actions/checkout@v3

- name: Setup bazelisk
uses: bazelbuild/setup-bazelisk@v2

- name: Mount bazel cache
uses: actions/cache@v3
with:
path: "~/.cache/bazel"
key: bazel
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4

- name: Setup useful build info
- name: Generate build version
id: info
run: |
echo "::set-output name=date::$(date +'%Y.%m.%d')"
echo "::set-output name=time::$(date +'%H%M%S')"
echo "::set-output name=short_commit_id::$(git rev-parse --short HEAD)"
formatted_date=$(date +'%Y.%m.%d')
time=$(date +'%H%M%S')
short_commit_id=$(git rev-parse --short HEAD)
echo "build_version=${formatted_date}.${time}.${short_commit_id}" >> $GITHUB_OUTPUT
- name: Mount Bazel cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4
with:
path: |
~/.cache/bazel
key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'MODULE.bazel') }}
restore-keys: |
${{ runner.os }}-bazel-
- name: Fetch dependencies
run: bazel fetch //...

- name: Build
run: bazel build //...

- name: Test
run: bazel coverage //...
run: bazel test //...

- name: Tag repo with build version
run: |
tag=${{ steps.info.outputs.build_version }}
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git tag "${tag}"
git push origin --tags
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Deploy images
- name: Build and publish images
run: |
version="${{ steps.info.outputs.date }}.${{ steps.info.outputs.time }}.${{ steps.info.outputs.short_commit_id }}"
images=$(bazel query "kind(\"container_push\", //...)" --output=label)
bazel run --define "version=$version" $images
tarballTargets=$(bazel query "kind(oci_tarball, //...)")
while IFS=';' read -ra tarballs; do
for tarball in "${tarballs[@]}"; do
bazel build $tarball
tarFiles=$(bazel cquery --noshow_progress --ui_event_filters=-info,-stderr --output=files $tarball)
img=$(docker load -q --input $tarFiles)
imgWithTag=${img#"Loaded image: "}
imgAndTag=(${imgWithTag//:/ })
img=${imgAndTag[0]}
tag=${imgAndTag[1]}
docker tag $imgWithTag "$img:${{ steps.info.outputs.build_version }}"
docker push -a $img
done
done <<< "$tarballTargets"
14 changes: 0 additions & 14 deletions BUILD

This file was deleted.

5 changes: 5 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("@gazelle//:def.bzl", "gazelle")

# gazelle:proto disable_global
# gazelle:prefix github.com/z5labs/megamind
gazelle(name = "gazelle")
45 changes: 45 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module(name = "com_github_z5labs_megamind")

bazel_dep(name = "gazelle", version = "0.36.0")
bazel_dep(name = "rules_go", version = "0.47.1")
bazel_dep(name = "rules_oci", version = "1.7.5")
bazel_dep(name = "rules_pkg", version = "0.10.1")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.3")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.22.2")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")

# See https://github.com/bazelbuild/rules_go/issues/3625#issuecomment-1674275131
# go_deps.gazelle_override(
# path = "github.com/googleapis/gax-go/v2",
# directives = [
# "gazelle:proto disable",
# ]
# )

# All *direct* Go dependencies of the module have to be listed explicitly.
use_repo(
go_deps,
"com_github_gin_gonic_gin",
"com_github_spf13_cobra",
"com_github_spf13_viper",
"com_github_stretchr_testify",
"org_golang_google_grpc",
"org_golang_google_protobuf",
"org_golang_x_sync",
"org_uber_go_zap",
)

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "distroless_base",
image = "gcr.io/distroless/base",
digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
platforms = ["linux/amd64", "linux/arm64"],
)

use_repo(oci, "distroless_base")
Loading

0 comments on commit a52418f

Please sign in to comment.