|
| 1 | +# Continuous Integration |
| 2 | + |
| 3 | +This directory contains tools used by crates in the `rust-bitcoin` org to implement Continuous |
| 4 | +Integration. Currently this is just a script `run_task.sh` that can be called from a GitHub workflow |
| 5 | +job to run a specific task. |
| 6 | + |
| 7 | +TL;DR `./run_task.sh --help` |
| 8 | + |
| 9 | +#### Table Of Contents |
| 10 | + |
| 11 | +- [Usage](#usage) |
| 12 | +- [Lock file](#lock-file) |
| 13 | +- [Crates](#crates) |
| 14 | + * [Per crate environment variables](#per-crate-environment-variables) |
| 15 | + * [Additional crate specific tests](#additional-crate-specific-tests) |
| 16 | +- [Fuzzing](#fuzzing) |
| 17 | +- [Example workflows](#example-workflows) |
| 18 | + * [A job using a stable toolchain](#a-job-using-a-stable-toolchain) |
| 19 | + * [A job using a specific nightly toolchain](#a-job-using-a-specific-nightly-toolchain) |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +The `run_task.sh` script expects a few things to be present when it runs: |
| 24 | + |
| 25 | +In the repository root: |
| 26 | + |
| 27 | +- A lock file: `Cargo.lock` |
| 28 | +- A script that defines the crates: `contrib/crates.sh` |
| 29 | + |
| 30 | +And for each crate there should exist a directory `REPO_DIR/CRATE/contrib/` containing: |
| 31 | + |
| 32 | +- `test_vars.sh`: Defines environment variables |
| 33 | +- Optional: `extra_tests.sh`: Additional test script. |
| 34 | + |
| 35 | +If the repository is not a workspace then per crate files go directly in `REPO_ROOT/contrib/`. |
| 36 | + |
| 37 | +(See [Crates`](#crates) below.) |
| 38 | + |
| 39 | +## Lock file |
| 40 | + |
| 41 | +Repositories MUST contain a `Cargo.lock` file before running `run_task.sh`. `cargo` is typically |
| 42 | +called with `--locked`. If you don't care about dependency versions just run `cargo update` in your |
| 43 | +CI job (to create a lock file) before calling `run_task.sh`. |
| 44 | + |
| 45 | +If you do care about versions consider adding: |
| 46 | + |
| 47 | +- `Cargo-recent.lock`: A manifest with some recent versions numbers that pass CI. |
| 48 | +- `Cargo-minimal.lock`: A manifest with some minimal version numbers that pass CI. |
| 49 | + |
| 50 | +Then you can use, for example: |
| 51 | + |
| 52 | +```yaml |
| 53 | + strategy: |
| 54 | + matrix: |
| 55 | + dep: [minimal, recent] |
| 56 | + steps: |
| 57 | + |
| 58 | + <!-- other stuff elided --> |
| 59 | + |
| 60 | + - name: "Copy lock file" |
| 61 | + run: cp Cargo-${{ matrix.dep }}.lock Cargo.lock |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +(Tip: Create minimal lock file with`cargo +nightly build -- -Z minimal-versions`.) |
| 66 | + |
| 67 | +## Crates |
| 68 | + |
| 69 | +All repositories MUST include a `REPO_DIR/contrib/crates.sh` script: |
| 70 | + |
| 71 | +```bash |
| 72 | +#!/usr/bin/env bash |
| 73 | + |
| 74 | +# Crates in this workspace to test (note "fuzz" is only built not tested). |
| 75 | +CRATES=("base58" "bitcoin" "fuzz" "hashes" "internals" "io" "units") |
| 76 | +``` |
| 77 | + |
| 78 | +`CRATES` MUST be an array. If repository is not a workspace use `CRATES=(".")`). |
| 79 | + |
| 80 | +### Per crate environment variables |
| 81 | + |
| 82 | +All crates MUST include a file `REPO_DIR/CRATE/contrib/test_vars.sh` |
| 83 | + |
| 84 | +```bash |
| 85 | +#!/usr/bin/env bash |
| 86 | + |
| 87 | +# Test all these features with "std" enabled. |
| 88 | +# |
| 89 | +# Ignore this if crate does not have "std" feature. |
| 90 | +FEATURES_WITH_STD="" |
| 91 | + |
| 92 | +# Test all these features without "std" enabled. |
| 93 | +# |
| 94 | +# Use this even if crate does not have "std" feature. |
| 95 | +FEATURES_WITHOUT_STD="" |
| 96 | + |
| 97 | +# Run these examples. |
| 98 | +EXAMPLES="" |
| 99 | +``` |
| 100 | + |
| 101 | +#### The `EXAMPLES` variable |
| 102 | + |
| 103 | +```bash |
| 104 | +EXAPMLES="example:feature" |
| 105 | +``` |
| 106 | + |
| 107 | +```bash |
| 108 | +EXAPMLES="example:feature1,feature2" |
| 109 | +``` |
| 110 | + |
| 111 | +```bash |
| 112 | +EXAPMLES="example_a:feature1,feature2 example_b:feature1" |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +Tip: if your example does not require any features consider using "default". |
| 117 | + |
| 118 | +```bash |
| 119 | +EXAPMLES="example_a:default" |
| 120 | +``` |
| 121 | + |
| 122 | +### Additional crate specific tests |
| 123 | + |
| 124 | +Additional tests can be put in an optional `contrib/extra_tests.sh` script. This script will be run |
| 125 | +as part of the `stable`, `nightly`, and `msrv` jobs after running unit tests. |
| 126 | + |
| 127 | +### Duplicate dependencies |
| 128 | + |
| 129 | +If any dependency should be ignored from the duplicate dependencies test (done when linting) specify |
| 130 | +them in a bash array in `REPO_DIR/contrib/whitelist_deps.sh` as such: |
| 131 | + |
| 132 | +Note, this is usually a temporary measure during upgrade. |
| 133 | + |
| 134 | +```bash |
| 135 | +#!/usr/bin/env bash |
| 136 | + |
| 137 | +DUPLICATE_DEPS=("bech32") |
| 138 | +``` |
| 139 | + |
| 140 | +## Fuzzing |
| 141 | + |
| 142 | +Fuzz tests are expected to be in a crate called `REPO_DIR/fuzz/`. The `run_task.sh` script just |
| 143 | +builds the fuzz crate as a sanity check. |
| 144 | + |
| 145 | +## Example workflows |
| 146 | + |
| 147 | +### A job using a stable toolchain |
| 148 | + |
| 149 | +To use the `run_task.sh` script you'll want to do something like this: |
| 150 | + |
| 151 | +```yaml |
| 152 | +jobs: |
| 153 | + Stable: # 2 jobs, one per manifest. |
| 154 | + name: Test - stable toolchain |
| 155 | + runs-on: ubuntu-latest |
| 156 | + strategy: |
| 157 | + fail-fast: false |
| 158 | + matrix: |
| 159 | + dep: [minimal, recent] |
| 160 | + steps: |
| 161 | + - name: "Checkout repo" |
| 162 | + uses: actions/checkout@v4 |
| 163 | + - name: "Checkout maintainer tools" |
| 164 | + uses: actions/checkout@v4 |
| 165 | + with: |
| 166 | + repository: rust-bitcoin/rust-bitcoin-maintainer-tools |
| 167 | + path: maintainer-tools |
| 168 | + - name: "Select toolchain" |
| 169 | + uses: dtolnay/rust-toolchain@stable |
| 170 | + - name: "Copy lock file" |
| 171 | + run: cp Cargo-${{ matrix.dep }}.lock Cargo.lock |
| 172 | + - name: "Run test script" |
| 173 | + run: ./maintainer-tools/ci/run_task.sh stable |
| 174 | +``` |
| 175 | +
|
| 176 | +### A job using a specific nightly toolchain |
| 177 | +
|
| 178 | +Have a file in the repository root with the nightly toolchain version to use. |
| 179 | +
|
| 180 | +```bash |
| 181 | +$ cat nightly_version |
| 182 | +nightly-2024-04-30 |
| 183 | +``` |
| 184 | + |
| 185 | +And use a `Prepare` job to a set an environment variable using the file. |
| 186 | + |
| 187 | +```yaml |
| 188 | +jobs: |
| 189 | + Prepare: |
| 190 | + runs-on: ubuntu-latest |
| 191 | + outputs: |
| 192 | + nightly_version: ${{ steps.read_toolchain.outputs.nightly_version }} |
| 193 | + steps: |
| 194 | + - name: Checkout Crate |
| 195 | + uses: actions/checkout@v4 |
| 196 | + - name: Read nightly version |
| 197 | + id: read_toolchain |
| 198 | + run: echo "nightly_version=$(cat nightly-version)" >> $GITHUB_OUTPUT |
| 199 | + |
| 200 | + Nightly: # 2 jobs, one per manifest. |
| 201 | + name: Test - nightly toolchain |
| 202 | + needs: Prepare |
| 203 | + runs-on: ubuntu-latest |
| 204 | + strategy: |
| 205 | + fail-fast: false |
| 206 | + matrix: |
| 207 | + dep: [minimal, recent] |
| 208 | + steps: |
| 209 | + - name: "Checkout repo" |
| 210 | + uses: actions/checkout@v4 |
| 211 | + - name: "Checkout maintainer tools" |
| 212 | + uses: actions/checkout@v4 |
| 213 | + with: |
| 214 | + repository: tcharding/rust-bitcoin-maintainer-tools |
| 215 | + ref: 05-02-ci |
| 216 | + path: maintainer-tools |
| 217 | + - name: "Select toolchain" |
| 218 | + uses: dtolnay/rust-toolchain@v1 |
| 219 | + with: |
| 220 | + toolchain: ${{ needs.Prepare.outputs.nightly_version }} |
| 221 | + - name: "Copy lock file" |
| 222 | + run: cp Cargo-${{ matrix.dep }}.lock Cargo.lock |
| 223 | + - name: "Run test script" |
| 224 | + run: ./maintainer-tools/ci/run_task.sh nightly |
| 225 | +``` |
0 commit comments