设置数据库和服务

本指南介绍了如何在 Travis CI 环境中设置最受欢迎的数据库和其他服务。

您可以查看您正在使用的构建环境中数据库和服务的可用性 这里.

所有服务都使用默认设置,除了添加了一些用户并放宽了安全设置。

启动服务 #

Travis CI 环境默认情况下不会启动服务,以便为构建脚本提供更多 RAM。通过将服务添加到 services: 部分的 .travis.yml 中来启动服务。

services: mongodb

如果您在 addons: 部分安装了服务,例如 MariaDB,则无需将其也添加到 services: 部分。

启动多个服务

services:
  - riak
  - rabbitmq
  - memcached

如果您手动下载并安装服务,则还必须在 before_install 步骤中启动它。 services 键仅适用于我们配置的服务。

MySQL #

在您的 .travis.yml 中启动 MySQL

services:
  - mysql

MySQL 绑定到 127.0.0.1~travis/.my.cnf 中定义的套接字,并且需要身份验证。您可以使用用户名 travisroot 和空密码进行连接。

请注意, travis 用户没有 root 用户具有的更高权限。

  Ubuntu Precise Ubuntu Trusty Ubuntu Xenial Ubuntu Bionic Ubuntu Focal Ubuntu Jammy
MySQL 5.5.x 5.6.x 5.7.x 5.7.x 8.0.x 8.0.x

您也可以 在 Ubuntu Trusty 上安装 MySQL 5.7.

使用 ActiveRecord 的 MySQL #

config/database.yml 使用 ActiveRecord 的 Ruby 项目示例

test:
  adapter: mysql2
  database: myapp_test
  username: travis
  encoding: utf8

您可能需要先创建 myapp_test 数据库,例如在 .travis.yml 中的 before_install 步骤中

before_install:
  - mysql -e 'CREATE DATABASE myapp_test;'

关于 test 数据库的说明 #

在旧版本的 MySQL 中,Ubuntu 包默认提供 test 数据库。由于安全问题,从版本 5.5.37 开始,这种情况不再存在(参见 更改日志)。

如果需要,可以创建 test 数据库,例如在 .travis.yml 中的 before_install 步骤中

before_install:
  - mysql -e 'CREATE DATABASE IF NOT EXISTS test;'

MySQL 5.7 #

MySQL 5.7 是 Xenial (dist: xenial) 和 Bionic (dist: bionic) 镜像上的默认版本。

从 2019 年 7 月 21 日起,MySQL 5.7 不再支持 Ubuntu Trusty (14.04)。请参见 MySQL 产品支持 EOL 公告MySQL 论坛中的这篇文章

PostgreSQL #

在您的 .travis.yml 中启动 PostgreSQL

services:
  - postgresql

在构建中使用 PostgreSQL #

访问本地 PostgreSQL 服务器的默认用户是 postgres,密码为空。

通过在您的 .travis.yml 中添加一行来为您的应用程序创建一个数据库

before_script:
  - psql -c 'create database travis_ci_test;' -U postgres

对于 Rails 应用程序,您现在可以使用以下 database.yml 配置来访问本地数据库

test:
  adapter: postgresql
  database: travis_ci_test

如果您的本地测试设置使用不同的凭据或设置来访问本地测试数据库,建议将这些设置放在存储库中的 database.yml.travis 中,并将它们复制到构建中。

before_script:
  - cp config/database.yml.travis config/database.yml

使用不同的 PostgreSQL 版本 #

Travis CI 构建环境在 Trusty 镜像上默认使用 9.2 版本,但官方 PostgreSQL APT 存储库 中的许多其他版本也可用。要使用非默认版本,请在您的 .travis.yml 中仅指定 **major.minor** 版本

addons:
  postgresql: "9.4"

许多 PostgreSQL 版本已预先安装在我们的构建环境中,其他版本可以通过使用 postgresqlapt 插件以及对 PGPORTPGUSER 的全局环境变量覆盖在构建时添加和激活。

addons:
  postgresql: "11"
  apt:
    packages:
    - postgresql-11
    - postgresql-client-11
env:
  global:
  - PGPORT=5433
  - PGUSER=travis

在 Xenial 镜像中,Postgres 9.4 到 9.6 仅需指定版本,默认使用 postgres 用户和默认端口 5432。

对于 PostgreSQL 10,您必须指定要安装的包,用户是 postgres,端口是 5432。对于 PostgreSQL 11 和 12,您必须指定包,但用户是 travis,端口是 5433。因此您必须指定 PGPORT

使用 PostGIS #

安装与您的 PostgreSQL 版本匹配的 PostGIS 版本,并使用以下命令激活 PostGIS 扩展

addons:
  postgresql: 9.6
  apt:
    packages:
    - postgresql-9.6-postgis-2.3
before_script:
  - psql -U postgres -c "create extension postgis"

PostgreSQL 和语言环境 #

Travis CI 构建环境预先安装了许多语言环境,但您也可以根据需要安装其他语言环境。

安装语言环境 #

以下示例显示了您需要添加到 .travis.yml 中的代码行以安装西班牙语语言包。

请注意,您需要从 .travis.yml 的 addons 部分中删除 PostgreSQL 版本

before_install:
  - sudo apt-get update
  - sudo apt-get install language-pack-es
  - sudo /etc/init.d/postgresql stop
  - sudo /etc/init.d/postgresql start 9.3

使用 pg_config #

如果您的构建依赖于 pg_config 命令,则需要安装另一个 apt 包 postgresql-server-dev-X.Y,其中 X.Y 与您使用的 PostgreSQL 版本匹配。

例如

addons:
  postgresql: '9.4'
  apt:
    packages:
      - postgresql-server-dev-9.4

有关更多详细信息,请参见 此 GitHub 问题

MariaDB #

MariaDB 是 MySQL 的社区开发分支。它在 Travis CI 上作为插件提供。

要使用 MariaDB,请在您的 .travis.yml 中指定要使用的“major.minor”版本。版本在 MariaDB 网站 上列出。

addons:
  mariadb: '10.0'

版本号作为环境变量 TRAVIS_MARIADB_VERSION 导出。

SQLite3 #

最简单最容易的关联数据库。

Ruby 项目中的 SQLite3 #

将 sqlite3 ruby 绑定添加到您的 bundle 中

# Gemfile
# for CRuby, Rubinius, including Windows and RubyInstaller
gem "sqlite3", :platform => [:ruby, :mswin, :mingw]

# for JRuby
gem "jdbc-sqlite3", :platform => :jruby

如果您使用 ActiveRecord,请将以下内容添加到您的 config/database.yml

test:
  adapter: sqlite3
  database: ":memory:"
  timeout: 500

或者,如果您没有使用 config/database.yml,请手动连接到数据库。

ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
                                        :database => ':memory:'

MongoDB #

在您的 .travis.yml 中启动 MongoDB。

services:
  - mongodb

MongoDB 绑定到 127.0.0.1,并且不需要预先进行身份验证或数据库创建。如果您添加了 admin 用户,则会启用身份验证,因为 mongod 是使用 --auth 参数启动的。

注意:管理员用户是在管理员数据库中创建的用户。

要为您的数据库创建用户,请在您的 .travis.yml 中添加一个 before_script 部分。

before_script:
  - mongo mydb_test --eval 'db.createUser({user:"travis",pwd:"test",roles:["readWrite"]});'

MongoDB 不会立即接受连接 #

一些用户报告说,MongoDB 在从构建脚本中连接时不会接受连接。

该问题是间歇性的,唯一可靠的避免该问题的方法是在进行第一次连接之前注入人工等待。

在您的 .travis.yml 中添加以下 before_script,在连接到 MongoDB 之前等待。

before_script:
  - sleep 15
  - mongo mydb_test --eval 'db.createUser({user:"travis",pwd:"test",roles:["readWrite"]});'

CouchDB #

在您的 .travis.yml 中启动 CouchDB。

services:
  - couchdb

CouchDB 绑定到 127.0.0.1,在 dist:xenial 及更早的 Linux 发行版上使用默认配置,并且不需要身份验证(在 CouchDB 术语中,它在管理员模式下运行)。

但是,对于 bionic,需要使用用户名 admin 和密码 travis 进行身份验证,例如 curl -X PUT http://admin:travis@localhost:5984/<db_name>

在使用 CouchDB 之前,您需要在构建过程中创建数据库。

before_script:
  - curl -X PUT localhost:5984/myapp_test

RabbitMQ #

RabbitMQ 需要 setuid 标志,因此您只能在 macOS 或 Ubuntu Trusty 基础设施上作为服务运行 RabbitMQ。

在您的 .travis.yml 中启动 RabbitMQ。

services:
  - rabbitmq

RabbitMQ 使用默认配置。

  • 虚拟主机:/
  • 用户名:guest
  • 密码:guest

您可以在 .travis.ymlbefore_script 部分中设置更多虚拟主机和角色。

RabbitMQ 可以在 Ubuntu Xenial 上使用 .travis.yml 中的 APT 插件启动。

addons:
  apt:
    packages:
    - rabbitmq-server 

Riak #

Riak 仅在 Ubuntu Trusty 环境 中可用。

在您的 .travis.yml 中启动 Riak。

services:
  - riak

Riak 使用默认配置,并将 Bitcask 作为存储后端。

默认情况下,Riak Search 被停用。

Memcached #

在您的 .travis.yml 中启动 Memcached 服务。

services:
  - memcached

Memcached 使用默认配置并绑定到本地主机。

Redis #

在您的 .travis.yml 中启动 Redis。

services:
  - redis-server

Redis 使用默认配置,并且在本地主机上可用。

Cassandra #

在您的 .travis.yml 中启动 Cassandra。

services:
  - cassandra

Cassandra 从 Apache apt 存储库 下载,并使用默认配置。它在 127.0.0.1 上可用。

安装旧版本的 Cassandra #

使用以下示例在您的 .travis.yml 中安装特定旧版本的 Cassandra。

before_install:
  - sudo rm -rf /var/lib/cassandra/*
  - wget http://www.us.apache.org/dist/cassandra/1.2.18/apache-cassandra-1.2.18-bin.tar.gz && tar -xvzf apache-cassandra-1.2.18-bin.tar.gz && sudo sh apache-cassandra-1.2.18/bin/cassandra

Neo4j #

在您的 .travis.yml 中启动 Neo4j。

services:
  - neo4j

Neo4j Server 使用默认配置,并绑定到本地主机上的端口 7474。

ElasticSearch #

在您的 .travis.yml 中启动 ElasticSearch。

services:
  - elasticsearch

ElasticSearch 启动需要几秒钟,为了确保它在构建脚本运行时可用,请在您的构建脚本中添加一个小延迟。

before_script:
  - sleep 10

ElasticSearch 使用默认配置,并且在 127.0.0.1 上可用。

安装特定版本的 ElasticSearch #

您可以使用以下方法覆盖已安装的 ElasticSearch,使用您需要的版本(例如,7.6.2)。

before_install:
  - curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-amd64.deb -o elasticsearch.deb
  - sudo dpkg -i --force-confnew elasticsearch.deb
  - sudo chown -R elasticsearch:elasticsearch /etc/default/elasticsearch
  - sudo service elasticsearch restart

我们建议您在 ElasticSearch 网站 上验证下载 URL 的有效性。

构建日志中的截断输出 #

当 ElasticSearch 启动时,您可能会看到一个截断的错误消息,例如

$ sudo service elasticsearch start
 * Starting ElasticSearch Server       ission denied on key 'vm.max_map_count'

这是由于 ElasticSearch 中的最新更改 造成的,正如 这里 所报告的那样。该消息无害,服务是可用的。

RethinkDB #

要在 Travis CI 中使用 RethinkDB,请在 .travis.yml 配置文件中将其列为插件,并指定版本号作为字符串。

addons:
  rethinkdb: '2.3.4'

如果您指定了部分版本号,则插件将安装并运行与之匹配的最新版本。例如,'2.3' 将与 2.3.x 行中的最新 RethinkDB 版本匹配。

导出两个环境变量。

  • TRAVIS_RETHINKDB_VERSION 是在配置中指定的版本(例如,'2.3.4''2.3')。
  • TRAVIS_RETHINKDB_PACKAGE_VERSION 是安装的包的完整版本(例如,'2.3.4+1~0precise')。

启用后,RethinkDB 将在默认端口 (28015) 上的 localhost 上启动。

多个数据库构建 #

如果您需要运行多个使用不同数据库的构建,您可以配置环境变量以及 before_scriptbefore_install 行来创建构建矩阵。

使用环境变量和 before_script 步骤 #

使用 DB 环境变量来指定数据库配置的名称。在本地,您将运行

DB=postgres [commands to run your tests]

在 Travis CI 上,您想要创建一个 构建矩阵,其中包含三个构建,每个构建都导出具有不同值的 DB 变量,为此,您可以使用 .travis.yml 中的 env 选项。

env:
  - DB=sqlite
  - DB=mysql
  - DB=postgres

然后,您可以使用这些值在 before_install(或 before_script)步骤中设置每个数据库。例如

before_script:
  - sh -c "if [ '$DB' = 'postgres' ]; then psql -c 'DROP DATABASE IF EXISTS tests;' -U postgres; fi"
  - sh -c "if [ '$DB' = 'postgres' ]; then psql -c 'DROP DATABASE IF EXISTS tests_tmp;' -U postgres; fi"
  - sh -c "if [ '$DB' = 'postgres' ]; then psql -c 'CREATE DATABASE tests;' -U postgres; fi"
  - sh -c "if [ '$DB' = 'postgres' ]; then psql -c 'CREATE DATABASE tests_tmp;' -U postgres; fi"
  - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS tests_tmp; CREATE DATABASE IF NOT EXISTS tests;'; fi"

Travis CI 没有对这些变量的任何特殊支持,它只是创建了三个导出不同值的构建。您的构建脚本以及 before_installbefore_script 步骤负责使用这些变量。

有关实际示例,请参阅 doctrine/doctrine2 .travis.yml

使用 Ruby #

另一种方法是将所有数据库配置放在一个 YAML 文件中(例如,test/database.yml),就像 ActiveRecord 所做的那样。

sqlite:
  adapter: sqlite3
  database: ":memory:"
  timeout: 500
mysql:
  adapter: mysql2
  database: myapp_test
  username:
  encoding: utf8
postgres:
  adapter: postgresql
  database: myapp_test
  username: postgres

然后,在您的测试套件中,将这些数据读入一个配置哈希中。

configs = YAML.load_file('test/database.yml')
ActiveRecord::Base.configurations = configs

db_name = ENV['DB'] || 'sqlite'
ActiveRecord::Base.establish_connection(db_name)
ActiveRecord::Base.default_timezone = :utc