Skip to content

Commit 91071eb

Browse files
committed
heroku: Add and use commit() fn with fallback logic
Use `HEROKU_BUILD_COMMIT` first, falling back to `HEROKU_SLUG_COMMIT` for backward compatibility with deployments using the older `runtime-dyno-metadata` Labs feature.
1 parent b493cb6 commit 91071eb

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

crates/crates_io_database_dump/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl DumpDirectory {
7070
}
7171
let metadata = Metadata {
7272
timestamp: &self.timestamp,
73-
crates_io_commit: crates_io_heroku::slug_commit()
73+
crates_io_commit: crates_io_heroku::commit()
7474
.ok()
7575
.flatten()
7676
.unwrap_or_else(|| "unknown".to_owned()),

crates/crates_io_heroku/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22

33
use crates_io_env_vars::var;
44

5+
/// Returns the Git SHA of the currently deployed commit.
6+
///
7+
/// This function tries `HEROKU_BUILD_COMMIT` first (the current standard),
8+
/// and falls back to `HEROKU_SLUG_COMMIT` (deprecated) if the former is not
9+
/// set. This provides compatibility with both old and new Heroku deployments.
10+
///
11+
/// Both environment variables are set by Heroku when the appropriate Labs
12+
/// features are enabled (`runtime-dyno-build-metadata` for `HEROKU_BUILD_COMMIT`,
13+
/// `runtime-dyno-metadata` for `HEROKU_SLUG_COMMIT`).
14+
///
15+
/// Returns `None` if neither variable is set (e.g., in local development).
16+
///
17+
/// See <https://devcenter.heroku.com/articles/dyno-metadata> for more
18+
/// information.
19+
///
20+
/// # Examples
21+
///
22+
/// ```
23+
/// use crates_io_heroku::commit;
24+
///
25+
/// if let Ok(Some(commit)) = commit() {
26+
/// println!("Running commit: {}", commit);
27+
/// } else {
28+
/// println!("Commit SHA unknown");
29+
/// }
30+
/// ```
31+
pub fn commit() -> anyhow::Result<Option<String>> {
32+
// Try the current standard first
33+
if let Some(commit) = build_commit()? {
34+
return Ok(Some(commit));
35+
}
36+
37+
// Fall back to the deprecated variable for backward compatibility
38+
slug_commit()
39+
}
40+
541
/// Returns the Git SHA of the currently deployed commit.
642
///
743
/// This value comes from the `HEROKU_SLUG_COMMIT` environment variable,

src/config/sentry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Context;
22
use crates_io_env_vars::{required_var, var, var_parsed};
3-
use crates_io_heroku::slug_commit;
3+
use crates_io_heroku::commit;
44
use sentry::IntoDsn;
55
use sentry::types::Dsn;
66

@@ -29,7 +29,7 @@ impl SentryConfig {
2929
Ok(Self {
3030
dsn,
3131
environment,
32-
release: slug_commit()?,
32+
release: commit()?,
3333
traces_sample_rate: var_parsed("SENTRY_TRACES_SAMPLE_RATE")?.unwrap_or(0.0),
3434
})
3535
}

src/controllers/site_metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::app::AppState;
22
use axum::Json;
33
use axum::response::IntoResponse;
4-
use crates_io_heroku::slug_commit;
4+
use crates_io_heroku::commit;
55
use serde::Serialize;
66

77
#[derive(Debug, Serialize, utoipa::ToSchema)]
@@ -34,7 +34,7 @@ pub struct MetadataResponse<'a> {
3434
pub async fn get_site_metadata(state: AppState) -> impl IntoResponse {
3535
let read_only = state.config.db.are_all_read_only();
3636

37-
let deployed_sha = slug_commit().ok().flatten();
37+
let deployed_sha = commit().ok().flatten();
3838
let deployed_sha = deployed_sha.as_deref().unwrap_or("unknown");
3939

4040
Json(MetadataResponse {

0 commit comments

Comments
 (0)