2017-09-13 52 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配置文件。 –

相關問題