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

ci: re-align with bazel-contrib/rules-template #129

Merged
merged 5 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# Bazel settings that apply to this repository.
# Take care to document any settings that you expect users to apply.
# Settings that apply only to CI are in .github/workflows/ci.bazelrc

# Required until this is the default; expected in Bazel 7
common --enable_bzlmod

# Don’t want to push a rules author to update their deps if not needed.
# https://bazel.build/reference/command-line-reference#flag--check_direct_dependencies
# https://bazelbuild.slack.com/archives/C014RARENH0/p1691158021917459?thread_ts=1691156601.420349&cid=C014RARENH0
common --check_direct_dependencies=off

# Load any settings specific to the current user.
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
# This needs to be last statement in this
# config, as the user configuration should be able to overwrite flags from this file.
# See https://docs.bazel.build/versions/master/best-practices.html#bazelrc
# (Note that we use .bazelrc.user so the file appears next to .bazelrc in directory listing,
# rather than user.bazelrc as suggested in the Bazel docs)
try-import %workspace%/.bazelrc.user

# Load any settings specific to ci pipelines.
try-import %workspace%/.bazelrc.ci
15 changes: 10 additions & 5 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# In code review, collapse generated files
docs/*.md linguist-generated=true

#################################
# Configuration for 'git archive'
# see https://git-scm.com/docs/git-archive/2.40.0#ATTRIBUTES
# Don't include examples in the distribution artifact, just to reduce size
.github/ export-ignore
examples/ export-ignore
tests/smoke_workspace/ export-ignore
# See https://git-scm.com/docs/git-archive#ATTRIBUTES

# Don't include examples in the distribution artifact, to reduce size.
# You may want to add additional exclusions for folders or files that users don't need.
examples export-ignore
.github export-ignore
8 changes: 8 additions & 0 deletions .github/workflows/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@buildifier_prebuilt//:rules.bzl", "buildifier")

buildifier(
name = "buildifier.check",
exclude_patterns = ["./.git/*"],
lint_mode = "warn",
mode = "diff",
)
178 changes: 178 additions & 0 deletions .github/workflows/bazel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# This is a modified version of upstream.
# see: https://github.com/bazel-contrib/.github/issues/21
#
# Reusable workflow that can be referenced by repositories in their .github/workflows/ci.yaml.
# See example usage in https://github.com/bazel-contrib/rules-template/blob/main/.github/workflows/ci.yaml
#
# This assumes the repo calling the workflow has at least these files:
# - .github/workflows/ci.bazelrc
# - .bazelrc
#
# This workflow uses https://github.com/bazel-contrib/setup-bazel to prepare the cache folders.
# Caching may be disabled by setting `mount_bazel_caches` to false.

on:
# Make this workflow reusable, see
# https://github.blog/2022-02-10-using-reusable-workflows-github-actions
workflow_call:
inputs:
folders:
required: true
# JSON is needed because list is not supported:
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_callinputsinput_idtype
description: |
JSON-formatted array of folders to run 'bazel test' in.
For example, '[".", "e2e/smoke"]'
type: string
bazelversions:
description: |
JSON-formatted array of bazelversion to run 'bazel test' for.

The version from .bazelversion is used unless this option is set.

For example, '["6.5.0", "7.4.1", "8.0.0rc1"]'
type: string
default: "[]"
exclude:
description: |
JSON-formatted array of exclusions to the generated matrix of tests.

By default, we don't test non-linux with Bazel 8 to minimize macOS and Windows minutes
since they are billed at 10X and 2X respectively:
https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#included-storage-and-minutes

Note: independent of this setting, we don't create MacOS/Windows matrix entries for pull requests
unless they come from a specially-named branch. See logic below.
type: string
default: |
[
{"bazelversion": "8.0.0rc1", "os": "macos-latest"},
{"bazelversion": "8.0.0rc1", "os": "windows-latest"}
]
exclude_windows:
description: Don't run any tests on Windows
type: boolean
bazel_test_command:
default: "bazel test //..."
description: |
Bazel test command that may be overridden to set custom flags and targets.
The `--enable_bzlmod={true,false}`, `--disk_cache=~/.cache/bazel-disk-cache`,
and `--repository_cache=~/.cache/bazel-repository-cache` flags are
automatically appended to the command.
type: string
mount_bazel_caches:
default: true
description: |
Whether to enable caching in the bazel-contrib/setup-bazel action.
type: boolean

jobs:
# matrix-prep-* steps generate JSON used to create a dynamic actions matrix.
# Inspired from
# https://stackoverflow.com/questions/65384420/how-to-make-a-github-action-matrix-element-conditional

matrix-prep-os:
# Prepares the 'os' axis of the test matrix, to reduce costs since GitHub hosted runners cost more on some platforms.
# https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions#included-storage-and-minutes
runs-on: ubuntu-latest
steps:
- id: linux
run: echo "os=ubuntu-latest" >> $GITHUB_OUTPUT
- id: windows
run: echo "os=windows-latest" >> $GITHUB_OUTPUT
# Only run on main branch (or PR branches that contain 'windows') to minimize Windows minutes (billed at 2X)
if: (github.ref == 'refs/heads/main' || contains(github.head_ref, 'windows')) && !inputs.exclude_windows
- id: macos
run: echo "os=macos-latest" >> $GITHUB_OUTPUT
# Only run on main branch (or PR branches that contain 'macos') to minimize macOS minutes (billed at 10X)
if: github.ref == 'refs/heads/main' || contains(github.head_ref, 'macos')
outputs:
# Will look like ["ubuntu-latest", "windows-latest", "macos-latest"]
os: ${{ toJSON(steps.*.outputs.os) }}

matrix-prep-bazelversion:
# Prepares the 'bazelversion' axis of the test matrix
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# NB: we assume this is Bazel 7
- id: bazel_from_bazelversion
if: inputs.bazelversions == '[]'
run: echo "bazelversion=$(head -n 1 .bazelversion)" >> $GITHUB_OUTPUT
outputs:
# Will look like ["<version from .bazelversion>"]
bazelversions: ${{ toJSON(steps.*.outputs.bazelversion) }}

test:
# The type of runner that the job will run on
runs-on: ${{ matrix.os }}

needs:
- matrix-prep-bazelversion
- matrix-prep-os

# Run bazel test in each workspace with each version of Bazel supported
strategy:
fail-fast: false
matrix:
os: ${{ fromJSON(needs.matrix-prep-os.outputs.os) }}
bazelversion:
[
"${{ fromJSON(needs.matrix-prep-bazelversion.outputs.bazelversions) }}",
"${{ fromJSON(inputs.bazelversions) }}",
]
folder: ${{ fromJSON(inputs.folders) }}
bzlmodEnabled: [true, false]
exclude: ${{ fromJSON(inputs.exclude) }}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4

- uses: bazel-contrib/[email protected]
with:
repository-cache: ${{ inputs.mount_bazel_caches }}
bazelrc: |
common --announce_rc
common --color=yes
common --enable_bzlmod=${{ matrix.bzlmodEnabled }}

- name: Configure bazelrc --enable_workspace (bazelversion >= 7.1.0)
working-directory: ${{ matrix.folder }}
if: ${{ !startsWith(matrix.bazelversion, '6.') && !startsWith(matrix.bazelversion, '7.0.') }}
run: |
echo "common --enable_workspace=${{ ! matrix.bzlmodEnabled }}" >> .bazelrc

- name: Configure Bazel version
working-directory: ${{ matrix.folder }}
run: |
echo "${{ matrix.bazelversion }}" > .bazelversion
bazel version

- name: Check for test.sh
# Checks for the existence of test.sh in the folder. Downstream steps can use
# steps.has_test_sh.outputs.files_exists as a conditional.
id: has_test_sh
uses: andstor/file-existence-action@v3
with:
files: "${{ matrix.folder }}/test.sh"

# See https://github.com/bazel-contrib/publish-to-bcr#including-patches
- name: verify bcr patches
if: matrix.bzlmodEnabled && hashFiles('.bcr/patches/*.patch') != '' && ! startsWith(matrix.os, 'windows')
run: patch --dry-run -p1 < .bcr/patches/*.patch

- name: Test
working-directory: ${{ matrix.folder }}
run: ${{ inputs.bazel_test_command }}

- name: Run ./test.sh
# Run if there is a test.sh file in the folder
# Don't run integration tests on Windows since they are bash scripts and Windows runs Powershell
if: steps.has_test_sh.outputs.files_exists == 'true' && ! startsWith(matrix.os, 'windows')
working-directory: ${{ matrix.folder }}
shell: bash
# Run the script potentially setting BZLMOD_FLAG=--enable_bzlmod. All test.sh
# scripts that run bazel directly should make use of this variable.
run: BZLMOD_FLAG=--enable_bzlmod=${{ matrix.bzlmodEnabled }} ./test.sh
19 changes: 19 additions & 0 deletions .github/workflows/buildifier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Buildifier

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [main]
pull_request:
branches: [main]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: buildifier
run: bazel run --enable_bzlmod //.github/workflows:buildifier.check
4 changes: 3 additions & 1 deletion .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# Debug where options came from
build --announce_rc
# This directory is configured in GitHub actions to be persisted between runs.
# We do not enable the repository cache to cache downloaded external artifacts
# as these are generally faster to download again than to fetch them from the
# GitHub actions cache.
build --disk_cache=~/.cache/bazel
build --repository_cache=~/.cache/bazel-repo
# Don't rely on test logs being easily accessible from the test runner,
# though it makes the log noisier.
test --test_output=errors
Expand Down
Loading
Loading