Table of contents
Introduction
In this tutorial, we will look at how to compile your Rust source code to binaries that run on the Windows, Linux, and macOS operating systems.
Note: This setup requires having a GitHub Account [↗] and assumes you already manage your project with GIT [↗].
We will be using Githubs Actions [↗] to build our source code into binaries for the Windows x86_64, Linux x86_64, and macOS architecture.
Setting up Github Action
- First, you need to have your code in a GitHub repository [↗]
- Next create a hidden GitHub directory i.e.:
.githubwithin the project source folder. Note: The.in front of github is what makes it hidden - Within the
.githubdirectory create a folder namedworkflows. - Now in the newly created
workflowsfolder, add a YAML file with the namerelease.yml
Your directory structure should look similar to this:
01: .
02: ├── .github
03: │ └── workflows
04: │ └── release.yml
05: └── srcCopy and paste the following script into the release.yml YAML [↗] file.
Build Script:
01: # .github/workflows/release.yml
02: on:
03: release:
04: types: [created]
05:
06: jobs:
07: release:
08: name: release ${{ matrix.target }}
09: runs-on: ubuntu-latest
10: strategy:
11: fail-fast: false
12: matrix:
13: target: [x86_64-pc-windows-gnu, x86_64-unknown-linux-musl, x86_64-apple-darwin]
14: steps:
15: - uses: actions/checkout@master
16: - name: Compile and release
17: uses: rust-build/rust-build.action@v1.4.3
18: env:
19: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20: with:
21: RUSTTARGET: ${{ matrix.target }}Go ahead, commit and push the updated source code to GitHub.
Permissions
Pushing the source code over to GitHub will not trigger our build script, this is because it is set to run only when you create a release [↗].
When a release is created our script will then run, generating the binaries and adding them to the final release assets. From there you can download them.
We need to grant our script read and write access so it can save the binaries on release.
Follow the steps below to update the permissions for the script:
![GitHub Repository permissions [→]](../images/go-to-settings.png)
First, head over to the Settings section of your repository (As shown above).
![GitHub Repository permissions [→]](../images/actions-click.png)
From the left hand-nav, collapse the Actions menu and select the General option.
Make sure the permissions match the selection shown below:
Workflow permissions
![GitHub Workflow permissions [→]](../images/permissions-2.png)
Actions permissions
![GitHub Action permissions [→]](../images/action-permission.png)
Build process
Now we can go ahead and create our release. Head back to the home page of your repository and click on the "Release" hyper-link as shown below:
![GitHub release [→]](../images/release-1.png)
Click on the "Draft a new release" button above.
![Drafting a new GitHub Release| GitHub [→]](../images/release-2.png)
Next, follow the steps shown in the image below to publish a release
- Choose or create a tag.
- If you don't have an existing tag, type out a name and click on the "Create new tag:.." button to create one.
- Describe your release.
- Click the "Publish release" button to publish your release.
![GitHub Release process [→]](../images/release-3.png)
This will trigger a build process. This will take some time, once done your release page should now look like the image shown below with your zipped binaries.
![Zipped Binaries [→]](../images/zipped-binaries.png)
Conclusion
This tutorial is based on a pre-packaged Action script [↗] from the GitHub marketplace.
Here is another article you might like 😊 GIT: List Files With Merge Conflicts