feat(ci-cd): setup Github CI-CD
This commit is contained in:
		
							
								
								
									
										1
									
								
								.env
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.env
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" | ||||
							
								
								
									
										16
									
								
								.github/workflows/audit.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								.github/workflows/audit.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -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 | ||||
							
								
								
									
										101
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -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 | ||||
| @@ -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. | ||||
|   | ||||
| @@ -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] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user