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.
Note that goto-bus-stop’s action does perform caching on the tarball between runs. My action also does this. However, there are some restrictions on GitHub’s caching system which mean re-fetches are sometimes inevitable.
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
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.
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.
Just wanted to say that I am moving standalone kubkon/zld repo to @mlugg’s GitHub action, and I am extremely happy with it. For context, I had only one requirement - download native tarball for the host architecture in question. With goto-bus-stop/setup-zig all this time on arm64 macOS CI image I was running zig via rosetta which only manifested itself very recently when I started experiencing difficult to explain and frankly unexpected deadlocks (for those who don’t follow my work, I have rewritten MachO linker to utiilise the threadpool recently). Anyhow, to provide an evidence for this, lo and behold:
Also just wanted to say that I’m using mlugg/setup-zig@v1 in cricket.zig to run tests and build examples on Linux, macOS and Windows and it’s working really well.
My Action isn’t official in any way. I’d encourage zigup and similar projects to introduce mirror support, and would help them if necessary (e.g. moving the mirrors.json data to a separate location so they can use it), but there’s nothing official here, and there doesn’t need to be.