diff options
author | Lars Wirzenius <liw@liw.fi> | 2024-01-20 12:10:10 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2024-01-20 12:10:10 +0200 |
commit | d8bcea71acca62a240685fb6c3204b47e05a088a (patch) | |
tree | 9f4d5ea3e07cbef54d88ca53a81e4fc6c273a825 | |
parent | 25aae0aeff4171d6fb6284cb98f23e2adee2dadc (diff) | |
download | ambient-driver-d8bcea71acca62a240685fb6c3204b47e05a088a.tar.gz |
feat: use /workspace/cache as a persistent cache between runs
Signed-off-by: Lars Wirzenius <liw@liw.fi>
Sponsored-by: author
-rw-r--r-- | ambient-driver.md | 45 | ||||
-rw-r--r-- | src/project.rs | 5 | ||||
-rw-r--r-- | src/run.rs | 7 |
3 files changed, 56 insertions, 1 deletions
diff --git a/ambient-driver.md b/ambient-driver.md index eab1226..5e20085 100644 --- a/ambient-driver.md +++ b/ambient-driver.md @@ -225,3 +225,48 @@ then stdout contains "pre_actions:\n" then stdout contains "\nactions:\n" then stdout contains "\npost_actions:\n" ~~~ +# Cache persists between CI runs + +_Requirement:_ Cache data is persisted between CI runs. + +_Justification:_ This allows incrementally building a project after +changes. + +_Stakeholders:_ Lars + +~~~scenario +given an installed ambient-driver +given file .config/ambient-driver/config.yaml from config.yaml +given an Ambient VM image ambient.qcow2 +given file cache.yaml +given a directory srcdir + +when I run ambient-driver run cache.yaml +then file ambient.log contains "counter is now 1." + +when I run ambient-driver run cache.yaml +then file ambient.log contains "counter is now 2." + +when I run ambient-driver run cache.yaml +then file ambient.log contains "counter is now 3." +~~~ + +~~~{#cache.yaml .file .yaml} +projects: + hello: + image: ambient.qcow2 + source: srcdir + plan: + - action: shell + shell: | + cache=/workspace/cache + counter="$cache/counter" + if [ -e "$counter" ]; then + n="$(expr "$(cat "$counter")" + 1)" + echo "$n" > "$counter" + else + echo 1 > "$counter" + fi + echo "counter is now $(cat "$counter")." + find "$cache" -ls +~~~ diff --git a/src/project.rs b/src/project.rs index 1ef8fc0..2c5f23d 100644 --- a/src/project.rs +++ b/src/project.rs @@ -183,6 +183,11 @@ impl State { Ok(()) } + /// Return cache directory for project. + pub fn cachedir(&self) -> PathBuf { + self.statedir.join("cache") + } + /// Return latest commit that CI has run on. pub fn latest_commit(&self) -> Option<&str> { self.latest_commit.as_deref() @@ -52,6 +52,11 @@ fn cmd_run(config: &EffectiveConfig, run: &RunCommand) -> Result<(), RunError> { if do_run { info!("{}: running CI on {}", name, project.source().display()); + let cachedir = state.cachedir(); + if !cachedir.exists() { + mkdir(&cachedir)?; + } + debug!("Executing pre-plan steps"); for action in project.pre_plan() { action.execute(project, statedir)?; @@ -96,7 +101,7 @@ fn cmd_run(config: &EffectiveConfig, run: &RunCommand) -> Result<(), RunError> { .with_artifact(&Some(artifact.clone())) .with_artifact_max_size(project.artifact_max_size()) .with_dependencies(&Some(dependencies.clone())) - .with_cache(&None) + .with_cache(&Some(cachedir.clone())) .with_log(log); let exit = qemu.run()?; debug!("QEMU exit code {}", exit); |