|
| 1 | +[--- |
| 2 | +title: Building and testing Rust |
| 3 | +intro: You can create a continuous integration (CI) workflow to build and test your Rust project. |
| 4 | +versions: |
| 5 | + fpt: '*' |
| 6 | + ghes: '*' |
| 7 | + ghec: '*' |
| 8 | +type: tutorial |
| 9 | +topics: |
| 10 | + - CI |
| 11 | +shortTitle: Build & test Rust |
| 12 | +redirect_from: |
| 13 | + - /actions/automating-builds-and-tests/building-and-testing-rust |
| 14 | +--- |
| 15 | + |
| 16 | +~~{% data reusables.actions.enterprise-github-hosted-runners %}~~ |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +This guide shows you how to build, test, and publish a Rust package. |
| 21 | + |
| 22 | +{% data variables.product.prodname_dotcom %}-hosted runners have a tools cache with preinstalled software, which includes the dependencies for Go. For a full list of up-to-date software and the preinstalled versions of Go, see [AUTOTITLE](/actions/using-github-hosted-runners/about-github-hosted-runners#preinstalled-software). |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +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). |
| 27 | + |
| 28 | +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). |
| 29 | + |
| 30 | +## Using a Rust workflow template |
| 31 | + |
| 32 | +{% data reusables.actions.workflow-templates-get-started %} |
| 33 | + |
| 34 | +{% 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. |
| 35 | + |
| 36 | +{% data reusables.repositories.navigate-to-repo %} |
| 37 | +{% data reusables.repositories.actions-tab %} |
| 38 | +{% data reusables.actions.new-starter-workflow %} |
| 39 | +1. The "Choose a workflow" page shows a selection of recommended workflow templates. Search for "Rust". |
| 40 | +1. Filter the selection of workflows by clicking **Continuous integration**. |
| 41 | +1. On the "Rust - by {% data variables.product.prodname_actions %}" workflow, click **Configure**. |
| 42 | + |
| 43 | +  |
| 44 | + |
| 45 | + |
| 46 | +1. Edit the workflow as required. For example, change the version of Rust. |
| 47 | +1. Click **Commit changes**. |
| 48 | + |
| 49 | +{% ifversion fpt or ghec %} |
| 50 | + The `rust.yml` workflow file is added to the `.github/workflows` directory of your repository. |
| 51 | +{% endif %} |
| 52 | + |
| 53 | +## Specifying a Rust version |
| 54 | + |
| 55 | +~~The easiest way to specify a Rust version is by using the `rustup` action provided by {% data variables.product.prodname_dotcom %}. For more information see, the [`setup-rust` action](https://github.com/actions/setup-rust/).~~ |
| 56 | + |
| 57 | +To use a preinstalled version of Rust on a {% data variables.product.prodname_dotcom %}-hosted runner, pass the relevant version to the `rust-version` property of the `setup-rust` action. This action finds a specific version of Rust from the tools cache on each runner, and adds the necessary binaries to `PATH`. These changes will persist for the remainder of the job. |
| 58 | + |
| 59 | +The `setup-rust` action is the recommended way of using rust with {% data variables.product.prodname_actions %}, because it helps ensure consistent behavior across different runners and different versions of Rust. If you are using a self-hosted runner, you must install rust and add it to your self-hosted runner's `PATH`. |
| 60 | + |
| 61 | +### Using multiple versions of rust |
| 62 | + |
| 63 | +```yaml copy |
| 64 | +name: Go |
| 65 | + |
| 66 | +on: [push] |
| 67 | + |
| 68 | +jobs: |
| 69 | + build: |
| 70 | + |
| 71 | + runs-on: ubuntu-latest |
| 72 | + strategy: |
| 73 | + matrix: |
| 74 | + rust-version: [ '1.8', '1.20', '1.21.x' ] |
| 75 | + |
| 76 | + steps: |
| 77 | + - uses: {% data reusables.actions.action-checkout %} |
| 78 | + - name: Setup Go {% raw %}${{ matrix.go-version }}{% endraw %} |
| 79 | + uses: {% data reusables.actions.action-setup-go %} |
| 80 | + with: |
| 81 | + go-version: {% raw %}${{ matrix.go-version }}{% endraw %} |
| 82 | + # You can test your matrix by printing the current Go version |
| 83 | + - name: Display Go version |
| 84 | + run: go version |
| 85 | +``` |
| 86 | +
|
| 87 | +### Using a specific Go version |
| 88 | +
|
| 89 | +You can configure your job to use a specific version of Go, such as `1.20.8`. Alternatively, you can use semantic version syntax to get the latest minor release. This example uses the latest patch release of Go 1.21: |
| 90 | + |
| 91 | +```yaml copy |
| 92 | + - name: Setup Go 1.21.x |
| 93 | + uses: {% data reusables.actions.action-setup-go %} |
| 94 | + with: |
| 95 | + # Semantic version range syntax or exact version of Go |
| 96 | + go-version: '1.21.x' |
| 97 | +``` |
| 98 | + |
| 99 | +## Installing dependencies |
| 100 | + |
| 101 | +You can use `go get` to install dependencies: |
| 102 | + |
| 103 | +```yaml copy |
| 104 | + steps: |
| 105 | + - uses: {% data reusables.actions.action-checkout %} |
| 106 | + - name: Setup Go |
| 107 | + uses: {% data reusables.actions.action-setup-go %} |
| 108 | + with: |
| 109 | + go-version: '1.21.x' |
| 110 | + - name: Install dependencies |
| 111 | + run: | |
| 112 | + go get . |
| 113 | + go get example.com/octo-examplemodule |
| 114 | + go get example.com/[email protected] |
| 115 | +``` |
| 116 | + |
| 117 | +### Caching dependencies |
| 118 | + |
| 119 | +You can cache and restore dependencies using the [`setup-go` action](https://github.com/actions/setup-go). By default, caching is {% ifversion actions-setup-go-default-cache-enabled %}enabled when using the `setup-go` action.{% else %}disabled, but you can set the `cache` parameter to `true` to enable it.{% endif %} |
| 120 | + |
| 121 | +{% ifversion actions-setup-go-default-cache-enabled %} |
| 122 | +The `setup-go` action searches for the dependency file, `go.sum`, in the repository root and uses the hash of the dependency file as a part of the cache key. |
| 123 | + |
| 124 | +You can use the `cache-dependency-path` parameter for cases when multiple dependency files are used, or when they are located in different subdirectories. |
| 125 | + |
| 126 | +```yaml copy |
| 127 | + - name: Setup Go |
| 128 | + uses: {% data reusables.actions.action-setup-go %} |
| 129 | + with: |
| 130 | + go-version: '1.17' |
| 131 | + cache-dependency-path: subdir/go.sum |
| 132 | +``` |
| 133 | + |
| 134 | +{% else %} |
| 135 | + |
| 136 | +When caching is enabled, the `setup-go` action searches for the dependency file, `go.sum`, in the repository root and uses the hash of the dependency file as a part of the cache key. |
| 137 | + |
| 138 | +```yaml copy |
| 139 | + - name: Setup Go |
| 140 | + uses: {% data reusables.actions.action-setup-go %} |
| 141 | + with: |
| 142 | + go-version: '1.21.x' |
| 143 | + cache: true |
| 144 | +``` |
| 145 | + |
| 146 | +Alternatively, you can use the `cache-dependency-path` parameter for cases when multiple dependency files are used, or when they are located in different subdirectories. |
| 147 | + |
| 148 | +```yaml copy |
| 149 | + - uses: {% data reusables.actions.action-setup-go %} |
| 150 | + with: |
| 151 | + go-version: '1.17' |
| 152 | + cache: true |
| 153 | + cache-dependency-path: subdir/go.sum |
| 154 | +``` |
| 155 | + |
| 156 | +{% endif %} |
| 157 | + |
| 158 | +If you have a custom requirement or need finer controls for caching, you can use the [`cache` action](https://github.com/marketplace/actions/cache). For more information, see [AUTOTITLE](/actions/using-workflows/caching-dependencies-to-speed-up-workflows). |
| 159 | + |
| 160 | +## Building and testing your code |
| 161 | + |
| 162 | +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: |
| 163 | + |
| 164 | +```yaml copy |
| 165 | +name: Go |
| 166 | +on: [push] |
| 167 | +
|
| 168 | +jobs: |
| 169 | + build: |
| 170 | + runs-on: ubuntu-latest |
| 171 | +
|
| 172 | + steps: |
| 173 | + - uses: {% data reusables.actions.action-checkout %} |
| 174 | + - name: Setup Go |
| 175 | + uses: {% data reusables.actions.action-setup-rust %} |
| 176 | + with: |
| 177 | + rust-version: '1.8.x' |
| 178 | + - name: Install dependencies |
| 179 | + run: go get . |
| 180 | + - name: Build |
| 181 | + run: go build -v ./... |
| 182 | + - name: Test with the Go CLI |
| 183 | + run: go test |
| 184 | +``` |
0 commit comments