Github workflows to do useful things with Rust
2020.02.05 Rust CIcontents
- Cargo check
- Cargo build
- Cargo clippy with annotations
- Cargo test
- Windows build
- OSX build
- Run coverage and upload to Coveralls
- Make a github release, and add Debian package asset
- Badges
It should be possible to copy any of the below workflows, commit them to a
project under .github/workflows/
; once pushed, Github actions should be
running to do various things with a Rust project, no other configuration
required.
Cargo check¶
Run cargo check
.
name: check on: [push, pull_request] jobs: check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable override: true - name: Run cargo-check run: | cargo install cargo-check cargo check --verbose
Cargo build¶
Run cargo build
.
name: build on: [push, pull_request] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Build run: cargo build --verbose
Cargo clippy with annotations¶
Run cargo clippy
to lint for possible improvements to your code. This treats
warnings as failure.
name: clippy on: [push, pull_request] jobs: clippy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: toolchain: stable components: clippy override: true - uses: actions-rs/clippy-check@v1 with: token: ${{ secrets.GITHUB_TOKEN }} args: --all-features -- -D warnings - name: Run clippy run: cargo clippy --all-features -- -D warnings
Cargo test¶
Run your test suite. This builds the tests, then runs them as a separate step, to make failures easier to find in the logs, without having to scroll through the verbose test build logs.
name: tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Build tests run: cargo test --verbose --no-run - name: Run tests run: cargo test --verbose --no-fail-fast -- --test-threads=1
Windows build¶
Build and test on windows.
name: windows on: [push, pull_request] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Build run: cargo build --verbose - name: Build tests run: cargo test --verbose --no-run - name: Run tests run: cargo test --verbose
OSX build¶
Build and test on OSX.
name: macOS on: [push, pull_request] jobs: build: runs-on: macos-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Build run: cargo build --verbose - name: Build tests run: cargo test --verbose --no-run - name: Run tests run: cargo test --verbose
Run coverage and upload to Coveralls¶
Get test coverage profiling results using grcov
, and upload to
coveralls.io.
{{PROJECT_NAME}}
must be substituted for your crate name, with underscores
rather than hyphens.
If you have a Coveralls account already linked to your Github account it will not even require you to create a new project, even if this project hasn't had coverage data uploaded for it before.
name: coverage on: [push, pull_request] jobs: coverage: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: fetch-depth: 0 - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: nightly override: true - name: Install grcov run: cargo install grcov - name: Run grcov env: PROJECT_NAME: "{{PROJECT_NAME}}" RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zno-landing-pads" CARGO_INCREMENTAL: 0 run: | cargo build --verbose cargo test --verbose zip -0 cov.zip $(find . -name "$PROJECT_NAME*.gc*" -print) grcov cov.zip -s . -t lcov --llvm --ignore-not-existing --ignore "/*" -o lcov.info - name: Push grcov results to Coveralls via GitHub Action uses: coverallsapp/github-action@v1.0.1 with: github-token: ${{ secrets.GITHUB_TOKEN }} path-to-lcov: "lcov.info"
Make a github release, and add Debian package asset¶
This job will run when you push a tag to the repository called, for example, "v0.4.2", it will then build, test and make releases.
Make a (draft) release on Github, build a Debian package using cargo deb and push the package as an asset to the release.
Additionally, publish the release to the Crates.io package registry.
{{PROJECT_NAME}}
must be substituted for your crate name (not replacing
hyphens with underscores).
name: release on: push: tags: - 'v[0-9]+.[0-9]+.[0-9]+' jobs: release: name: release runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 with: fetch-depth: 0 - name: Get version id: get_version run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v} - name: Build and test run: | cargo test --verbose cargo clippy --verbose --all-features -- -D warnings cargo build --verbose --release - name: Build Debian package run: | cargo install cargo-deb cargo deb - name: Create Github release id: make_release uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: ${{ github.ref }} release_name: ${{ github.ref }} draft: true prerelease: false - name: Upload assets to Github uses: actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PROJECT_NAME: {{PROJECT_NAME}} with: upload_url: ${{ steps.make_release.outputs.upload_url }} asset_content_type: application/vnd.debian.binary-package asset_path: target/debian/${PROJECT_NAME}_${{ steps.get_version.outputs.VERSION }}_amd64.deb asset_name: ${PROJECT_NAME}_${{ steps.get_version.outputs.VERSION }}_amd64.deb - name: Publish to crates.io env: CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }} run: | cargo login ${CRATES_IO_TOKEN} cargo publish
Badges¶
Add the following to your readme, substituting {{NAME}}
for your project
name, and {{USER}}
for the appropriate Github user name under which the
project is hosted. After doing this you will have badges for all of the above
workflows.
[![build](https://github.com/{{USER}}/{{NAME}}/workflows/build/badge.svg)](https://github.com/{{USER}}/{{NAME}}/actions?query=branch%3Amaster+event%3Apush+workflow%3Abuild)
[![test](https://github.com/{{USER}}/{{NAME}}/workflows/tests/badge.svg)](https://github.com/{{USER}}/{{NAME}}/actions?query=branch%3Amaster+event%3Apush+workflow%3Atests)
[![clippy](https://github.com/{{USER}}/{{NAME}}/workflows/clippy/badge.svg)](https://github.com/{{USER}}/{{NAME}}/actions?query=branch%3Amaster+event%3Apush+workflow%3Aclippy)
[![macOS](https://github.com/{{USER}}/{{NAME}}/workflows/macOS/badge.svg)](https://github.com/{{USER}}/{{NAME}}/actions?query=branch%3Amaster+event%3Apush+workflow%3AmacOS)
[![windows](https://github.com/{{USER}}/{{NAME}}/workflows/windows/badge.svg)](https://github.com/{{USER}}/{{NAME}}/actions?query=branch%3Amaster+event%3Apush+workflow%3Awindows)
[![coveralls](https://img.shields.io/coveralls/github/{{USER}}/{{NAME}}/master)](https://coveralls.io/github/{{USER}}/{{NAME}}?branch=master)
[![crates.io](https://img.shields.io/crates/v/{{NAME}})](https://crates.io/crates/{{NAME}})