【问题标题】:Adding postgresql DB to travis将 postgresql 数据库添加到 travis
【发布时间】:2019-08-06 15:35:03
【问题描述】:

这是我第一次使用任何 CI。我用过特拉维斯。在当前提交中,我添加了postgresql,但使用stacktrace 构建失败(在我的计算机(本地)上它工作正常):(整个stacktrace https://travis-ci.org/must1/BookstoreScraper/builds/568424005?utm_source=github_status&utm_medium=notification

org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.Driver.makeConnection(Driver.java:454) ~[postgresql-42.2.5.jar:42.2.5]
    at org.postgresql.Driver.connect(Driver.java:256) ~[postgresql-42.2.5.jar:42.2.5]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.2.0.jar:na]
    at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:151) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:319) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356) ~[spring-jdbc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:73) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:142) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:112) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$314b4507.CGLIB$jpaVendorAdapter$5(<generated>) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration$$EnhancerBySpringCGLIB$$314b4507$$FastClassBySpringCGLIB$$ae85cd83.invoke(<generated>) ~[spring-boot-autoconfigure-2.1.1.RELEASE.jar:2.1.1.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]

我知道它找不到 postgres,但我不知道如何添加它。

travis.yml配置:

language: java

addons:
  sonarcloud:
    organization: "must1"
    token:
      secure: TOKEN # encrypted value of your token

script:
  # the following command line builds the project, runs the tests with coverage and then execute the SonarCloud analysis
  - mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/bookstore_scraper
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults= false

【问题讨论】:

    标签: java postgresql travis-ci


    【解决方案1】:

    Travis 文档中有一整章介绍了如何设置数据库。

    特别是:

    https://docs.travis-ci.com/user/database-setup/#postgresql

    基本上你将 Postgres 添加到服务中:

    services:
      - postgresql
    

    并创建一个数据库:

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

    如果您需要特定版本的 Postgresql:

    https://docs.travis-ci.com/user/database-setup/#using-a-different-postgresql-version

    如果您需要 Postgresql 11 - 还有一些工作要做(2019-08-07):

    Java 的最小示例 .travis.yml:

    dist: xenial
    sudo: required
    language: java
    
    before_script:
      - psql -c 'create database travis_ci_test' -U postgres
    
    before_install:
      - sudo apt-get update
      - sudo apt-get --yes remove postgresql\*
      - sudo apt-get install -y postgresql-11 postgresql-client-11
      - sudo cp /etc/postgresql/{9.6,11}/main/pg_hba.conf
      - sudo service postgresql restart 11
    
    env:
      global:
        - PGUSER=postgres
        - PGPORT=5432
    
    script:
      - psql -c "SELECT version();" 
    

    上面的例子可以修改为其他语言 - 比如language: node_js

    【讨论】:

    • 我更新了我的答案 - Postgres 11 需要一个支持它的发行版(xenial)
    • 所以我的配置应该如下所示:pastebin.com/zAh59WGm 还是应该包含更多内容?
    • 看起来不错 - Travis 尝试和错误......也许首先将 psql -c "SELECT version();" 放在 script: 部分。它应该打印出使用的服务器版本。当它说“Postgres 11”时 - 那你就走在了一个好地方。
    • 这里是构建:travis-ci.org/must1/BookstoreScraper/builds/568918411 仍然失败 :( 我无法添加 psql -c 'select ersion();' 因为它说无法解析 must1/BookstoreScraper/.travis.yml@e1445dc8b2f569b8
    • 我更新了我的答案。解析错误是由于 .yml 文件的格式错误。可能是复制+粘贴错误...
    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 2015-02-24
    • 2013-08-24
    • 2021-10-23
    • 2018-11-20
    • 2018-12-24
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多