Automate Compilation and Testing of Rust Code using GitHub Actions. (Part -2)

Table of contents
Reading Time: 2 minutes

Hello everyone, in a previous blog we discussed Github Actions and designed a basic workflow to Build rust program on Github. Now in this blog, we are going to do little advanced stuff. But I highly recommend you to visit my previous blog before going through this. 

Okay, So the objective of this blog is that we are going to create a CRON Job which will check build daily and if the build fails then it will create an issue on that Github repo.

So this how it’s going to look:-

name: Crown Job

on:
  schedule:
    - cron: '0 4 * * *'

env:
  CARGO_TERM_COLOR: always

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Install latest nightly
      uses: actions-rs/toolchain@v1
      with:
          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
    - name: Create issue using REST API
      if: failure()
      run: |
        curl --request POST \
        --url https://api.github.com/repos/${{ github.repository }}/issues \
        --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
        --header 'content-type: application/json' \
        --data '{
          "title": "Build failing",
          "body": "Build is failing. Please look into it."
          }'

Now let’s begin explaining important components one by one:-

on:
  schedule:
    - cron: '0 4 * * *'

Using key ‘on’ we set which event is going to trigger our workflow. As you can see under ‘on’ there is a ‘schedule’ where we can set ‘Cron Job’. This particular example will trigger workflow 4 AM daily. If you are not comfortable with the Cron Job expression then you can visit https://crontab.guru/. It’s very helpful to learn and understand how cron jobs work.

Now let’s move to the next component:-

- name: Create issue using REST API
  if: failure()
  run: |
    curl --request POST \
    --url https://api.github.com/repos/${{ github.repository }}/issues \
    --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
    --header 'content-type: application/json' \
    --data '{
      "title": "Build failing",
      "body": "Polkadex build is failing. Please look into it."
      }'

This is a job which will create an issue on Github. As you can see below the ‘name’ there is ‘if’ key which means it will only trigger that job if the given condition is met. In the given condition we gave ‘failure()’ so it will only trigger if the above job fails. In our case, I above rust build fails then it will create a new issue on Github by sending a POST request. 

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: https://bit.ly/2Vdlld7.

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

 

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading