Skip to content

Commit a52418f

Browse files
authored
story(build): migrate to bazel modules (#85)
* build(issue-84): migrated to bazel modules * build(issue-84): updated github action
1 parent c0cdd72 commit a52418f

20 files changed

+3407
-1067
lines changed

.bazelrc

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
common --enable_bzlmod
2+
13
build --show_timestamps
24

35
test --test_output=errors

.bazelversion

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.3.0
1+
7.1.2

.github/workflows/main.yml

+70-24
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,95 @@ name: CI
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches:
6+
- main
7+
paths:
8+
- '**.go'
9+
- '**.bazel'
10+
- '.bazelversion'
11+
- '.bazelrc'
12+
- 'go.mod'
13+
- 'go.sum'
14+
- 'MODULE.bazel'
15+
- 'MODULE.bazel.lock'
616
pull_request:
7-
branches: [ "main" ]
17+
branches:
18+
- main
19+
paths:
20+
- '**.go'
21+
- '**.bazel'
22+
- '.bazelversion'
23+
- '.bazelrc'
24+
- 'go.mod'
25+
- 'go.sum'
26+
- 'MODULE.bazel'
27+
- 'MODULE.bazel.lock'
28+
29+
permissions:
30+
contents: write
31+
packages: write
832

933
jobs:
1034
build:
1135
runs-on: ubuntu-latest
12-
1336
steps:
14-
- name: Checkout source
15-
uses: actions/checkout@v3
16-
17-
- name: Setup bazelisk
18-
uses: bazelbuild/setup-bazelisk@v2
19-
20-
- name: Mount bazel cache
21-
uses: actions/cache@v3
22-
with:
23-
path: "~/.cache/bazel"
24-
key: bazel
37+
- name: Checkout
38+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
2539

26-
- name: Setup useful build info
40+
- name: Generate build version
2741
id: info
2842
run: |
29-
echo "::set-output name=date::$(date +'%Y.%m.%d')"
30-
echo "::set-output name=time::$(date +'%H%M%S')"
31-
echo "::set-output name=short_commit_id::$(git rev-parse --short HEAD)"
43+
formatted_date=$(date +'%Y.%m.%d')
44+
time=$(date +'%H%M%S')
45+
short_commit_id=$(git rev-parse --short HEAD)
46+
echo "build_version=${formatted_date}.${time}.${short_commit_id}" >> $GITHUB_OUTPUT
47+
48+
- name: Mount Bazel cache
49+
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4
50+
with:
51+
path: |
52+
~/.cache/bazel
53+
key: ${{ runner.os }}-bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'MODULE.bazel') }}
54+
restore-keys: |
55+
${{ runner.os }}-bazel-
56+
57+
- name: Fetch dependencies
58+
run: bazel fetch //...
3259

3360
- name: Build
3461
run: bazel build //...
3562

3663
- name: Test
37-
run: bazel coverage //...
64+
run: bazel test //...
65+
66+
- name: Tag repo with build version
67+
run: |
68+
tag=${{ steps.info.outputs.build_version }}
69+
git config user.name "${GITHUB_ACTOR}"
70+
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
71+
git tag "${tag}"
72+
git push origin --tags
3873
3974
- name: Login to GitHub Container Registry
40-
uses: docker/login-action@v2
75+
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3
4176
with:
4277
registry: ghcr.io
4378
username: ${{ github.actor }}
4479
password: ${{ secrets.GITHUB_TOKEN }}
4580

46-
- name: Deploy images
81+
- name: Build and publish images
4782
run: |
48-
version="${{ steps.info.outputs.date }}.${{ steps.info.outputs.time }}.${{ steps.info.outputs.short_commit_id }}"
49-
images=$(bazel query "kind(\"container_push\", //...)" --output=label)
50-
bazel run --define "version=$version" $images
83+
tarballTargets=$(bazel query "kind(oci_tarball, //...)")
84+
while IFS=';' read -ra tarballs; do
85+
for tarball in "${tarballs[@]}"; do
86+
bazel build $tarball
87+
tarFiles=$(bazel cquery --noshow_progress --ui_event_filters=-info,-stderr --output=files $tarball)
88+
img=$(docker load -q --input $tarFiles)
89+
imgWithTag=${img#"Loaded image: "}
90+
imgAndTag=(${imgWithTag//:/ })
91+
img=${imgAndTag[0]}
92+
tag=${imgAndTag[1]}
93+
docker tag $imgWithTag "$img:${{ steps.info.outputs.build_version }}"
94+
docker push -a $img
95+
done
96+
done <<< "$tarballTargets"

BUILD

-14
This file was deleted.

BUILD.bazel

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load("@gazelle//:def.bzl", "gazelle")
2+
3+
# gazelle:proto disable_global
4+
# gazelle:prefix github.com/z5labs/megamind
5+
gazelle(name = "gazelle")

MODULE.bazel

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module(name = "com_github_z5labs_megamind")
2+
3+
bazel_dep(name = "gazelle", version = "0.36.0")
4+
bazel_dep(name = "rules_go", version = "0.47.1")
5+
bazel_dep(name = "rules_oci", version = "1.7.5")
6+
bazel_dep(name = "rules_pkg", version = "0.10.1")
7+
bazel_dep(name = "aspect_bazel_lib", version = "2.7.3")
8+
9+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
10+
go_sdk.download(version = "1.22.2")
11+
12+
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
13+
go_deps.from_file(go_mod = "//:go.mod")
14+
15+
# See https://github.com/bazelbuild/rules_go/issues/3625#issuecomment-1674275131
16+
# go_deps.gazelle_override(
17+
# path = "github.com/googleapis/gax-go/v2",
18+
# directives = [
19+
# "gazelle:proto disable",
20+
# ]
21+
# )
22+
23+
# All *direct* Go dependencies of the module have to be listed explicitly.
24+
use_repo(
25+
go_deps,
26+
"com_github_gin_gonic_gin",
27+
"com_github_spf13_cobra",
28+
"com_github_spf13_viper",
29+
"com_github_stretchr_testify",
30+
"org_golang_google_grpc",
31+
"org_golang_google_protobuf",
32+
"org_golang_x_sync",
33+
"org_uber_go_zap",
34+
)
35+
36+
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
37+
38+
oci.pull(
39+
name = "distroless_base",
40+
image = "gcr.io/distroless/base",
41+
digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
42+
platforms = ["linux/amd64", "linux/arm64"],
43+
)
44+
45+
use_repo(oci, "distroless_base")

0 commit comments

Comments
 (0)