Skip to main content

Use Services

A service runs alongside a job and is reachable by its service name. This example starts a disposable MySQL database for a job running with a Docker executor.

  1. Under Settings > Build > Job Secrets, define db-password with a test database password and authorize the branch used for the build. See Use Job Secrets.

  2. In the build specification's Services tab, define a service named mysql with image mysql:8.4. Add environment variables MYSQL_ROOT_PASSWORD with value @secret:db-password@ and MYSQL_ROOT_HOST with value %. Set Readiness Check Command to mysqladmin ping -h 127.0.0.1 --silent.

    MySQL service definition

  3. In the job's Dependencies & Services section, add mysql to Required Services.

    Required service for the job

  4. Add an Execute Commands step using image mysql:8.4 and a POSIX shell. Run:

    set -eu
    export MYSQL_PWD='@secret:db-password@'
    mysql -h mysql -u root -e 'SELECT 42 AS tutorial_result'
  5. Commit and run the job. The log should show that the service is ready, followed by tutorial_result and 42 from the query. The hostname mysql matches the service name; do not use localhost to reach the service from the job container.

The root account and host wildcard above are for this disposable test database. Configure an application account and appropriate permissions for your own database tests.

Kubernetes Service Placement​

If the job runs with a Kubernetes executor and you want the MySQL service to run on particular nodes, configure the service locator in More Settings of the executor:

Service locator