mysql - Jenkins - 来自 MySQL 查询的主动选择

标签 mysql database jenkins groovy jenkins-plugins

第一次使用 Jenkins 并尝试使用 groovy 脚本使事件选择参数正常工作,但运气不佳。运行构建时,下拉参数始终为空。我的常规脚本如下:

import groovy.sql.Sql

def output = []

def sql = Sql.newInstance('jdbc:mysql://localhost:3308/information_schema', 'jenkins', 'password', 'com.mysql.jdbc.Driver')
String sqlString = "select schema_name from information_schema.schemata;"
sql.eachRow(sqlString){ row ->  
    output.push(row[0])
}


return output

我哪里错了?有没有一种方法可以查看 groovy 脚本的输出,以便我可以查看它是否甚至与数据库建立了连接?

提前感谢任何帮助/建议

最佳答案

你可以尝试这样的事情:

import groovy.sql.Sql

//describe the DB connection params
def db = [
        url:'jdbc:mysql://localhost:3306/information_schema',
        user:'root',
        password:'',
        driver:'com.mysql.jdbc.Driver']

//New connection
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)

def output = []
//query
String sqlString = "select schema_name from information_schema.schemata"
sql.eachRow(sqlString){ row ->
  output.push(row.schema_name)
}
//clean up
sql.close()

如果你愿意,你现在可以往里看:

//Check what's up
String[] dbs = output as String[]

println dbs

在我的机器上输出是这样的:

[information_schema, mysql, ******, performance_schema, sys]

如果您出于某种原因无法连接到数据库,那么您将得到一个异常。更多文档 here .

在您的 Jenkins 管道中,您可以使用 ChoiceParameterDefinition .像这样的东西(免责声明 - 未经测试):

stage('Ask DB') {

    echo "Collecting user input about the DB..."

    String[] dbs = ...;//collect DB-s here

    //define the choice
    userChoice = new ChoiceParameterDefinition('targetDB',
            dbs,//<-- Pass array here!
            'Choose wisely your DB!')

    def targetDB = input(
            id: 'target-db',
            message: 'What is the DB?',
            parameters: [userChoice])

    echo "Input collected, proceeding for DB [${targetDB}]"
  }

关于mysql - Jenkins - 来自 MySQL 查询的主动选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51255434/

相关文章:

mysql - 刚刚开始 ROR Lynda 教程,尝试执行 Rails 服务器时出错

MySql 使用另一个表中的数据更新一个表

java - 更新java中的查询语法错误

MySQL 瓶颈

build - 如何在 Jenkins 中禁用 'schedule build' 的工作按钮?

mysql - NULL 和 NOT NULL 查询版本之间的时间增加了十倍

php - 如何批准待处理记录?

docker - 如何在不使用Docker容器的情况下在Open Server中运行Docker镜像

jenkins - 如何使用 Jenkins Skip Certificate Check 插件?

mysql MyISAM vs NDB storege engine for heavy reads