我正在将freestyle作业从链接到流水线的过程中迁移到Jenkinsfile中。
我当前的管道将并行执行两个作业,其中一个将创建一个到数据库的隧道(使用随机生成的端口),下一个作业需要获得这个端口号,所以我正在执行curl命令,并读取create-db-tunnel作业的控制台并存储端口号。create-db-tunnel需要保持运行,因为后续作业正在连接到数据库并进行DB转储。这是我在第二个作业上运行的curl命令,它从已建立的DB隧道返回随机生成的端口号:
Port=$(curl -u ${USERNAME}:${TOKEN} http://myjenkinsurl.com/job/create-db-tunnel/lastBuild/consoleText | grep Port | grep -Eo '[0-9]{3,5}')
我想知道有没有类似的东西可以在Jenkinsfile中使用?我目前有两个并行触发的作业,但由于create-db-tunnel不再是一个自由式作业,我不确定我是否还能获得端口号?我可以确认db_tunnel阶段的控制台日志中包含端口号,但不确定如何查询该控制台。这是我的jenkinsfile:
pipeline {
agent any
environment {
APTIBLE_LOGIN = credentials('aptible')
}
stages {
stage('Setup') {
parallel {
// run db_tunnel and get_port in parralel
stage ('db_tunnel') {
steps {
sh """
export PATH=$PATH:/usr/local/bin
aptible login --email=$APTIBLE_LOGIN_USR --password=$APTIBLE_LOGIN_PSW
aptible db:tunnel postgres-prod & sleep 30s
"""
}
}
stage('get_port') {
steps {
sh """
sleep 15s
//this will not work
Port=$(curl -u ${USERNAME}:${TOKEN} http://myjenkinsurl.com/job/db_tunnel/lastBuild/consoleText | grep Port | grep -Eo '[0-9]{3,5}')
echo "Port=$Port" > port.txt
"""
}
}
}
}
}
}发布于 2018-09-09 16:17:41
实际上,我找到了我的问题的解决方案-这是我必须运行的一个非常类似的curl命令,现在我正在获得所需的端口号。如果有人感兴趣,下面是jenkinsfile:
pipeline {
agent any
environment {
APTIBLE_LOGIN = credentials('aptible')
JENKINS_TOKEN = credentials('jenkins')
}
stages {
stage('Setup') {
parallel {
// run db_tunnel and get_port in parralel
stage ('db_tunnel') {
steps {
sh """
export PATH=$PATH:/usr/local/bin
aptible login --email=$APTIBLE_LOGIN_USR --password=$APTIBLE_LOGIN_PSW
aptible db:tunnel postgres-prod & sleep 30s
"""
}
}
stage('get_port') {
steps {
sh """
sleep 20
Port=\$(curl -u $JENKINS_TOKEN_USR:$JENKINS_TOKEN_PSW http://myjenkinsurl.com/job/schema-archive-jenkinsfile/lastBuild/consoleText | grep Port | grep -Eo '[0-9]{3,5}')
echo "Port=\$Port" > port.txt
"""
}
}
}
}
}
}https://stackoverflow.com/questions/52239468
复制相似问题