2020-08-23 11:00:32 +00:00
name : Rust
2020-12-05 19:14:22 +00:00
on :
# NB: this differs from the book's project!
# These settings allow us to run this specific CI pipeline for PRs against
# this specific branch (a.k.a. book chapter).
push :
branches :
2022-03-14 11:22:46 +00:00
- root-chapter-07-part2
2020-12-05 19:14:22 +00:00
pull_request :
2024-08-29 12:54:52 +00:00
types : [ opened, synchronize, reopened]
2020-12-05 19:14:22 +00:00
branches :
2022-03-14 11:22:46 +00:00
- root-chapter-07-part2
2020-08-23 11:00:32 +00:00
env :
CARGO_TERM_COLOR : always
2024-08-29 12:54:52 +00:00
SQLX_VERSION : 0.8 .0
2023-02-18 16:24:47 +00:00
SQLX_FEATURES : "rustls,postgres"
2024-08-31 15:09:08 +00:00
APP_USER : app
APP_USER_PWD : secret
APP_DB_NAME : newsletter
2020-08-23 11:00:32 +00:00
jobs :
test :
name : Test
runs-on : ubuntu-latest
2024-08-30 13:23:07 +00:00
# Service containers to run alongside the `test` container job
2020-08-25 21:19:53 +00:00
services :
postgres :
2024-08-29 12:54:52 +00:00
# Docker Hub image
2023-02-18 22:14:52 +00:00
image : postgres:14
2020-12-05 19:14:22 +00:00
env :
POSTGRES_USER : postgres
POSTGRES_PASSWORD : password
POSTGRES_DB : postgres
2020-08-25 21:19:53 +00:00
ports :
- 5432 : 5432
2020-08-23 11:00:32 +00:00
steps :
2024-08-29 12:54:52 +00:00
# Downloads a copy of the code in your repository before running CI tests
- name : Check out repository code
2024-08-30 09:58:39 +00:00
# The uses keyword specifies that this step will run v4 of the actions/checkout action.
2024-08-29 12:54:52 +00:00
# This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools).
# You should use the checkout action any time your workflow will run against the repository's code.
2024-08-30 09:58:39 +00:00
uses : actions/checkout@v4
2024-08-29 12:54:52 +00:00
2024-08-30 09:58:39 +00:00
# This GitHub Action installs a Rust toolchain using rustup. It is designed for one-line concise usage and good defaults.
# It also takes care of caching intermediate build artifacts.
2024-08-29 12:54:52 +00:00
- name : Install the Rust toolchain
2024-08-30 09:58:39 +00:00
uses : actions-rust-lang/setup-rust-toolchain@v1
2024-08-30 13:23:07 +00:00
2022-10-01 17:07:52 +00:00
- name : Install sqlx-cli
2023-02-18 16:24:47 +00:00
run :
2024-08-29 12:54:52 +00:00
cargo install sqlx-cli
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no -default-features
--locked
# The --locked flag can be used to force Cargo to use the packaged Cargo.lock file if it is available.
# This may be useful for ensuring reproducible builds, to use the exact same set of dependencies that were available when the package was published.
# It may also be useful if a newer version of a dependency is published that no longer builds on your system, or has other problems
2024-08-30 13:23:07 +00:00
2024-08-31 15:09:08 +00:00
- name : Create app user in Postgres
run : |
sudo apt-get install postgresql-client
# Create the application user
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${CREATE_QUERY}"
# Grant create db privileges to the app user
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${GRANT_QUERY}"
2020-08-25 21:19:53 +00:00
- name : Migrate database
run : |
2020-12-05 19:14:22 +00:00
SKIP_DOCKER=true ./scripts/init_db.sh
2024-08-30 13:23:07 +00:00
2023-02-18 16:24:47 +00:00
- name : Run tests
run : cargo test
2020-08-23 11:00:32 +00:00
2024-08-30 13:23:07 +00:00
- name : Check that queries are fresh
run : cargo sqlx prepare --workspace --check -- --all-targets
# `fmt` container job
2020-08-23 11:00:32 +00:00
fmt :
name : Rustfmt
runs-on : ubuntu-latest
steps :
2024-08-30 09:58:39 +00:00
- uses : actions/checkout@v4
- name : Install the Rust toolchain
uses : actions-rust-lang/setup-rust-toolchain@v1
2020-08-23 11:00:32 +00:00
with :
2021-01-23 17:48:23 +00:00
components : rustfmt
2023-02-18 16:24:47 +00:00
- name : Enforce formatting
run : cargo fmt --check
2020-08-23 11:00:32 +00:00
clippy :
name : Clippy
runs-on : ubuntu-latest
2024-08-30 13:23:07 +00:00
env :
SQLX_OFFLINE : true
2020-08-23 11:00:32 +00:00
steps :
2024-08-30 09:58:39 +00:00
- uses : actions/checkout@v4
- name : Install the Rust toolchain
uses : actions-rust-lang/setup-rust-toolchain@v1
2020-08-23 11:00:32 +00:00
with :
2021-01-23 17:48:23 +00:00
components : clippy
2023-02-18 16:24:47 +00:00
- name : Linting
run : cargo clippy -- -D warnings
2020-08-23 11:00:32 +00:00
coverage :
name : Code coverage
runs-on : ubuntu-latest
2020-08-25 21:19:53 +00:00
services :
postgres :
2023-02-18 22:14:52 +00:00
image : postgres:14
2020-12-05 19:14:22 +00:00
env :
POSTGRES_USER : postgres
POSTGRES_PASSWORD : password
POSTGRES_DB : postgres
2020-08-25 21:19:53 +00:00
ports :
- 5432 : 5432
2020-08-23 11:00:32 +00:00
steps :
2024-08-30 09:58:39 +00:00
- uses : actions/checkout@v4
- name : Install the Rust toolchain
uses : actions-rust-lang/setup-rust-toolchain@v1
2021-08-31 21:13:44 +00:00
with :
2024-08-30 09:58:39 +00:00
components : llvm-tools-preview
2023-02-18 16:24:47 +00:00
- name : Install sqlx-cli
2024-08-29 12:54:52 +00:00
run : cargo install sqlx-cli
2023-02-18 16:24:47 +00:00
--version=${{ env.SQLX_VERSION }}
--features ${{ env.SQLX_FEATURES }}
--no -default-features
--locked
2024-08-31 15:09:08 +00:00
- name : Create app user in Postgres
run : |
sudo apt-get install postgresql-client
# Create the application user
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${CREATE_QUERY}"
# Grant create db privileges to the app user
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;"
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${GRANT_QUERY}"
2020-08-25 21:19:53 +00:00
- name : Migrate database
2023-02-18 16:24:47 +00:00
run : SKIP_DOCKER=true ./scripts/init_db.sh
2024-08-30 09:58:39 +00:00
- name : Install cargo-llvm-cov
uses : taiki-e/install-action@cargo-llvm-cov
2023-02-18 16:24:47 +00:00
- name : Generate code coverage
2024-08-30 09:58:39 +00:00
run : cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
- name : Generate report
run : cargo llvm-cov report --html --output-dir coverage
- uses : actions/upload-artifact@v4
with :
name : "Coverage report"
path : coverage/