Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

583
Visualizações
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 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda