Skip to content

Commit fc95bc3

Browse files
committed
setup caching
1 parent 138758a commit fc95bc3

File tree

7 files changed

+148
-16
lines changed

7 files changed

+148
-16
lines changed

.github/workflows/tests.yml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
name: CI
1+
name: Tests and lints
22

33
on:
44
pull_request:
5-
workflow_dispatch:
65

76
jobs:
87
ci:
@@ -13,8 +12,35 @@ jobs:
1312
- name: Check out code
1413
uses: actions/checkout@v4
1514

15+
- name: Install dagger
16+
run: curl -fsSL https://dl.dagger.io/dagger/install.sh | BIN_DIR=$HOME/.local/bin sh
17+
18+
- uses: actions/cache/restore@v4
19+
with:
20+
path: ./cache
21+
key: dagger-cache-${{ github.run_id }}
22+
restore-keys:
23+
dagger-cache-
24+
25+
- name: make sure cache directory exists.
26+
run: mkdir -p ./cache/target
27+
1628
- name: Run tests
17-
uses: dagger/dagger-for-github@v8.1.0
29+
id: tests
30+
run: dagger run cargo run -p dagger_pipeline --features ci -- tests --export
31+
continue-on-error: true
32+
33+
- uses: actions/cache/save@v4
34+
with:
35+
path: ./cache
36+
key: dagger-cache-${{ github.run_id }}
37+
38+
- name: Upload report
39+
uses: actions/upload-artifact@v4
1840
with:
19-
verb: run
20-
args: cargo run -p dagger_pipeline -- tests --tui
41+
name: test-report
42+
path: ./allure_report
43+
44+
- name: Fail Workflow
45+
if: steps.tests.outcome == 'failure'
46+
run: exit 1
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Upload test results
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Tests and lints"]
6+
types: [completed]
7+
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
12+
jobs:
13+
upload:
14+
if: github.event.workflow_run.conclusion != 'cancelled'
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Resolve PR number
18+
id: pr
19+
uses: actions/github-script@v7
20+
with:
21+
script: |
22+
const run = context.payload.workflow_run;
23+
// Prefer PR info embedded in the workflow_run payload
24+
if (run.pull_requests && run.pull_requests.length > 0) {
25+
core.setOutput('number', String(run.pull_requests[0].number));
26+
} else {
27+
// Fallback: find PR by commit SHA
28+
const {owner, repo} = context.repo;
29+
const {data} = await github.rest.repos.listPullRequestsAssociatedWithCommit({
30+
owner, repo, commit_sha: run.head_sha
31+
});
32+
core.setOutput('number', data[0]?.number ? String(data[0].number) : '');
33+
}
34+
35+
- name: Download report artifact
36+
if: steps.pr.outputs.number != ''
37+
uses: dawidd6/action-download-artifact@v3
38+
with:
39+
run_id: ${{ github.event.workflow_run.id }}
40+
name: test-report
41+
path: ./report
42+
43+
- name: Deploy preview to Cloudflare Pages
44+
if: steps.pr.outputs.number != ''
45+
id: pages
46+
uses: cloudflare/pages-action@v1
47+
with:
48+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
49+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
50+
projectName: natrix-test-result
51+
directory: ./report
52+
branch: pr-${{ steps.pr.outputs.number }}-${{ github.event.workflow_run.head_sha }}
53+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
54+
55+
- name: Comment on PR with preview URL
56+
if: steps.pr.outputs.number != '' && steps.pages.outputs.url != ''
57+
uses: marocchino/sticky-pull-request-comment@v2
58+
with:
59+
header: test-report-preview
60+
number: ${{ steps.pr.outputs.number }}
61+
message: |
62+
Test report: ${{ steps.pages.outputs.url }}
63+
64+
- name: Add to job summary
65+
if: steps.pages.outputs.url != ''
66+
run: |
67+
echo 'Test report preview: ${{ steps.pages.outputs.url }}' >> $GITHUB_STEP_SUMMARY

dagger_pipeline/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ uuid = { version = "1.18.1", features = ["v4"] }
2626
serde = { version = "1.0.219", features = ["derive"] }
2727
serde_json = "1.0.143"
2828

29+
[features]
30+
ci = []
31+
2932
[lints]
3033
workspace = true

dagger_pipeline/src/main.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ struct TestCommand {
2121
/// Output to a tui instead
2222
#[arg(short, long)]
2323
tui: bool,
24+
/// Generate the allure report but just export it to the host.
25+
#[arg(long)]
26+
export: bool,
2427
}
2528

2629
/// Run a dagger pipeline too generate test reports.
@@ -75,7 +78,7 @@ mod prelude {
7578
no_cache: None,
7679
},
7780
);
78-
self.with_directory("/app", workspace)
81+
self.with_mounted_directory("/app", workspace)
7982
.with_workdir("/app")
8083
.with_mounted_cache("/app/target", client.cache_volume("rust-target"))
8184
}
@@ -109,7 +112,31 @@ async fn main() -> Result<()> {
109112
dagger_sdk::connect(async move |client| {
110113
match arguments {
111114
Cli::Tests(arguments) => {
115+
if cfg!(feature = "ci") {
116+
base_images::base(&client)
117+
.with_workspace(&client)
118+
.with_mounted_directory("/load_cache", client.host().directory("./cache"))
119+
.with_exec(vec![
120+
"sh",
121+
"-c",
122+
"mv /load_cache/target/* /app/target/* || true",
123+
])
124+
.sync()
125+
.await?;
126+
}
127+
112128
let reports = report::run_all_tests(&client, &arguments).await?;
129+
130+
if cfg!(feature = "ci") {
131+
base_images::base(&client)
132+
.with_workspace(&client)
133+
.with_exec(vec!["mkdir", "/store_cache"])
134+
.with_exec(vec!["sh", "-c", "mv /app/target /store_cache"])
135+
.directory("/store_cache")
136+
.export("./cache")
137+
.await?;
138+
}
139+
113140
if arguments.tui {
114141
let total = reports.len();
115142
let mut pass = 0u16;
@@ -142,8 +169,19 @@ async fn main() -> Result<()> {
142169
return Err(eyre!("Tests failed"));
143170
}
144171
} else {
145-
let report = report::generate_allure_report(&client, reports).await?;
146-
report::serve_dist(&client, report).await?;
172+
let report = report::generate_allure_report(&client, &reports).await?;
173+
174+
if arguments.export {
175+
report.export("./allure_report/").await?;
176+
let tests_failed = reports
177+
.into_iter()
178+
.any(|report| matches!(report.status, TestStatus::Failed));
179+
if tests_failed {
180+
return Err(eyre!("Tests failed"));
181+
}
182+
} else {
183+
report::serve_dist(&client, report).await?;
184+
}
147185
}
148186
}
149187
Cli::Fix => {

dagger_pipeline/src/report.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn all_tests<'q>(
7878
Box::pin(crate::targets::test_project_gen(client, "nightly")),
7979
false,
8080
),
81-
(Box::pin(crate::targets::test_book_links(client)), false),
81+
(Box::pin(crate::targets::test_book_links(client)), true),
8282
(Box::pin(crate::targets::test_book_examples(client)), true),
8383
(
8484
Box::pin(crate::targets::integration_test(
@@ -109,14 +109,14 @@ fn all_tests<'q>(
109109
}
110110

111111
/// Generate the final report
112-
pub async fn generate_allure_report(client: &Query, results: Vec<TestResult>) -> Result<Directory> {
112+
pub async fn generate_allure_report(client: &Query, results: &[TestResult]) -> Result<Directory> {
113113
let mut reports = client.directory();
114114
// HACK: Dagger doesnt like you creating 100+ files in one call.
115115
// So we chunk it up.
116-
for chunk in &results.into_iter().chunks(50) {
116+
for chunk in &results.iter().chunks(50) {
117117
let mut chunked = client.directory();
118118
for result in chunk {
119-
chunked = chunked.with_directory(".", result.into_file(client)?);
119+
chunked = chunked.with_directory(".", result.as_file(client)?);
120120
}
121121

122122
reports = reports.with_directory(".", chunked.sync().await?);

dagger_pipeline/src/targets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ pub(crate) struct TestResult {
139139

140140
impl TestResult {
141141
/// Convert test result to a directory containing the result file
142-
pub(crate) fn into_file(self, client: &Query) -> Result<Directory> {
143-
let content = serde_json::to_string(&self)?;
142+
pub(crate) fn as_file(&self, client: &Query) -> Result<Directory> {
143+
let content = serde_json::to_string(self)?;
144144
let filename = format!("{}-result.json", self.uuid);
145145
Ok(client.directory().with_new_file(filename, content))
146146
}

flake.nix

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
])
4646
pkgs.nur.repos.dagger.dagger
4747
just
48-
bacon
49-
cargo-edit
5048

5149
wasm-bindgen-cli_0_2_100
5250
binaryen

0 commit comments

Comments
 (0)