Skip to content

Commit

Permalink
GHA workflow updates
Browse files Browse the repository at this point in the history
- Build from a matrix of platform-arch pairs
- Add helper function to automate cross-compilation
- Add PLATFORMS to platforms.bzl identifying what to build for
- Create a new release on tags
  • Loading branch information
imeyer committed Aug 25, 2024
1 parent d15800b commit 280c1ec
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build & Release
run-name: Build & Release
on: push

jobs:
build:
strategy:
matrix:
goos: [linux, darwin]
goarch: [arm64, amd64]
runs-on: ${{ matrix.goos }}
steps:
- name: Check out smart-dns-updater
uses: actions/checkout@v4
- run: |
bazelisk build //:smart-dns-updater_${{ matrix.goos }}_${{ matrix.goarch }}
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: smart-dns-updater_${{ matrix.goos }}_${{ matrix.goarch }}
path: bazel-bin/smart-dns-updater_${{ matrix.goos }}_${{ matrix.goarch }}_/smart-dns-updater_${{ matrix.goos }}_${{ matrix.goarch }}
if-no-files-found: error

release:
runs-on: ubuntu-latest
needs: build
steps:
- name: Release
uses: softprops/action-gh-release@v2
# Only release on tags
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
bazel-bin/smart-dns-updater_*_/smart-dns-updater_*
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ name: Test
run-name: Test
on:
push:
branches: [main]
pull_request:
paths:
- "!README.md"
- "!LICENSE"

jobs:
Test:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Check out smart-dns-updater
Expand Down
26 changes: 16 additions & 10 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
load("@gazelle//:def.bzl", "gazelle")
load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@rules_go//go:def.bzl", "go_library", "go_test")
load("helper.bzl", "cross_compile_binary")
load("platforms.bzl", "PLATFORMS")

# Name of the binary
NAME = "smart-dns-updater"

# gazelle:prefix github.com/imeyer/smart-dns-updater
gazelle(name = "gazelle")

go_library(
name = "smart-dns-updater_lib",
name = "{}_lib".format(NAME),
srcs = ["main.go"],
importpath = "github.com/imeyer/smart-dns-updater",
importpath = "github.com/imeyer/{}".format(NAME),
visibility = ["//visibility:private"],
x_defs = {
"version": "{STABLE_VERSION}",
Expand All @@ -22,17 +27,18 @@ go_library(
],
)

go_binary(
name = "smart-dns-updater",
embed = [":smart-dns-updater_lib"],
visibility = ["//visibility:public"],
)
# Create go_binary targets for each platform-arch combination
[cross_compile_binary(
name = NAME,
goarch = goarch,
goos = goos,
) for (goos, goarch) in PLATFORMS]

go_test(
name = "smart-dns-updater_test",
name = "{}_test".format(NAME),
size = "small",
srcs = ["main_test.go"],
embed = [":smart-dns-updater_lib"],
embed = [":{}_lib".format(NAME)],
deps = [
"@com_github_miekg_dns//:dns",
"@com_github_stretchr_testify//assert",
Expand Down
13 changes: 13 additions & 0 deletions helper.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Helper module to create binaries per PLATFORM-ARCH combination"""

load("@rules_go//go:def.bzl", "go_binary")

# Function to create a go_binary target for each platform-arch combination
def cross_compile_binary(name, goos, goarch):
go_binary(
name = "{}_{}_{}".format(name, goos, goarch),
embed = [":{}_lib".format(name)],
goarch = goarch,
goos = goos,
visibility = ["//visibility:public"],
)
9 changes: 9 additions & 0 deletions platforms.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Define platform, arch pairs to build"""

# Define the matrix of platforms and architectures
PLATFORMS = [
("linux", "amd64"),
("linux", "arm64"),
("darwin", "amd64"),
("darwin", "arm64"),
]

0 comments on commit 280c1ec

Please sign in to comment.