Skip to content

Commit

Permalink
Merge pull request #38 from Tietokilta/commit-hash
Browse files Browse the repository at this point in the history
feat: add commit hash to pdf files and /health
  • Loading branch information
lajp authored Oct 31, 2024
2 parents dccc115 + 5db866e commit 8c23a55
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN apk --no-cache add musl-dev
WORKDIR /app

# Copy over the Cargo.toml files to the shell project
COPY Cargo.toml Cargo.lock ./
COPY Cargo.toml Cargo.lock build.rs ./
# Build and cache the dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs
RUN cargo fetch
Expand All @@ -16,12 +16,14 @@ RUN rm src/main.rs
COPY ./src ./src
COPY ./templates ./templates
# Update the file date so Cargo rebuilds it
ARG GIT_COMMIT_SHA=development
ENV GIT_COMMIT_SHA=$GIT_COMMIT_SHA
RUN touch src/main.rs
RUN cargo build --release

FROM alpine as runtime

Check warning on line 24 in Dockerfile

View workflow job for this annotation

GitHub Actions / Push Docker image to GitHub Packages (laskugeneraattori)

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/
ENV BIND_ADDR 0.0.0.0

Check warning on line 25 in Dockerfile

View workflow job for this annotation

GitHub Actions / Push Docker image to GitHub Packages (laskugeneraattori)

Legacy key/value format with whitespace separator should not be used

LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format More info: https://docs.docker.com/go/dockerfile/rule/legacy-key-value-format/
WORKDIR /app
COPY --from=builder /app/target/release/laskugeneraattori app
EXPOSE 5237
EXPOSE 3000
CMD ["/app/app"]
12 changes: 12 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::process::Command;

fn main() {
let commit_hash = std::env::var("GIT_COMMIT_SHA").unwrap_or_else(|_| {
if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() {
String::from_utf8(output.stdout).unwrap()
} else {
String::from("dirty")
}
});
println!("cargo:rustc-env=COMMIT_HASH={}", commit_hash);
}
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
build:
context: .
dockerfile: Dockerfile
args:
GIT_COMMIT_SHA: development
environment:
- PORT=3000
- BIND_ADDR=0.0.0.0
Expand Down
8 changes: 6 additions & 2 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub fn app() -> Router<crate::state::State> {
})
}

async fn health() -> &'static str {
"OK"
async fn health() -> String {
format!(
"Laskugeneraattori {} {}",
&env!("CARGO_PKG_VERSION"),
&env!("COMMIT_HASH")
)
}
10 changes: 7 additions & 3 deletions src/pdfgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use typst::{
diag::{FileError, FileResult},
eval::Tracer,
foundations::{Bytes, Datetime, IntoValue},
foundations::{Bytes, Datetime, IntoValue, Value},
model::Document,
syntax::{FileId, Source, VirtualPath},
text::{Font, FontBook},
Expand Down Expand Up @@ -154,8 +154,12 @@ impl Sandbox {

fn with_data(&self, data: impl IntoValue) -> Self {
let mut new = self.clone();
new.library
.update(|l| l.global.scope_mut().define("data", data));
new.library.update(|l| {
let scope = l.global.scope_mut();
scope.define("data", data);
scope.define("COMMIT_HASH", Value::Str(env!("COMMIT_HASH").into()));
scope.define("VERSION", Value::Str(env!("CARGO_PKG_VERSION").into()));
});
new.time = time::OffsetDateTime::now_utc();
new
}
Expand Down
10 changes: 7 additions & 3 deletions templates/invoice.typ
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
Laskut hyväksytään Tietokillan hallituksen kokouksissa.
Ongelmatapauksissa ota yhteyttä rahastonhoitajaan: #link("mailto:rahastonhoitaja@tietokilta.fi").
Tarkemmat yhteystiedot löydät killan sivuilta.

#v(1em)
#align(right)[Laskugeneraattori #VERSION #link("https://github.com/Tietokilta/laskugeneraattori/commit/" + COMMIT_HASH)[#COMMIT_HASH.slice(0, 7)]]
],
footer-descent: -0.5em,
)
#set text(lang: "fi")

Expand Down Expand Up @@ -95,11 +99,11 @@
table.header([*Tiedosto*], [*Kuvaus*]),
..data.attachments
.zip(data.attachment_descriptions)
.map(((a, d)) => (
.map(((a, d)) =>
// NOTE: add breakpoints to the string
// so that it can be wrapped to multiple lines
a.filename.codepoints().map(x => x + sym.zws).join(),
d)).flatten()
(a.filename.codepoints().map(x => x + sym.zws).join(), d)
).flatten()
)

#for file in data.attachments {
Expand Down

0 comments on commit 8c23a55

Please sign in to comment.