Setup Flyway,Postgres in Jenkins using Docker

  • Recently I was trying to setup Flyway on Jenkins for my db in Postgres.
  • The required files can be found here
  • The basic files you will need will be 
    • The configuration file in conf folder
    • The sql files in the sql folder
    • The pipeline file "jenkinsfile-2"
  • Here is the sample configuration file
    •  flyway.url= jdbc:postgresql://localhost:5432/flyway  
       flyway.user = postgres  
       flyway.password = postgres  
       flyway.schemas= flyway  
       flyway.locations=filesystem:../sql  
       flyway.driver=org.postgresql.Driver  
      
  • Create DB Credentials in Jenkins with ID  "db-creds"
    • We are using this in our sample you may change it but be sure to change it in jenkins pipeline file too.
  • Here is the sample jenkins pipeline file
  • My postgres is installed on localhost so I am using "--network=host" in my docker command for example
    •  docker run --rm --network=host -v /var/lib/jenkins/workspace/flyway/jenkins_flyway/sql:/flyway/sql -v /var/lib/jenkins/workspace/flyway/jenkins_flyway/conf:/flyway/conf flyway/flyway:8.5.1 -user=postgres -password=postgres migrate  
      
    • If your postgres is running from a docker container you may not use this.
    • Also if you have mapped 5432 in PostgreSQL to 5432 on the host then you should use this.
  • Issues Faced
    • The first Issue I faced was ERROR: No database found to handle "jdbc:postgresql://localhost:5432/mydb"
      • To rectify this make sure you have written the configuration file exactly as above. I was using values in quotes.
    • The second Issue I faced was Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
      • To rectify this I modified following properties in my postgresql.conf file
        • listen_addresses = '*'
      • And added following line in my postgres pg_hba.conf file
        • host    all             all             192.168.1.0/24          md5
    • Jenkins is not able to access Docker
      • Add Jenkins user to docker group using following command
        • sudo usermod -a -G docker jenkins
    • Permission issues
      • sudo usermod -a -G usergroup jenkins
        • add jenkins to your usergroup so that it can access your workspace
      • sudo usermod -a -G usergroup docker
        • add docker to your usergroup so that you can execute docker without sudo permission from jenkins.

Comments

Popular posts from this blog

Docker

Jenkins