简体   繁体   English

如何在 Jenkins 容器化 docker 环境中运行 liquibase docker 镜像

[英]How to run the liquibase docker image in Jenkins containerized docker environment

I'm doing POC with liquibase docker image,我正在用 liquibase docker 镜像做 POC,

I would like to run the liquibase docker image in docker with Jenkins kubernetes POD template.我想使用 Jenkins kubernetes POD 模板在liquibase运行liquibase镜像。 unfortunately unable to make it.不幸的是无法做到。

And also I have attached the Jenkins file and my observation.我还附上了 Jenkins 文件和我的观察。

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詹金斯主节点: 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 master-chart-deploy-259-qxrn5-nqq7j-hhlb8 上的 Docker 容器

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 Liquibase 容器

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

total 0共 0

Files are not available in the liquibase running container. liquibase运行容器中的文件不可用。 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?为什么liquibase运行容器中没有文件?
  • Is this a problem, because of file permissions due to Docker in Docker case?这是一个问题,因为在 Docker 情况下由于 Docker 的文件权限?
  • Is there any other way I can achieve this?有没有其他方法可以实现这一目标?

Thank you in advance for the help.预先感谢您的帮助。

I think you are somehow messing with docker configuration.我认为你在某种程度上搞乱了 docker 配置。 From documentation it looks like liquibase expects that you mount everything inside /liquibase/changelog directory.文档看来,liquibase 期望您将所有内容都安装在/liquibase/changelog目录中。

And in your command you are mapping your changelogs to /app/liquibase :在您的命令中,您将更改日志映射到/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.注意:如果你依赖它,我已经删除了--classpath=/app/liquibase ,如果你添加了一些额外的驱动程序或其他东西,你可能应该再次包含它,但首先尝试阅读它。 I think the documentation is pretty good.我认为文档非常好。

you must specify full path when you use docker run in jekins pipeline:在 jekins 管道中使用 docker docker run时必须指定完整路径:

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

Define in jenkins pipeline :在詹金斯管道中定义:

environment {
        HOME = '.'
    }

Change log file is the main point from where Liquibase looks for configuration.更改日志文件是 Liquibase 查找配置的主要点。 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.如果我们在 Spring Boot 中没有定义更改日志文件路径,它会将 db/changelog/db.changelog-master.yaml 视为 YAML 格式的默认路径。 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.由于我们将使用 XML 格式,我们需要在 application.properties 文件中为更改日志文件路径设置 spring.liquibase.change-log=classpath:/db/changelog/changelog-master.xml。 You can set logging level of liquibase logs by setting log level in logging.level.liquibase property.您可以通过在 logging.level.liquibase 属性中设置日志级别来设置 liquibase 日志的日志级别。 Other properties in given below properties file are for H2 database configuration.下面给出的属性文件中的其他属性用于 H2 数据库配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM