1

I'm doing POC with liquibase docker image,

I would like to run the liquibase docker image in docker with Jenkins kubernetes POD template. unfortunately unable to make it.

And also I have attached the Jenkins file and my observation.

Jenkins File

def workspace_dir = "/home/jenkins/agent/workspace/${env.JOB_BASE_NAME}"
def project_name = "master-chart"
def isDeployerJob = (env.JOB_BASE_NAME).contains("deploy") ? "true" : "false"

// These variables come from the build parameters in the Jenkins job
def git_branch = git_branch
def release_version


if (isDeployerJob == "true") {
    // Extracting the release version from the branch
    def temp = git_branch.split("/")
    release_version = temp[temp.length - 1]

    switch(environment) {
      case "dev":
        hs_jdbc_url="jdbc:postgresql://40.xx.xx.xx:5432/dbname"
        db_username="username"
        db_password="pwd"
        break
      default:
        break
    }
} 

pipeline {

  agent {
    kubernetes {
      cloud 'eks-tools-13'
      yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: azcli-kubectl-helm
    image: internal.docker.cioxhealth.com/azcli-kubectl-helm
    command:
      - cat
    tty: true
  - name: docker
    image: docker
    command:
      - cat
    tty: true
    privileged: true
    volumeMounts:
      - name: dockersock
        mountPath: /var/run/docker.sock
  volumes:
    - name: dockersock
      hostPath:
        path: /var/run/docker.sock
"""
    }
  }

  stages {
        
    stage('Install Database Scripts') {
      when {
        expression {
          "${isDeployerJob}" == "true"
        }
      }
      steps {
        container('docker') { 
            sh """
               docker run --rm --network="host" -v ${workspace_dir}/db:/liquibase/changelog liquibase/liquibase --url=${hs_jdbc_url} --changeLogFile=db.changelog-master.yaml --driver=org.postgresql.Driver --username=${db_username} --password=${db_password} --logLevel=info  update
            """
        }
      }
    }   
  
  }
}

For verifying the files, I have getting into running container

Jenkins Master Node: ls -ltr /home/jenkins/agent/workspace/master-chart-deploy/db

total 4
drwxr-xr-x    3 1000     1000            21 Nov  6 04:35 sql
drwxr-xr-x    3 1000     1000            21 Nov  6 04:35 rollback
drwxr-xr-x    4 1000     1000            35 Nov  6 04:35 migration
-rw-r--r--    1 1000     1000           154 Nov  6 04:35 db-master-changelog.yaml
drwxr-xr-x    2 1000     1000            38 Nov  6 04:35 changelog

Docker container on master-chart-deploy-259-qxrn5-nqq7j-hhlb8

ls -ltr /home/jenkins/agent/workspace/master-chart-deploy/db

total 4
drwxr-xr-x    3 1000     1000            21 Nov  6 04:35 sql
drwxr-xr-x    3 1000     1000            21 Nov  6 04:35 rollback
drwxr-xr-x    4 1000     1000            35 Nov  6 04:35 migration
-rw-r--r--    1 1000     1000           154 Nov  6 04:35 db-master-changelog.yaml
drwxr-xr-x    2 1000     1000            38 Nov  6 04:35 changelog

Liquibase Container

docker run --rm '--network=host' -v /home/jenkins/agent/workspace/master-chart-deploy/db:/liquibase/changelog liquibase/liquibase -- ls -ltr /liquibase/changelog

total 0

Files are not available in the liquibase running container. due this the following error has been occurred.

Error:

Starting Liquibase at 14:50:38 (**version 4.1.1** #10 built at 2020-10-12 19:24+0000)
[2020-11-05 14:50:38] INFO [liquibase.lockservice] Successfully acquired change log lock
[2020-11-05 14:50:38] INFO [liquibase.lockservice] Successfully released change log lock
Unexpected error running Liquibase: db-master-changelog.yaml does not exist
For more information, please use the --logLevel flag
[2020-11-05 14:50:38] SEVERE [liquibase.integration] Unexpected error running Liquibase: db-master-changelog.yaml does not exist
liquibase.exception.ChangeLogParseException: db-master-changelog.yaml does not exist
    at liquibase.parser.core.yaml.YamlChangeLogParser.parse(YamlChangeLogParser.java:27)
    at liquibase.Liquibase.getDatabaseChangeLog(Liquibase.java:337)
    at liquibase.Liquibase.lambda$update$1(Liquibase.java:229)
    at liquibase.Scope.lambda$child$0(Scope.java:160)
    at liquibase.Scope.child(Scope.java:169)
    at liquibase.Scope.child(Scope.java:159)
    at liquibase.Scope.child(Scope.java:138)
    at liquibase.Liquibase.runInScope(Liquibase.java:2277)
    at liquibase.Liquibase.update(Liquibase.java:215)
    at liquibase.Liquibase.update(Liquibase.java:201)
    at liquibase.integration.commandline.Main.doMigration(Main.java:1760)
    at liquibase.integration.commandline.Main$1.lambda$run$0(Main.java:361)
    at liquibase.Scope.lambda$child$0(Scope.java:160)
  • May I know, What did I do wrong in this case? and why files are not available in liquibase running container?
  • Is this a problem, because of file permissions due to Docker in Docker case?
  • Is there any other way I can achieve this?

Thank you in advance for the help.

Ramakrishnan M
  • 482
  • 1
  • 6
  • 16

3 Answers3

0

I think you are somehow messing with docker configuration. From documentation it looks like liquibase expects that you mount everything inside /liquibase/changelog directory.

And in your command you are mapping your changelogs to /app/liquibase:

docker run --rm --network="host" -v ${workspace_dir}/db:/app/liquibase liquibase/liquibase --url=${hs_jdbc_url} --changeLogFile=db.changelog-master.yaml --classpath=/app/liquibase --driver=org.postgresql.Driver --username=${db_username} --password=${db_password} --logLevel=info  update

so instead of that I'd use this:

docker run --rm --network="host" -v ${workspace_dir}/db:/liquibase/changelog liquibase/liquibase --url=${hs_jdbc_url} --changeLogFile=db.changelog-master.yaml  --driver=org.postgresql.Driver --username=${db_username} --password=${db_password} --logLevel=info  update

note: I've removed --classpath=/app/liquibase if you rely on it, if you've added some additional driver or something else you should probably include it again, but try to read about it at first. I think the documentation is pretty good.

bilak
  • 4,526
  • 3
  • 35
  • 75
  • thanks for your feedback. Now As per documentation I have changed. But issue not resolved and Files are not available in the `liquibase` running container volume – Ramakrishnan M Nov 06 '20 at 15:10
  • and what kind of issue do you have currently? Try to update your question so we can help you more. – bilak Nov 06 '20 at 15:39
  • Mentioned error I'm encountering while running the build : `Unexpected error running Liquibase: db-master-changelog.yaml does not exist liquibase.exception.ChangeLogParseException: db-master-changelog.yaml does not exist` and same i have updated in my question as well – Ramakrishnan M Nov 06 '20 at 15:43
  • Even if I mount a Directory as a Volume, I am unable to view the files in the resulting container. Is this a problem, because of file permissions due to Docker in Docker case? – Ramakrishnan M Nov 09 '20 at 03:46
0

you must specify full path when you use docker run in jekins pipeline:

--changeLogFile=/app/liquibase/db.changelog-master.yaml 

Define in jenkins pipeline :

environment {
        HOME = '.'
    }
Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53
  • Getting the error Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter. And here problem is files are not file in the resulting container – Ramakrishnan M Nov 09 '20 at 13:30
  • 1
    @RamakrishnanM "Please use a relative path or add '/' to the classpath parameter" what does this mean? please, provide example, not description. – Simon Logic Jul 13 '21 at 20:21
0

Change log file is the main point from where Liquibase looks for configuration. If we do not define change log file path in Spring Boot, it considers db/changelog/db.changelog-master.yaml as default path for YAML format. As we will go with XML format, we need to set spring.liquibase.change-log=classpath:/db/changelog/changelog-master.xml for change log file path in application.properties file. You can set logging level of liquibase logs by setting log level in logging.level.liquibase property. Other properties in given below properties file are for H2 database configuration.

  • As your answer is currently written, it is hard to read. Please check the [help center](https://stackoverflow.com/editing-help) for info on how to format answers. – Tyler2P Nov 26 '21 at 17:44