GitHub Actions: mlugg/setup-zig

Hey everyone! I’ve spent a little time putting together a GitHub Action to use for creating Actions workflows utilizing the Zig compiler. You can find it here. The typical use case here is setting up CI for Zig projects.

Today, people often use a similar Action by goto-bus-stop. My new Action is an alternative which makes (in my opinion) a few improvements.

  • Zig is not fetched from ziglang.org, but rather from a third-party mirror. This decision was made to avoid frequent downloads from the official Zig website, which have a tendency to use up a lot of server resources, the funding for which the ZSF would prefer to utilize elsewhere.
  • After download, the tarball’s minisign signature is verified. This helps avoid potential issues in the event of an attacker gaining access to ziglang.org. (Of course, it is also a strict necessity for this Action, since third-party mirrors are not trusted!)
  • Mach nominated Zig versions can be directly referenced for projects which pin to those (for instance, probably any project based on Mach).
  • The global Zig cache is cached between runs. This avoids redundant work (e.g. building compiler-rt, running AstGen over the standard library) from happening on every CI run.
  • All local cache directories are redirected to the global cache. This makes the aforementioned cache preservation more useful still; the work done by the compiler would be equivalent to someone on the previous commit doing a git pull && zig build.
    • This will become more significant once incremental compilation is functional: all incremental compilation state for your project will be automatically saved between workflow runs, making simple changes to projects incredibly fast to test!

If you’re using (or intend to use) Zig in a GitHub Actions workflow, please do give this a try, and let me know of any issues you encounter!

Simple usage example:

jobs:
  test:
    runs-on: ubuntu-latest
    name: Build and Test
    steps:
      - uses: actions/checkout@v3
      - uses: mlugg/setup-zig@v1
      - run: zig build test
19 Likes

Is this cache invalidated when the Zig version changes or will the cache just grow over time indefinitely?

The cache is indeed tied to the Zig version. Additionally, GitHub Actions has a cache eviction policy: whenever the total size of caches in a repo exceeds 10GB, the oldest cache entries are wiped. So, if your cache gets super big, it’ll be evicted and you’ll start afresh.

I should note that unfortunately, Actions doesn’t have a great way to replace/delete an existing cache entry, so the entries corresponding to the Zig cache will build up after every workflow run. However, they’re not doing any harm aside from contributing to the quota: they’ll just get evicted once they’ve been unused a while.

3 Likes

Looking good so far, all it took was a one-line change for me: ci: Swap to mlugg/setup-zig · squeek502/resinator@e2a3bfe · GitHub

1 Like

We added my US west coast mirror today :slight_smile:
https://zigmirror.hryx.net

6 Likes

Thanks works fine in my project. Just recently wrote a small script to cache downloaded zig compiler for my CI, but your solution is a great improvement, especially .zig-cache caching.

@mlugg It would be nice to have option to only cache $ZIG_LOCAL_CACHE and $ZIG_GLOBAL_CACHE for scenarios where zig binary is already provided by something else.

No, I’m going to draw a line there. This already has more options than I intended. If you really want to cache things without downloading the compiler, do so manually with other actions.

2 Likes