【问题标题】:Run tests in Jenkins pipeline when the build is running构建运行时在 Jenkins 管道中运行测试
【发布时间】:2021-11-27 03:12:46
【问题描述】:

我正在尝试将管道配置为在每个 PR 到 dev 分支上运行自动化 e2e 测试。

为此,我正在拉动项目,构建它,当我想运行我的测试时,我不能这样做,因为当项目运行时,管道不会切换到第二阶段。

问题是当我在 Jenkins 中构建项目并运行时,如何强制我的测试运行?

我尝试了并行阶段执行,但它也不起作用,因为我的测试在项目开始构建时开始运行。

我的管道:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Cloning..'
                git branch: 'dev', url: 'https://github.com/...'
                echo 'Building..'
                sh 'npm install'
                sh 'npm run dev'
            }
        }
        stage('e2e Test') {
            steps {
                echo 'Cloning..'
                git branch: 'cypress-tests', url: 'https://github.com/...'
                echo 'Testing..'
                sh 'cd cypress-e2e'
                sh 'npm install'
                sh 'npm run dev'
            }
        }
    }
}

【问题讨论】:

  • “e2e 测试”阶段无法运行的唯一原因是“构建”阶段出现异常或错误。你能分享管道的控制台输出吗?

标签: jenkins jenkins-pipeline devops pipeline


【解决方案1】:

您可以添加一个用于克隆测试分支的阶段,然后使用parallel 以相同的方式运行构建和测试阶段。以下管道应该可以工作:

pipeline {
agent any
stages {
    stage ('Clone branchs') {
        steps {
            echo 'Cloning cypress-tests'
            git branch: 'cypress-tests', url: 'https://github.com/...'
            echo 'Cloning dev ..'
            git branch: 'dev', url: 'https://github.com/...'
        }
    }
    stage('Build and test') {
        parallel {
            stage('build') {
                steps {
                    echo 'Building..'
                    sh 'npm install'
                    sh 'npm run dev'
                }
            }
            stage('e2e Test') {
                steps {
                    echo 'Testing..'
                    sh 'cd cypress-e2e'
                    sh 'npm install'
                    sh 'npm run dev'
                }
            }
        }
    }
}                

您将拥有以下管道:

【讨论】:

  • 这里dev项目覆盖cypress-tests克隆项目的问题?
【解决方案2】:

我可以想到两种可能的方法来处理这个问题:

  1. 在不同的节点上执行每个阶段。以便为每个阶段创建不同的工作空间。示例:

    pipeline {
        agent any
        stages {
            stage('Build') {
                agent {
                  label "node1"
                }
                steps {
                    echo 'Cloning..'
                    git branch: 'dev', url: 'https://github.com/...'
                    echo 'Building..'
                    sh 'npm install'
                    sh 'npm run dev'
                }
            }
            stage('e2e Test') {
                agent {
                  label "node2"
                }
                steps {
                    echo 'Cloning..'
                    git branch: 'cypress-tests', url: 'https://github.com/...'
                    echo 'Testing..'
                    sh 'cd cypress-e2e'
                    sh 'npm install'
                    sh 'npm run dev'
                }
            }
        }
    }
    
  2. BUILD_DIRE2E_DIR 创建单独的目录。
    cd 进入每个阶段的相关目录,然后执行git checkout 和那里的其余步骤。示例:

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh '''
                    BUILD="${WORKSPACE}/BUILD_DIR"
                    mkdir -p "${BUILD}" && cd "${BUILD}"
                    '''
                    echo 'Cloning..'
                    git branch: 'dev', url: 'https://github.com/...'
                    echo 'Building..'
                    sh 'npm install'
                    sh 'npm run dev'
                }
            }
            stage('e2e Test') {
                steps {
                    sh '''
                    E2E="${WORKSPACE}/E2E_DIR"
                    mkdir -p "${E2E}" && cd "${E2E}"
                    '''
                    echo 'Cloning..'
                    git branch: 'cypress-tests', url: 'https://github.com/...'
                    echo 'Testing..'
                    sh 'cd cypress-e2e'
                    sh 'npm install'
                    sh 'npm run dev'
                }
            }
        }
    }
    

【讨论】:

    最近更新 更多