第一次使用 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/