End of Chapter 9 #76
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Rust | |
on: | |
push: | |
branches: [ "main", "development" ] | |
pull_request: | |
types: [ opened, synchronize, reopened ] | |
branches: [ "main", "development" ] | |
env: | |
CARGO_TERM_COLOR: always | |
SQLX_VERSION: 0.8.2 | |
SQLX_FEATURES: "rustls,postgres" | |
APP_USER: app | |
APP_USER_PWD: secret | |
APP_DB_NAME: claans | |
jobs: | |
test: | |
name: Test | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:16 | |
env: | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: password | |
POSTGRES_DB: postgres | |
ports: | |
- 5432:5432 | |
steps: | |
- name: Check out repository code | |
uses: actions/checkout@v4 | |
- name: Install the Rust toolchain | |
uses: actions-rust-lang/setup-rust-toolchain@v1 | |
- name: Install sqlx-cli | |
run: | |
cargo install sqlx-cli | |
--version=${{ env.SQLX_VERSION }} | |
--features ${{ env.SQLX_FEATURES }} | |
--no-default-features | |
--locked | |
- name: Create app user in Postgres | |
run: | | |
sudo apt-get install postgresql-client | |
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';" | |
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${CREATE_QUERY}" | |
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;" | |
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${GRANT_QUERY}" | |
- name: Migrate database | |
run: | | |
SKIP_DOCKER=true ./scripts/init_db.sh | |
- name: Run tests | |
run: cargo test | |
- name: Check that queries are fresh | |
run: cargo sqlx prepare --workspace --check -- --all-targets | |
fmt: | |
name: Rustfmt | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install the Rust toolchain | |
uses: actions-rust-lang/setup-rust-toolchain@v1 | |
with: | |
components: rustfmt | |
- name: Enforce formatting | |
run: cargo fmt --check | |
clippy: | |
name: Clippy | |
runs-on: ubuntu-latest | |
env: | |
SQLX_OFFLINE: true | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install the Rust toolchain | |
uses: actions-rust-lang/setup-rust-toolchain@v1 | |
with: | |
components: clippy | |
- name: Linting | |
run: cargo clippy -- -D warnings | |
coverage: | |
name: Code coverage | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:14 | |
env: | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: password | |
POSTGRES_DB: postgres | |
ports: | |
- 5432:5432 | |
redis: | |
image: redis:7 | |
ports: | |
- 6379:6379 | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Install the Rust toolchain | |
uses: actions-rust-lang/setup-rust-toolchain@v1 | |
with: | |
components: llvm-tools-preview | |
- name: Install sqlx-cli | |
run: cargo install sqlx-cli | |
--version=${{ env.SQLX_VERSION }} | |
--features ${{ env.SQLX_FEATURES }} | |
--no-default-features | |
--locked | |
- name: Create app user in Postgres | |
run: | | |
sudo apt-get install postgresql-client | |
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';" | |
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${CREATE_QUERY}" | |
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;" | |
PGPASSWORD="password" psql -U "postgres" -h "localhost" -c "${GRANT_QUERY}" | |
- name: Migrate database | |
run: SKIP_DOCKER=true ./scripts/init_db.sh | |
- name: Install cargo-llvm-cov | |
uses: taiki-e/install-action@cargo-llvm-cov | |
- name: Generate code coverage | |
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/ |