Automate Compilation and Testing of Rust Code using GitHub Actions

Table of contents
Reading Time: 2 minutes

DevOps became a crucial part of Software development in the past few years. And there are a lot of tools present out there to practice DevOps methodology.  In this blog, I am going to talk about Git hubs Actions which emerged as one of the popular tools to achieve automation in your project. So in this blog, I am going to create a simple workflow which will build and test our code as soon as code is pushed or PR is raised.  

So let’s get started …

For writing Github action we use YAML/YML if you are not aware of it I would suggest you watch this video first then move to this blog.

Okay, so to create a new workflow first create a new folder in your GitHub repository name {.github} under that folder create folder {workflows}. Under this folder, we store all of our workflows. 

For this blog, let’s create the first workflow name :[ simple_build.yml ]

Inside that file paste following code:-

name: Basic-Workflow
branches: [ master, develop ]
branches: [ master, develop ]

Here, the name signifies the name of our workflow and Simple-Build is value. Now let’s move to the next line which is ‘on’ it means what all events are going to trigger our workflow right now its ‘push’ and ‘pull_request’ which stores values branches. Which means whenever there is going to be new push or PR is raised on following branches which in our case is ‘master’ and ‘develop’, will trigger the workflow.

Now, let’s move to the second part of our code:-

runs-on: ubuntu-latest

- uses: actions/checkout@v2
- name: Install latest nightly
  uses: actions-rs/toolchain@v1
      toolchain: nightly-2020-09-28
      target: wasm32-unknown-unknown
      override: true
      components: rustfmt, clippy
- name: Build
  run: cargo build --verbose
- name: Run tests
  run: cargo test --verbose

It contains all jobs which the following workflow is going to perform. So first we provide that where we are going to perform build under ‘runs_on’ in our case it’s ‘ubuntu-latest’ though you can select different versions of OS depending on the requirement. After that, we start writing about different jobs. As you can see our first job is going to Checkout to the current folder. After that, we are installing Rust Toolchain with which we provided arguments using ‘with’ like toolchain target and all. After that, we performed to build and run our test cases.

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter:

Happy learning!!!

This image has an empty alt attribute; its file name is screenshot-from-2020-06-08-11-00-35.png

 This image has an empty alt attribute; its file name is footer-2.jpg