Skip to content

Use a docker image for testing on CI #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,32 @@ jobs:
restore-key: |
${{ runner.os }}-cargo-build-target-${{ env.CURRENT_RUSTC_VERSION }}-

- name: Install PostgreSQL
- name: Launch the postgres image
run: |
sudo apt-get update && DEBIAN_FRONTEND=noninteractive sudo apt-get install -y postgresql
sudo systemctl start postgresql
sudo -u postgres createuser $(whoami) -w
sudo -u postgres createdb $(whoami) -O $(whoami)
echo "::set-env name=CRATESFYI_DATABASE_URL::postgresql://$(whoami)@%2Fvar%2Frun%2Fpostgresql/$(whoami)"

- name: Build docs.rs
run: cargo build --locked
cp .env.sample .env
. .env
mkdir -p ${CRATESFYI_PREFIX}/public-html
docker-compose up -d db
# Give the database enough time to start up
sleep 5
# Make sure the database is actually working
psql "${CRATESFYI_DATABASE_URL}"

- name: Run rustfmt
run: cargo fmt -- --check

- name: Run clippy
run: cargo clippy -- -D warnings
run: cargo clippy --locked -- -D warnings

- name: Prepare the test environment
run: |
prefix=$(pwd)/ignored/prefix
mkdir -p ${prefix}/public-html
echo "::set-env name=CRATESFYI_PREFIX::${prefix}"
- name: Build docs.rs
run: cargo build --locked

- name: Test docs.rs
run: cargo test --locked -- --test-threads=1

- name: Clean up the database
run: docker-compose down --volumes

docker:
name: Docker
runs-on: ubuntu-latest
Expand Down
10 changes: 5 additions & 5 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use self::delete_crate::delete_crate;
pub use self::file::{add_path_into_database, move_to_s3};
pub use self::migrate::migrate;

use failure::Fail;
use postgres::error::Error;
use postgres::{Connection, TlsMode};
use std::env;
Expand All @@ -18,11 +19,10 @@ pub(crate) mod file;
mod migrate;

/// Connects to database
pub fn connect_db() -> Result<Connection, Error> {
// FIXME: unwrap might not be the best here
let db_url = env::var("CRATESFYI_DATABASE_URL")
.expect("CRATESFYI_DATABASE_URL environment variable is not exists");
Connection::connect(&db_url[..], TlsMode::None)
pub fn connect_db() -> Result<Connection, failure::Error> {
let err = "CRATESFYI_DATABASE_URL environment variable is not set";
let db_url = env::var("CRATESFYI_DATABASE_URL").map_err(|e| e.context(err))?;
Connection::connect(&db_url[..], TlsMode::None).map_err(Into::into)
}

pub(crate) fn create_pool() -> r2d2::Pool<r2d2_postgres::PostgresConnectionManager> {
Expand Down