首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jenkinsfile:来自上一阶段(仍在运行)的Curl日志

Jenkinsfile:来自上一阶段(仍在运行)的Curl日志
EN

Stack Overflow用户
提问于 2018-09-09 05:15:10
回答 1查看 178关注 0票数 0

我正在将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:

代码语言:javascript
复制
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
                        """
                    }
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-09-09 16:17:41

实际上,我找到了我的问题的解决方案-这是我必须运行的一个非常类似的curl命令,现在我正在获得所需的端口号。如果有人感兴趣,下面是jenkinsfile:

代码语言:javascript
复制
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
                        """
                    }
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52239468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档