Cross-Compiling and Uploading Binaries on Release with GitHub Actions and cross
TL;DR
A common use case for Rust is creating command-line tools. One of the advantages of using Rust for CLI tools is that you can distribute a single binary, making it easy to use.
Since the source code will be managed on GitHub anyway, it's convenient to automatically distribute the latest binaries with each release. And it's even more convenient to automatically cross-compile for multiple platforms, so I'll explain how to do that.
Cross Build
We'll use cross, a package that performs cross builds using Docker. By using actions-rs/cargo for Rust GitHub Actions, you can easily perform cross builds in GitHub Actions.
The usage is very simple -- just set use-cross to true in the with section when using actions-rs/cargo@v1.
Release and Upload
While there are actions for creating releases and uploading, they tend to get quite long with uploading binaries as artifacts and such, so we'll use Upload files to a GitHub release. The official sample looks like this, making it very easy to create a release and upload binaries.
simple_example.yml
Release.yml
The final YAML combining cross build with release and upload is shown below. We use a matrix to build for multiple platforms. We specify both musl and gnu because there's a critical issue where binaries won't run on older CentOS without musl. Normally, you'd also want to include Windows and similar targets.
When including Windows, you can build normally on Ubuntu. The only thing to watch out for is that the output file needs a .exe extension.
Release.yml