java 在 Jenkins 中运行自定义数据库脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11185380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:07:21  来源:igfitidea点击:

Run custom database scripts in Jenkins

javadatabasejenkins

提问by John Doe

So far I've seen different options and jobs in Jenkins that were related to the build process. I wonder if there is a way to create a job that would connect to the database and run a script, that would perform some actions. Maybe, Jenkins could run a script file or just store some actions to run. Thanks in advance.

到目前为止,我已经在 J​​enkins 中看到了与构建过程相关的不同选项和工作。我想知道是否有一种方法可以创建一个连接到数据库并运行脚本的作业,该脚本将执行一些操作。也许,Jenkins 可以运行一个脚本文件或者只是存储一些要运行的操作。提前致谢。

采纳答案by Jens Schauder

You have two major options:

您有两个主要选择:

  • use the Execute shellor Execute Windows batch commandbuild steps

  • use a java based tool like liquibase, ant tasks, maven plugin or many more. You might even make it part of JUnit tests. Consider searching for 'database migration java'

  • 使用执行 shell执行 Windows 批处理命令构建步骤

  • 使用基于 Java 的工具,如 liquibase、ant 任务、maven 插件或更多。您甚至可以将其作为 JUnit 测试的一部分。考虑搜索“数据库迁移 java”

Which of those is the 'correct' solution depends on the purpose of the build:

其中哪一个是“正确”的解决方案取决于构建的目的:

Do you need the databases for some tests? Then you probably should go with a java based solution, because it is platform independent and will most likely work on any OS.

您需要数据库进行某些测试吗?那么您可能应该使用基于 Java 的解决方案,因为它是独立于平台的,并且很可能适用于任何操作系统。

Do you want to actually test the database scripts before you hand them of to the admins for deployment? Then you probably want use the exact tool the admins going to use and execute it on the OS the admins use. Most likely they aren't very keen to use a java based tool for the job.

在将数据库脚本交给管理员进行部署之前,您是否想实际测试它们?那么您可能想要使用管理员将要使用的确切工具并在管理员使用的操作系统上执行它。很可能他们不太热衷于使用基于 Java 的工具来完成这项工作。

回答by i.am.michiel

Out of the box, there is no database plugin or job in Jenkins.

开箱即用,Jenkins 中没有数据库插件或作业。

The easiest way to do this would be a Ant task. There is native support for ant tasks in Jenkins. Checkout the SQLTaskof Ant.

执行此操作的最简单方法是 Ant 任务。在 Jenkins 中有对 ant 任务的原生支持。查看Ant的SQLTask

回答by malenkiy_scot

Execute shell(on Unix-like systems) and Execute Windows batch command(on Windows) build steps can be used to run arbitrary scripts on Jenkins.

执行 shell(在类 Unix 系统上)和执行 Windows 批处理命令(在 Windows 上)构建步骤可用于在 Jenkins 上运行任意脚本。

回答by EngineerBetter_DJ

You may want to look at LiquiBase, which can execute platform-agnostic scripts through Maven. This might not be appropriate for your use case, but I've used it successfully in the past to manage database changes associated with specific builds of software, and promotions of builds between environments.

您可能需要查看LiquiBase,它可以通过 Maven 执行与平台无关的脚本。这可能不适合您的用例,但我过去曾成功地使用它来管理与特定软件构建相关的数据库更改,以及环境之间构建的提升。