jenkins-mysql查询的活动选项

cmssoen2  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(368)

第一次玩jenkins,尝试使用groovy脚本获得activechoices参数,但运气不太好。运行生成时,参数下拉列表始终为空。我的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脚本的输出,这样我就可以查看它是否连接到数据库了?
提前感谢您的帮助/建议

eimct9ow

eimct9ow1#

您可以尝试以下操作:

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]

如果由于某种原因无法连接到db,则会出现异常。这里有更多的文件。
在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}]"
  }

相关问题