2017-09-13 53 views
7

我有一个Spring Boot应用程序,目前在Heroku的CI中构建和运行测试,我也试图让它在Circle CI中工作。我的配置文件看起来像这样:在CircleCi中从Spring Boot访问PostgreSQL 9.6

version: 2 
jobs: 
    build: 
    docker: 
     - image: circleci/jdk8:0.1.1 
     - image: postgres:9.6 
    working_directory: ~/repo 

    environment: 
     # Customize the JVM maximum heap limit 
     JVM_OPTS: -Xmx3200m 
     TERM: dumb 

    steps: 
     - checkout 
     - run: chmod +x gradlew 

     # Download and cache dependencies 
     - restore_cache: 
      keys: 
      - v1-dependencies-{{ checksum "build.gradle" }} 
      # fallback to using the latest cache if no exact match is found 
      - v1-dependencies- 

     - run: ./gradlew dependencies 

     - save_cache: 
      paths: 
      - ~/.m2 
      key: v1-dependencies-{{ checksum "build.gradle" }} 

     # run tests! 
     - run: ./gradlew test 

我试图定义DATABASE_URL没有效果的多种方法:

jobs: 
    build: 
    docker: 
     - image: circleci/jdk8:0.1.1 
     environment: 
     - DATABASE_URL=postgresql://[email protected]:5433/dashman_test 
     - image: postgres:9.6 
     environment: 
     - POSTGRES_USER=dashman_test 
     - POSTGRES_DB=dashman_test 

jobs: 
    build: 
    docker: 
     - image: circleci/jdk8:0.1.1 
     environment: 
     - DATABASE_URL=postgresql://[email protected]:5434/dashman_test 
     - image: postgres:9.6 
     environment: 
     - POSTGRES_USER=dashman_test 
     - POSTGRES_DB=dashman_test 

jobs: 
    build: 
    docker: 
     - image: circleci/jdk8:0.1.1 
     environment: 
      DATABASE_URL: postgresql://[email protected]:5434/dashman_test 
     - image: postgres:9.6 
     environment: 
      POSTGRES_USER: dashman_test 
      POSTGRES_DB: dashman_test 


TEST_DATABASE_URL: postgresql://[email protected]/circle_test?sslmode=disable   
DATABASE_URL: postgresql://[email protected]/circle_test?sslmode=disable 

DATABASE_URL: postgres://ubuntu:@127.0.0.1:5433/circle_test 

DATABASE_URL: postgres://localhost:5433/dashman_test 

DATABASE_URL: postgresql://[email protected]:5434/circle_test?sslmode=disable 

DATABASE_URL: postgres://dashman_test:[email protected]:5433/dashman_test 

似乎没有任何工作,我总是最后与此错误:

tech.dashman.dashmanserver.models.AccountTest > create FAILED 
    java.lang.IllegalStateException 
     Caused by: org.springframework.beans.factory.BeanCreationException 
      Caused by: org.springframework.beans.BeanInstantiationException 
       Caused by: org.springframework.beans.factory.BeanCreationException 
        Caused by: org.springframework.beans.BeanInstantiationException 
         Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException 

tech.dashman.dashmanserver.models.UserTest > create FAILED 
    java.lang.IllegalStateException 
     Caused by: org.springframework.beans.factory.BeanCreationException 
      Caused by: org.springframework.beans.BeanInstantiationException 
       Caused by: org.springframework.beans.factory.BeanCreationException 
        Caused by: org.springframework.beans.BeanInstantiationException 
         Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException 

tech.dashman.dashmanserver.DashmanserverApplicationTests > contextLoads FAILED 
    java.lang.IllegalStateException 
     Caused by: org.springframework.beans.factory.BeanCreationException 
      Caused by: org.springframework.beans.BeanInstantiationException 
       Caused by: org.springframework.beans.factory.BeanCreationException 
        Caused by: org.springframework.beans.BeanInstantiationException 
         Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException 

什么是配置数据库的正确方法?我有点失落。

+0

什么是您的DATABASE_URL? – StanislavL

+0

@StanislavL:我添加了我试过的每个DATABASE_URL的列表。那是你问的吗? – Pablo

+0

难道它不是postgree在码头集装箱中运行,因此而不是本地主机,您需要容器IP(或名称)? – StanislavL

回答

8

这里有几点可以帮助你解决这个问题。


(1)你的意见(this one)中提到的文件是过期或纯误导。这:

The default user, port, test database for PostgreSQL 9.6 are as follows: postgres://ubuntu:@127.0.0.1:5432/circle_test

...是不是真的

postgres:9.6实际的默认值是:

  • 用户名:Postgres的
  • 密码:<empty>
  • 端口:5432
  • 数据库:Postgres的

您可以从到达的Postgres实例你的申请127.0.0.1

你可以找到更多关于默认here信息,但有一个关于设置他们(关于此更多在(3))。


(2)据我所知在Postgres的连接器JDBC URL there is no way to pass usrename\password,所以你可能要告诉你的应用,不仅DATABASE_URL也像DATABASE_USERDATABASE_PASSWORD

这部分是依赖于应用程序的细节,但对于默认的数据库设置典型的春季启动应用程序要与下面的设置来结束:

spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres 
spring.datasource.username=postgres 
spring.datasource.password= 

(3)或者,如果你的连接设置是硬编码的,你可能想为postgres实例配置凭证。

不幸的是,即使设置POSTGRES_*环境变量与docker run运行容器时正常工作,将它们设置在.circleci/config.yml不起作用。有几个开放的bug报告(1,2)描述这个或类似的问题,我的钱就是这个bug。

幸运的是,您仍然可以通过在生成过程中安装psql并创建所需的用户凭据(默认凭据仍然有效)来解决此问题。添加类似:

- run: apt-get update -qq && apt-get install -y postgresql 
    - run: 
     command: | 
     psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE databasename;" 
     psql -h 127.0.0.1 -U postgres -c "CREATE USER username WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE databasename TO username;" 

...您steps应该做的伎俩(见full example here)。

使用machine executor手动运行postgres(请参阅this page上的最后一个示例)也可能是一个选项,但我自己并没有尝试过这一个。


(4)我真的试图配置此为我自己,你可以用working version here退房回购。建立output example here

我用这个spring boot sample,并使其使用postgres,只留下相关的测试,添加圈ci以及其他小的调整。它演示了通过env配置应用程序。变量和配置postgres实例。

最有趣的部分是.circleci/config.yml(其中凭据ENV变量定义和用户\ DB获取Postgres的实例创建的。):

 
version: 2 
jobs: 
    build: 
    docker: 
     - image: maven:3.5.0-jdk-8 
     environment: 
      DATABASE_URL: jdbc:postgresql://127.0.0.1:5432/databasename 
      DATABASE_USER: username 
      DATABASE_PASSWORD: password 
     - image: postgres:9.6 

    working_directory: ~/repo 

    steps: 
     - checkout 
     - run: apt-get update -qq && apt-get install -y postgresql 
     - run: 
      command: | 
      psql -h 127.0.0.1 -U postgres -c "CREATE DATABASE databasename;" 
      psql -h 127.0.0.1 -U postgres -c "CREATE USER username WITH PASSWORD 'password'; GRANT ALL PRIVILEGES ON DATABASE databasename TO username;" 
- run: mvn test 

...和application.properties(其中凭据ENV变量使用) :

 
spring.h2.console.enabled=false 

logging.level.org.hibernate.SQL=error 

spring.datasource.url=${DATABASE_URL} 
spring.datasource.username=${DATABASE_USER} 
spring.datasource.password=${DATABASE_PASSWORD} 
spring.jpa.hibernate.ddl-auto=create-drop 
spring.jpa.database=POSTGRESQL 
spring.datasource.platform=postgres 
spring.jpa.show-sql=true 
spring.database.driverClassName=org.postgresql.Driver 
+0

谢谢。这个例子真的有帮助,特别是你需要如何设置属性文件,这是我错过了。我会将它添加到答案中(以防例子被删除)。 – Pablo

+0

@Pablo现在没有计划删除它,但是,好主意。我添加了属性文件以及我在示例中使用的circleci配置文件。 –

相关问题