|
| 1 | +#![doc = include_str!("../README.md")] |
| 2 | + |
| 3 | +use crates_io_env_vars::var; |
| 4 | + |
| 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 | + |
| 41 | +/// Returns the Git SHA of the currently deployed commit. |
| 42 | +/// |
| 43 | +/// This value comes from the `HEROKU_SLUG_COMMIT` environment variable, |
| 44 | +/// which is set by Heroku when the `runtime-dyno-metadata` Labs feature |
| 45 | +/// is enabled. If the variable is not set (e.g., in local development |
| 46 | +/// or when the feature is disabled), returns `None`. |
| 47 | +/// |
| 48 | +/// Note: `HEROKU_SLUG_COMMIT` is deprecated by Heroku in favor of |
| 49 | +/// `HEROKU_BUILD_COMMIT`, but this function continues to use |
| 50 | +/// `HEROKU_SLUG_COMMIT` for backward compatibility with existing |
| 51 | +/// deployments. |
| 52 | +/// |
| 53 | +/// See <https://devcenter.heroku.com/articles/dyno-metadata> for more |
| 54 | +/// information. |
| 55 | +/// |
| 56 | +/// # Examples |
| 57 | +/// |
| 58 | +/// ``` |
| 59 | +/// use crates_io_heroku::slug_commit; |
| 60 | +/// |
| 61 | +/// if let Ok(Some(commit)) = slug_commit() { |
| 62 | +/// println!("Running commit: {}", commit); |
| 63 | +/// } else { |
| 64 | +/// println!("Commit SHA unknown"); |
| 65 | +/// } |
| 66 | +/// ``` |
| 67 | +pub fn slug_commit() -> anyhow::Result<Option<String>> { |
| 68 | + var("HEROKU_SLUG_COMMIT") |
| 69 | +} |
| 70 | + |
| 71 | +/// Returns the Git SHA of the currently deployed commit. |
| 72 | +/// |
| 73 | +/// This value comes from the `HEROKU_BUILD_COMMIT` environment variable, |
| 74 | +/// which is set by Heroku when the `runtime-dyno-build-metadata` Labs |
| 75 | +/// feature is enabled. If the variable is not set (e.g., in local development |
| 76 | +/// or when the feature is disabled), returns `None`. |
| 77 | +/// |
| 78 | +/// This is the recommended function to use, as `HEROKU_BUILD_COMMIT` is |
| 79 | +/// the current standard while `HEROKU_SLUG_COMMIT` is deprecated. |
| 80 | +/// |
| 81 | +/// See <https://devcenter.heroku.com/articles/dyno-metadata> for more |
| 82 | +/// information. |
| 83 | +/// |
| 84 | +/// # Examples |
| 85 | +/// |
| 86 | +/// ``` |
| 87 | +/// use crates_io_heroku::build_commit; |
| 88 | +/// |
| 89 | +/// if let Ok(Some(commit)) = build_commit() { |
| 90 | +/// println!("Running commit: {}", commit); |
| 91 | +/// } else { |
| 92 | +/// println!("Commit SHA unknown"); |
| 93 | +/// } |
| 94 | +/// ``` |
| 95 | +pub fn build_commit() -> anyhow::Result<Option<String>> { |
| 96 | + var("HEROKU_BUILD_COMMIT") |
| 97 | +} |
0 commit comments