Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instructions for building and testing with Rust #35725

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: There's a style guide for UI highlights — does the orange rectangle follow the guidelines for consistency?

TODO: Size/dimensions of the image, is it expected to follow some resolution pattern from other pages for legibility?

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: Building and testing Rust
intro: You can create a continuous integration (CI) workflow to build and test your Rust project.
versions:
ghec: '*'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: check for versions applicability.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also the reason you don't "see" it in the overview page — cuz the default would be fpt, and since you don't spec that, you can only see it if you switch the overview to ghec — otherwise it won't get listed there.

type: tutorial
topics:
- CI
shortTitle: Build, test & Publish with Rust
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are three places, title, intro, shortTitle, that either mention publishing, or don't. Consistency?

NB: Correct sentence case?

---
<!-- {% data reusables.actions.enterprise-github-hosted-runners %}-->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: check for versions applicability.

And either include the reusable if relevant; or don't if not needed anymore.


## Introduction

This guide shows you how to build, test, and publish a Rust package.
foursixnine marked this conversation as resolved.
Show resolved Hide resolved

{% data variables.product.prodname_dotcom %}-hosted runners have a tools cache with preinstalled software, which includes the dependencies for Rust. For a full list of up-to-date software and the preinstalled versions of Rust, see [AUTOTITLE](/actions/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software).

## Prerequisites

You should already be familiar with YAML syntax and how it's used with {% data variables.product.prodname_actions %}. For more information, see [AUTOTITLE](/actions/using-workflows/workflow-syntax-for-github-actions).

We recommend that you have a basic understanding of the Rust language. For more information, see [Getting started with Rust](https://www.rust-lang.org/learn).

## Using a Rust workflow template

{% data reusables.actions.workflow-templates-get-started %}

{% data variables.product.prodname_dotcom %} provides a Rust workflow template that should work for most basic Rust projects. The subsequent sections of this guide give examples of how you can customize this workflow template.
foursixnine marked this conversation as resolved.
Show resolved Hide resolved

{% data reusables.repositories.navigate-to-repo %}
{% data reusables.repositories.actions-tab %}
{% data reusables.actions.new-starter-workflow %}
1. The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "Rust".
1. Filter the selection of workflows by clicking **Continuous integration**.
1. On the "Rust - by {% data variables.product.prodname_actions %}" workflow, click **Configure**.

![Screenshot of the "Choose a workflow" page. The "Configure" button on the "Rust" workflow is highlighted with an orange outline.](/assets/images/help/actions/starter-workflow-rust.png)
1. Edit the workflow as required. For example, change the version of Rust.
1. Click **Commit changes**.

{% ifversion fpt or ghec %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: check for versions applicability.

The `rust.yml` workflow file is added to the `.github/workflows` directory of your repository.
{% endif %}

## Specifying a Rust version

At the time of writing, the default rust compiler version is 1.83.0 rustup is available and can be used to install additional toolchains.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runner image cached tools change every few weeks, so it's better to say a current version is available — this will age badly with maintenance burden.


```yaml copy
- name: Temporarily modify the rust toolchain version
run: rustup override set nightly
- name: Ouput rust version for educational purposes
foursixnine marked this conversation as resolved.
Show resolved Hide resolved
run: rustup --version
```

### Caching dependencies

You can cache and restore dependencies using the following example below. Note that you will need to have Cargo.lock in your repository to cache dependencies.

```yaml copy
- name: ⚡ Cache
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place using emoji, feels a bit inconsistent..

- uses: {% data reusables.actions.action-cache %}
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: {% raw %}${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}{% endraw %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only raw escaping? check all the other expressions render correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you need to escape in all the places, otherwise:

Screenshot 2025-01-18 at 2 38 04

```

If you have a custom requirement or need finer controls for caching, you can take a look at the [`cache` action](https://github.com/marketplace/actions/cache). For more information, see [AUTOTITLE](/actions/using-workflows/caching-dependencies-to-speed-up-workflows).

## Building and testing your code

You can use the same commands that you use locally to build and test your code. This example workflow demonstrates how to use `cargo build` and `cargo test` in a job:

```yaml copy
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
BUILD_TARGET: [release] # refers to a cargo profile
outputs:
release_built: ${{ steps.set-output.outputs.release_built }}
steps:
- uses: {% data reusables.actions.action-checkout %}
- name: Build binaries in "${{ matrix.BUILD_TARGET }}" mode
run: cargo build --profile ${{ matrix.BUILD_TARGET }}
- name: Run tests in "${{ matrix.BUILD_TARGET }}" mode
run: cargo test --profile ${{ matrix.BUILD_TARGET }}
```

Note that the `release` keyword used above, corresponds to a cargo profile. You can use any [profile](https://doc.rust-lang.org/cargo/reference/profiles.html) you have defined in your `Cargo.toml` file.

## Upload artifacts

In case publishing artifacts is needed, but not to crates.io, the following example demonstrates how to upload artifacts to the workflow run:

```yaml copy
- name: Upload hello app
uses: {% data reusables.actions.action-upload-artifact %}
with:
name: cndk8-hello
path: target/${{ matrix.BUILD_TARGET }}/cndk8
```

And to use them on a different job, i.e publishing:

```yaml copy
- name: Download hello app
uses: {% data reusables.actions.action-download-artifact %}
with:
name: cndk8-hello
path: ./cndk8-hello
Comment on lines +116 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make it obvious why this folder structure and naming, would something like hello-app or an obvious placeholder be a better example?

- name: Publish hello app to GitHub Packages
run: |
curl -u "${{ github.actor }}:${{ secrets.GH_TOKEN }}" \
foursixnine marked this conversation as resolved.
Show resolved Hide resolved
-X POST "https://uploads.github.com/repos/${{ github.repository }}/releases/assets?name=cndk8-hello.tar.gz" \
--header "Content-Type: application/gzip" \
--data-binary @./cndk8-hello/cndk8
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is wanted/desired or if gh has plans to offer a rust registry

```

## Publishing your package or library to crates.io

Once you have setup your workflow to build and test your code, you can alternatively use a secret to login to crates.io and publish your package.

```yaml copy
- name: login into crates.io
run: cargo login ${{ secrets.CRATES_IO }}
- name: Build binaries in "release" mode
run: cargo build -r
- name: "Package for crates.io"
run: cargo package # publishes a package as a tarball
- name: "Publish to crates.io"
run: cargo publish # publishes your crate as a library that can be added as a dependency
```

As an example of how packages are published, see the [cndk8 0.1.0](https://crates.io/crates/cndk8/0.1.0). In the case that there are errors with Metadata check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if linking to a non-example crate on an external site is sustainable to maintain from docs; don't see the utility of the reference.

(this all seems taken from a real-world workflow of a real-world project — following the other CI tutorials it would feel better as an "anonymized" hello world type of code and naming?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. in the case of .NET that needs an argument with some placeholder filled in, it uses the GH Docs convention of using angle brackets:

run: dotnet pack --configuration Release <my project>

The examples here should too make it obvious what are just placeholder values in need of filling in correctly before the examples start to work for the reader…

your [manifest](https://doc.rust-lang.org/cargo/reference/manifest.html) Cargo.toml, when its about dirty directory check your Cargo.lock, and read the corresponding documentation.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ children:
- /building-and-testing-powershell
- /building-and-testing-python
- /building-and-testing-ruby
- /building-and-testing-rust
foursixnine marked this conversation as resolved.
Show resolved Hide resolved
- /building-and-testing-swift
- /building-and-testing-xamarin-applications
---
Expand Down
Loading