# Test a Go service with a database

> Run a Go service's tests against a MySQL database that runs beside the job.
> **Available** in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its `.forge-ci.yaml` or `.xeonr-ci.yaml`. See [What is available](https://docs.branchd.run/branchd/pipelines#what-is-available).

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# Test a Go service against a MySQL database that runs beside the job.
name: CI
on: [push, pull_request]
jobs:
  test:
    image: golang:1.26
    services:
      mysql:
        image: mysql:8
        env:
          MYSQL_ROOT_PASSWORD: test
          MYSQL_DATABASE: app
    env:
      # The service's alias, mysql, is its host name.
      TEST_MYSQL_DSN: root:test@tcp(mysql:3306)/app?parseTime=true
    steps:
      - name: Wait for MySQL
        run: |
          for i in $(seq 1 60); do
            (echo > /dev/tcp/mysql/3306) 2>/dev/null && exit 0
            sleep 1
          done
          echo "MySQL did not start" >&2
          exit 1
        shell: bash
      - run: go vet ./...
      - name: Test
        run: go test -race -count=1 ./...
```

- **`services:`** starts each listed image beside the job, for the job's
  whole life. The key, `mysql`, is the service's host name: the tests connect
  to `mysql:3306`. A service is an image, or
  `{image, env, command, entrypoint, ports}`.
- **`env:`** on the job gives every step `TEST_MYSQL_DSN`.
- **Waiting.** A service is started with the job, not waited for. The first
  step polls the port until MySQL answers. It uses bash's `/dev/tcp`, so it
  says `shell: bash`; the job's other steps run in the job's shell (bash when
  the image has it, else sh).
- **`name:`** is what the log shows for a step; without it the log shows the
  command.

To try another database, change `image:` under the service; to test against
several, see [Matrix](https://docs.branchd.run/branchd/pipelines/matrix).

Source: https://docs.branchd.run/branchd/pipelines/test-go-with-database
