Skip to content

Cannot connect to MySQL service in GitHub Actions pipeline

Published: at 09:23 AM

Symptoms

Given the following GitHub Actions pipeline:

name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  checks:
    name: Rust checks
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_DATABASE: test_db
          MYSQL_USER: user
          MYSQL_PASSWORD: password
          MYSQL_ROOT_PASSWORD: moot
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Install latest nightly
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
          components: rustfmt, clippy
      - uses: davidB/rust-cargo-make@v1
      - name: Run format
        working-directory: backend/
        run: cargo make format

The following error happens:

2022-02-12T21:43:40.0249440Z ---- tests::test_no_key stdout ----
2022-02-12T21:43:40.0252069Z thread 'tests::test_no_key' panicked at 'Error connecting to ***localhost/test_db', src/db.rs:29:49
2022-02-12T21:43:40.0252453Z stack backtrace:
2022-02-12T21:43:40.0252697Z    0: rust_begin_unwind
2022-02-12T21:43:40.0253068Z              at /rustc/e789f3a3a3d96ebf99b7bbd95011527e5be32a11/library/std/src/panicking.rs:584:5
2022-02-12T21:43:40.0253448Z    1: core::panicking::panic_fmt
2022-02-12T21:43:40.0253817Z              at /rustc/e789f3a3a3d96ebf99b7bbd95011527e5be32a11/library/core/src/panicking.rs:143:14
2022-02-12T21:43:40.0254219Z    2: lib::db::establish_connection::{{closure}}
2022-02-12T21:43:40.0254508Z              at ./src/db.rs:29:49

Not sure why, the database url in logs seems not valid : ***localhost/test_db , it looks like GitHub Actions maybe censored (?) the login/password from logs? Not sure how to check this. The healthcheck passes before running other steps.

Fix

Something like this worked, not sure why, possible reasons might include

name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  checks:
    name: Rust checks
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_DATABASE: test_db
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Verify database exists
        run:  mysql --host 127.0.0.1 --port 3306 -uroot -e "SHOW DATABASES LIKE 'test_db'"

      - name: Install latest nightly
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true
          components: rustfmt, clippy
	....

      - name: Run test
        working-directory: backend/
        run: cargo make test
        env:
          TEST_DATABASE_URL: mysql://[email protected]/test_db
          API_KEY: "123"
          RUST_BACKTRACE: "full"
          INFLUXDB_HOST: http://localhost:8086