Repository navigation
Gap 9: cargo xtask ci on every PR (Linux x64); ci --quick; one local ci at a time #2302
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5690afe
Gap 9: cargo xtask ci on every PR (Linux x64); ci --quick; one local …
mattsivak c3c4cf1
ci --quick: build the workspace's test targets, run only the changed …
mattsivak 960e1f1
Gap 9: ci --quick reads git's -z output, tests the crates that embed …
mattsivak 10c4d0c
Gap 9: only a PR's own pushes share a CI concurrency group
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # The quality gate (`cargo xtask ci`: fmt, clippy, heif, tests, parity, layers, assets, wasm) on | ||
| # every pull request and every push to main, so the full run happens here rather than on the | ||
| # machine of everyone with a branch open. Locally, `cargo xtask ci --quick` checks what a branch's | ||
| # changes can break in a few minutes; this job is the merge gate. | ||
| # | ||
| # Linux only: macOS / Windows test jobs are #1148. Standard GitHub-hosted runner (free for public | ||
| # repositories), never a larger one. | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| concurrency: | ||
| # A pull request's pushes share one group, so a new push supersedes the run of the old one. | ||
| # Every other run (a push to main, a manual one) gets a group of its own: a group keeps only one | ||
| # active and one pending run, so sharing one would let a later push replace a queued run and skip | ||
| # its `cargo xtask ci` — and every push to main is meant to get a full run. | ||
| group: ${{ github.event_name == 'pull_request' && format('ci-{0}', github.ref) || format('ci-{0}', github.run_id) }} | ||
| cancel-in-progress: ${{ github.event_name == 'pull_request' }} | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| env: | ||
| CARGO_TERM_COLOR: always | ||
| # a fresh build every run: incremental state is only overhead, and it bloats the cache | ||
| CARGO_INCREMENTAL: 0 | ||
| RUST_BACKTRACE: 1 | ||
| # one job per runner: the machine-wide CI lock (xtask/src/ci_lock.rs) has nothing to wait for | ||
| LIGHTCRAFT_CI_LOCK: "off" | ||
|
|
||
| jobs: | ||
| xtask-ci: | ||
| name: cargo xtask ci (Linux x86_64) | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 90 | ||
| env: | ||
| CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts | ||
| CRAFT_FONTS_REQUIRED: "1" | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| persist-credentials: false | ||
| # the font build input, so the Japanese and Chinese font tests run (they skip without it) | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| repository: storytold/craft-fonts | ||
| ref: 3d9ab3a132ed0eff457e424226a435a901da30d3 # as freebsd.yml; bump deliberately | ||
| path: craft-fonts | ||
| persist-credentials: false | ||
| - name: Install system libraries | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y --no-install-recommends \ | ||
| libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev | ||
| - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable | ||
| with: | ||
| components: rustfmt, clippy | ||
| targets: wasm32-unknown-unknown | ||
| # dependencies only (rust-cache drops the workspace's own artifacts), saved from main and | ||
| # restored by pull requests | ||
| - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 | ||
| with: | ||
| key: ci | ||
| save-if: ${{ github.ref == 'refs/heads/main' }} | ||
| - name: cargo xtask ci | ||
| run: cargo xtask ci | ||
| - name: Job summary | ||
| if: ${{ !cancelled() }} | ||
| run: cat target/ci/summary.md >> "$GITHUB_STEP_SUMMARY" || true | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| //! One `cargo xtask ci` at a time per machine. | ||
| //! | ||
| //! Every CI run builds the workspace several times with one job per CPU. Several worktrees running | ||
| //! it at once (parallel agents) slow each other down far more than waiting in turn would, and make | ||
| //! the machine unusable meanwhile. So `ci` first takes one of `LIGHTCRAFT_CI_SLOTS` (default 1) | ||
| //! lock files in a per-user cache directory, shared by every checkout and worktree, and holds it | ||
| //! until it exits. The OS drops the lock when the process ends, crashed or not. | ||
| //! | ||
| //! - `LIGHTCRAFT_CI_SLOTS=N`: N runs at once (1–64). | ||
| //! - `LIGHTCRAFT_CI_LOCK=off`: no lock (e.g. a CI runner that only ever runs one job). | ||
| //! - `LIGHTCRAFT_CI_LOCK=<dir>`: keep the lock files there instead. | ||
|
|
||
| use std::fs::{File, OpenOptions, TryLockError}; | ||
| use std::io::Write; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| const POLL: Duration = Duration::from_secs(2); | ||
|
|
||
| /// A held CI slot; dropping it releases the slot. | ||
| pub struct Held { | ||
| _file: File, | ||
| owner: PathBuf, | ||
| } | ||
|
|
||
| impl Drop for Held { | ||
| fn drop(&mut self) { | ||
| let _ = std::fs::remove_file(&self.owner); | ||
| } | ||
| } | ||
|
|
||
| /// Where the lock files live, or `None` when the lock is switched off. | ||
| fn lock_dir() -> Option<PathBuf> { | ||
| match std::env::var_os("LIGHTCRAFT_CI_LOCK") { | ||
| Some(v) if matches!(v.to_str(), Some("off" | "0" | "false" | "no")) => return None, | ||
| Some(v) if !v.is_empty() => return Some(PathBuf::from(v)), | ||
| _ => {} | ||
| } | ||
| let cache = std::env::var_os("XDG_CACHE_HOME") | ||
| .map(PathBuf::from) | ||
| .or_else(|| std::env::var_os("LOCALAPPDATA").map(PathBuf::from)) | ||
| .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cache"))) | ||
| .unwrap_or_else(std::env::temp_dir); | ||
| Some(cache.join("lightcraft")) | ||
| } | ||
|
|
||
| fn slots() -> usize { | ||
| std::env::var("LIGHTCRAFT_CI_SLOTS").ok().and_then(|s| s.trim().parse().ok()).unwrap_or(1).clamp(1, 64) | ||
| } | ||
|
|
||
| fn slot_path(dir: &Path, slot: usize) -> PathBuf { | ||
| if slot == 0 { dir.join("ci.lock") } else { dir.join(format!("ci-{}.lock", slot + 1)) } | ||
| } | ||
|
|
||
| fn owner_path(lock: &Path) -> PathBuf { | ||
| lock.with_extension("lock.owner") | ||
| } | ||
|
|
||
| /// Take the first free slot of `slots` in `dir`, or `None` when all are held. | ||
| pub fn try_acquire(dir: &Path, slots: usize, owner: &str) -> Result<Option<Held>, String> { | ||
| std::fs::create_dir_all(dir).map_err(|e| format!("ci lock: {}: {e}", dir.display()))?; | ||
| for slot in 0..slots.max(1) { | ||
| let path = slot_path(dir, slot); | ||
| let file = | ||
| OpenOptions::new().create(true).truncate(false).write(true).open(&path).map_err(|e| format!("ci lock: {}: {e}", path.display()))?; | ||
| match file.try_lock() { | ||
| Ok(()) => { | ||
| // a sidecar file, not the lock file: Windows locks are mandatory, so others couldn't read it | ||
| let owner_file = owner_path(&path); | ||
| let _ = std::fs::File::create(&owner_file).and_then(|mut f| f.write_all(owner.as_bytes())); | ||
| return Ok(Some(Held { _file: file, owner: owner_file })); | ||
| } | ||
| Err(TryLockError::WouldBlock) => continue, | ||
| Err(TryLockError::Error(e)) => return Err(format!("ci lock: {}: {e}", path.display())), | ||
| } | ||
| } | ||
| Ok(None) | ||
| } | ||
|
|
||
| /// Who holds each slot, as they wrote it. | ||
| fn holders(dir: &Path, slots: usize) -> Vec<String> { | ||
| (0..slots.max(1)) | ||
| .filter_map(|slot| std::fs::read_to_string(owner_path(&slot_path(dir, slot))).ok()) | ||
| .map(|s| s.trim().to_owned()) | ||
| .filter(|s| !s.is_empty()) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Wait for a CI slot (see the module docs). `None` when the lock is off. | ||
| pub fn acquire(root: &Path, command: &str) -> Result<Option<Held>, String> { | ||
| let Some(dir) = lock_dir() else { | ||
| return Ok(None); | ||
| }; | ||
| let slots = slots(); | ||
| let owner = format!("pid {} in {}: {command}", std::process::id(), root.display()); | ||
| if let Some(held) = try_acquire(&dir, slots, &owner)? { | ||
| return Ok(Some(held)); | ||
| } | ||
| let waiting = Instant::now(); | ||
| eprintln!("=== ci: waiting for another CI run to finish ({slots} at a time; LIGHTCRAFT_CI_SLOTS changes it) ==="); | ||
| for h in holders(&dir, slots) { | ||
| eprintln!(" running: {h}"); | ||
| } | ||
| loop { | ||
| std::thread::sleep(POLL); | ||
| if let Some(held) = try_acquire(&dir, slots, &owner)? { | ||
| eprintln!("=== ci: waited {:.0} s for a CI slot ===", waiting.elapsed().as_secs_f64()); | ||
| return Ok(Some(held)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn scratch(name: &str) -> PathBuf { | ||
| let dir = std::env::temp_dir().join(format!("xtask-ci-lock-{name}-{}", std::process::id())); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| dir | ||
| } | ||
|
|
||
| #[test] | ||
| fn one_slot_is_exclusive_until_dropped() { | ||
| let dir = scratch("one"); | ||
| let first = try_acquire(&dir, 1, "first").unwrap().expect("free slot"); | ||
| assert!(try_acquire(&dir, 1, "second").unwrap().is_none()); | ||
| assert_eq!(holders(&dir, 1), ["first"]); | ||
| drop(first); | ||
| assert!(holders(&dir, 1).is_empty()); | ||
| assert!(try_acquire(&dir, 1, "third").unwrap().is_some()); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn more_slots_admit_more_runs() { | ||
| let dir = scratch("two"); | ||
| let a = try_acquire(&dir, 2, "a").unwrap(); | ||
| let b = try_acquire(&dir, 2, "b").unwrap(); | ||
| assert!(a.is_some() && b.is_some()); | ||
| assert!(try_acquire(&dir, 2, "c").unwrap().is_none()); | ||
| assert_eq!(holders(&dir, 2), ["a", "b"]); | ||
| let _ = std::fs::remove_dir_all(&dir); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.