fix(ci): actually checkout the repository #64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Continuous Integration | |
on: | |
push: | |
branches: [ master ] | |
paths: [ | |
'flake.nix', | |
'Cargo.toml', | |
'Cargo.lock', | |
'crates/**/src/**', | |
'crates/**/Cargo.toml', | |
'.github/workflows/ci.yml', | |
] | |
jobs: | |
get-changes: | |
runs-on: ubuntu-latest | |
outputs: | |
changed_crates: ${{ steps.extract-crates.outputs.changed_crates }} | |
rebuild_all: ${{ steps.check-root-changes.outputs.rebuild_all }} | |
matrix: ${{ steps.generate-matrix.outputs.matrix }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Extract Changed Crates | |
id: extract-crates | |
run: | | |
changes=$(git diff --name-only HEAD^ HEAD | grep -oP 'crates/[^/]+/' | sort -u) | |
crates=$(echo "$changes" | sed -E 's/crates\/([^\/]+)\//\1/g' | sort -u) | |
echo "changed_crates=$crates" >> "$GITHUB_ENV" | |
# If any of the root files have changed we should rebuild all crates. | |
- name: Check for Root Changes | |
id: check-root-changes | |
if: ${{ steps.extract-crates.outputs.changed_crates == '' }} | |
run: | | |
changes=$(git diff --name-only HEAD^ HEAD | grep -oP 'Cargo.toml|Cargo.lock|flake.nix|ci.yml' | sort -u) | |
if [ -n "$changes" ]; then | |
echo "rebuild_all=true" >> "$GITHUB_ENV" | |
fi | |
- name: Generate Matrix | |
id: generate-matrix | |
run: | | |
systems=( | |
"x86_64-linux" | |
"aarch64-linux" | |
"x86_64-windows" | |
"aarch64-windows" | |
"x86_64-darwin" | |
"aarch64-darwin" | |
) | |
if [ -n "$rebuild_all" ]; then | |
crates=$(find crates -mindepth 1 -maxdepth 1 -type d -exec basename {} \;) | |
else | |
crates=${{ steps.extract-crates.outputs.changed_crates }} | |
fi | |
# We only want to build the crates defined as default members. | |
default_members=$(cargo metadata --no-deps --format-version 1 | jq -r '.workspace_default_members[] | split("#")[0]' | xargs -n1 basename) | |
crates=$(echo "$crates" | grep -Fxf <(echo "$default_members")) | |
matrix="{\"system\": [" | |
for system in "${systems[@]}"; do | |
matrix+="\"$system\"," | |
done | |
matrix="${matrix::-1}],\"crate\": [" | |
for crate in $crates; do | |
matrix+="\"$crate\"," | |
done | |
matrix="${matrix::-1}]}" | |
echo "matrix=$matrix" >> "$GITHUB_ENV" | |
build-artifacts: | |
needs: [ get-changes ] | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJson(needs.get-changes.outputs.matrix) }} | |
runs-on: ${{ endsWith(matrix.system, 'darwin') && 'macos-latest' || 'ubuntu-latest' }} | |
name: Build ${{ matrix.crate }} for ${{ matrix.system }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set Swap Space | |
if: ${{ !endsWith(matrix.system, 'darwin') }} | |
uses: pierotofy/set-swap-space@master | |
with: | |
swap-size-gb: 12 | |
- uses: cachix/install-nix-action@v30 | |
- uses: DeterminateSystems/magic-nix-cache-action@v8 | |
- run: nix build .#${{ matrix.crate }}-${{ matrix.system }}-dev -L --accept-flake-config | |
- uses: actions/[email protected] | |
if: ${{ !github.event.act }} | |
with: | |
name: ${{ matrix.crate }}-${{ matrix.system }} | |
path: result/bin/ | |
if-no-files-found: error | |
run-tests: | |
needs: [ get-changes, build-artifacts ] | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJson(needs.get-changes.outputs.matrix) }} | |
runs-on: ${{ endsWith(matrix.system, 'darwin') && 'macos-latest' || 'ubuntu-latest' }} | |
name: Run Tests for ${{ matrix.crate }} on ${{ matrix.system }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: cachix/install-nix-action@v30 | |
- uses: DeterminateSystems/magic-nix-cache-action@v8 | |
- name: Should we run tests? | |
id: should-run-tests | |
run: | | |
matrix_system="${{ matrix.system }}" | |
# If the system ends with windows we should skip the tests. | |
if [[ "$matrix_system" == *windows ]]; then | |
echo "run_tests=false" >> "$GITHUB_ENV" | |
fi | |
# We only want to run native tests on the runner that matches the host system. | |
host_system=$(nix eval --raw nixpkgs#system) | |
if [[ "$matrix_system" == "$host_system" ]]; then | |
echo "run_tests=true" >> "$GITHUB_ENV" | |
fi | |
echo "run_tests=false" >> "$GITHUB_ENV" | |
- name: Run Tests | |
if: ${{ steps.should-run-tests.outputs.run_tests == 'true' }} | |
run: nix develop --impure -c cargo nextest run --package ${{ matrix.crate }} --all-features | |
- name: Run Doc Tests | |
if: ${{ steps.should-run-tests.outputs.run_tests == 'true' }} | |
run: nix develop --impure -c cargo test --doc --package ${{ matrix.crate }} |