From b4fa90f18334cda488890686712904bb1141cf81 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Sat, 4 May 2024 09:43:54 -0400 Subject: [PATCH] feat(ci-cd): setup Github CI-CD --- .env | 1 + .github/workflows/audit.yml | 16 ++++++ .github/workflows/ci.yml | 101 ++++++++++++++++++++++++++++++++++++ docs/technical_write_up.md | 4 ++ tests/subscribe.rs | 13 +++-- 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 .env create mode 100644 .github/workflows/audit.yml create mode 100644 .github/workflows/ci.yml diff --git a/.env b/.env new file mode 100644 index 0000000..88cfb53 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml new file mode 100644 index 0000000..2a5d565 --- /dev/null +++ b/.github/workflows/audit.yml @@ -0,0 +1,16 @@ +name: Security audit +on: + schedule: + - cron: "0 0 * * *" + push: + paths: + - "**/Cargo.toml" + - "**/Cargo.lock" +jobs: + security_audit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: taiki-e/install-action@cargo-deny + - name: Scan for vulnerabilities + run: cargo deny check advisories diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..430b7ee --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,101 @@ +name: Rust + +on: + push: + branches: + - master + +env: + CARGO_TERM_COLOR: always + SQLX_VERSION: 0.7.1 + SQLX_FEATURES: "rustls,postgres" + +jobs: + test: + name: Test + runs-on: ubuntu-latest + services: + postgres: + image: postgres:14 + ports: + - 5432:5432 + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + - name: Migrate database + run: | + sudo apt-get install libpq-dev -y + cargo install sqlx-cli --no-default-features --features postgres + SKIP_DOCKER=true ./scripts/init_db.sh + - name: Run tests + run: cargo test + + fmt: + name: Rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + - name: Enforce formatting + run: cargo fmt --check + + clippy: + name: Clippy + runs-on: ubuntu-latest + services: + postgres: + image: postgres:14 + ports: + - 5432:5432 + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + with: + components: clippy + - uses: Swatinem/rust-cache@v2 + with: + key: sqlx-${{ env.SQLX_VERSION }} + - name: Install sqlx-cli + run: cargo install sqlx-cli + --version=${{ env.SQLX_VERSION }} + --features ${{ env.SQLX_FEATURES }} + --no-default-features + --locked + - name: Migrate database + run: | + sudo apt-get install libpq-dev -y + SKIP_DOCKER=true ./scripts/init_db.sh + - name: Linting + run: cargo clippy -- -D warnings + + coverage: + name: Code coverage + runs-on: ubuntu-latest + services: + postgres: + image: postgres:14 + ports: + - 5432:5432 + steps: + - name: Checkout repository + uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + - name: Install libpq + run: sudo apt-get update && sudo apt-get install postgresql-client -y + - uses: Swatinem/rust-cache@v2 + with: + key: sqlx-${{ env.SQLX_VERSION }} + - name: Install tarpaulin + run: cargo install cargo-tarpaulin + - name: Install sqlx-cli + run: cargo install sqlx-cli + --version=${{ env.SQLX_VERSION }} + --features ${{ env.SQLX_FEATURES }} + --no-default-features + --locked + - name: Migrate database + run: SKIP_DOCKER=true ./scripts/init_db.sh + - name: Generate code coverage + run: cargo tarpaulin --verbose --workspace diff --git a/docs/technical_write_up.md b/docs/technical_write_up.md index 4923e1c..febc4e7 100644 --- a/docs/technical_write_up.md +++ b/docs/technical_write_up.md @@ -22,3 +22,7 @@ - The OS will find an available port for the test suite to use. - We use the same PostgreSQL database instance for both testing and production environment (might bite us in the ass later ?). + +## SQLx + +The SQLx library will run compile time checks to make sure our SQL queries are valid. This is done by running PostgreSQL queries during compile time. Therefore, it is important that DATABASE_URL must be properly set. diff --git a/tests/subscribe.rs b/tests/subscribe.rs index c57927b..14fd3b3 100644 --- a/tests/subscribe.rs +++ b/tests/subscribe.rs @@ -1,9 +1,8 @@ mod test_utils; -use test_utils::spawn_app; - use email_newsletter_api::configuration::{self, get_configuration}; use sqlx::{Connection, PgConnection}; +use test_utils::spawn_app; #[tokio::test] async fn subscribe_returns_a_200_for_valid_form_data() { @@ -13,7 +12,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() { let postgres_connection_string = configuration.database.connection_string(); - let connection = PgConnection::connect(&postgres_connection_string) + let mut connection = PgConnection::connect(&postgres_connection_string) .await .expect("Failed to connect to Postgres"); @@ -31,6 +30,14 @@ async fn subscribe_returns_a_200_for_valid_form_data() { assert!(response.status().is_success()); assert_eq!(Some(0), response.content_length()); + + let saved = sqlx::query!("SELECT email, name FROM subscriptions") + .fetch_one(&mut connection) + .await + .expect("Failed to fetch saved subscribtions"); + + assert_eq!(saved.email, "le_test@gmail.com"); + assert_eq!(saved.name, "le test"); } #[tokio::test]