• Testing Bazel with Github Actions

  • Testing Bazel with Github Actions

    In 2019, github has made it incredibly easy to run continuous integration (CI) on public repositories, using Github Actions.

    Thanks to a simple .yml configuration file, any open-source project can be enhanced with a very capable, Github-hosted, CI server.

    Lately I have been working a lot with Bazel, to ensure that my projects can obtain fully reproducible binaries across different platforms.

    Let’s add some CI testing to one of these project!

    Adding CI to a Bazel project

    As an example, I’ll use the Github repository from one of my previous posts on how to set up an embedded toolchain for Bazel.

    Use bazelisk:

    Testing this particular project is very simple. We just need to make sure that the command bazel build //project completes successfully on our CI servers.

    However, bazel has many different versions and some are incompatible with others. We could provision the CI server to be provisioned with the right version, but that would be tedious.

    Lucky for us, bazelisk is exactly what we need. Not only it comes pre-installed on every Github Actions container, but it can also check our project for a .bazelversion file and use the right version of Bazel automatically!

    Say that our project used Bazel version 3.1.0, we would just add the following .bazelversion:

    1
    2
    3
    4
    5
    
    3.1.0
    
    # This .bazelversion file will allow bazelisk 
    # to download the appropriate version of Bazel
    # automatically.
    

    Github Actions:

    Now for the meat of the project. We want to run the following command on our CI servers:

    bazelisk build //project

    And we want it to work across different operating systems too!

    Traditionally we would have had to spin up different virtual machines, and provision them… A project in and of itself.

    But wait! Github Actions provides an amazingly simple solution that allow us to run a multi platform command like bazelisk on different operating systems by using its matrix strategy.

    Let’s add this build.yml to our .github/workflows folder:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    # .github/workflows/build.yml
    
    name: Build
    
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    
    jobs:
      
      test:
        name: build on ${{ matrix.os }}
        runs-on: ${{ matrix.os }}
        strategy:
          matrix:
            os: [ubuntu-latest, windows-latest, macos-latest]
    
        steps:
          - uses: actions/checkout@v2
          - name: build project
            run: bazelisk build project
    
    

    And we are done! Pretty incredible huh?

    Conclusions

    Using Github Actions is a simple and intuitive way of adding CI to any open source project. Definitely worth trying when working with Bazel!

    Comments

    comments powered by Disqus