• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

544
Vistas
GitHub actions cannot connect to MongoDB service

I'm having trouble running my automated tests with GitHub actions. I can't figure out why I can't connect with the MongoDB service running my integration tests. I tried different hosts: localhost, 127.0.0.1, 0.0.0.0, but none of them can connect with the database.

It works perfectly fine in my docker setup, but for some reason not with GitHub actions.

    name: CI master
    
    on: [push, pull_request]
    
    env:
      RUST_BACKTRACE: 1
      CARGO_TERM_COLOR: always
      APP_ENV: development
      APP_MONGO_USER: test
      APP_MONGO_PASS: password
      APP_MONGO_DB: test
    
    jobs:
      # Run tests
      test:
        name: Test
        runs-on: ubuntu-latest
        services:
          mongo:
            image: mongo
            env:
              MONGO_INITDB_ROOT_USERNAME: ${APP_MONGO_USER}
              MONGO_INITDB_ROOT_PASSWORD: ${APP_MONGO_PASS}
              MONGO_INITDB_DATABASE: ${APP_MONGO_DB}
            ports:
              - 27017:27017
        steps:
          - uses: actions/checkout@v2
          - uses: actions-rs/toolchain@v1
            with:
              profile: minimal
              toolchain: stable
              override: true
          - uses: actions-rs/cargo@v1
            with:
              command: test

Config file (development.toml).

[application]
host = "127.0.0.1"
port = 8080

Connecting to the database. The environment variables and config file get merged and I'm accessing them here through config: &Settings.

pub async fn init(config: &Settings) -> Result<Database> {
    let client_options = ClientOptions::parse(
        format!(
            "mongodb://{}:{}@{}:27017",
            config.mongo.user, config.mongo.pass, config.application.host
        )
        .as_str(),
    )
    .await?;

    let client = Client::with_options(client_options)?;
    let database = client.database("test"); // TODO: replace with env var

    database.run_command(doc! {"ping": 1}, None).await?;
    println!("Connected successfully.");

    Ok(database)
}

Calling the init function.

// Mongo
let mongo = mongo::init(&config).await.expect("Failed to init mongo");

The error I get.

thread 'health_check' panicked at 'Failed to init mongo: Error { kind: ServerSelectionError { message: "Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: 127.0.0.1:27017, Type: Unknown, Error: Connection refused (os error 111) }, ] }" }, labels: [] }', tests/health_check.rs:31:44
over 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I eventually solved it by adding a health check to my service. Seems that my issue had something to do with my database not being up yet before running the tests.

services:
          mongodb:
              image: mongo
              env:
                MONGO_INITDB_ROOT_USERNAME: test
                MONGO_INITDB_ROOT_PASSWORD: password
                MONGO_INITDB_DATABASE: test
              options: >-
                --health-cmd mongo
                --health-interval 10s
                --health-timeout 5s
                --health-retries 5
              ports:
                - 27017:27017
over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda