Jenkins 构建失败而没有更新 Xray 的失败状态

Jenkins build failing without updating Xray with the failed status

如果这不是问这个问题的地方,请原谅我。我是来自 Jenkinsfile 的 Jenkins 管道中的 运行 python 脚本。我还在 Jenkisfile 中更新 Jira Xray 票证。 Behave 用于验证测试状态。如果检查失败,则 Jenkins 构建失败,而 Xray 票据没有更新失败。我尝试使用“尝试”来捕获故障,但未能成功将故障传播到 Xray 票证。

这里有人知道我在哪里可以找到答案吗?我会在你的部门。

詹金斯文件

node() {
    
    def repoURL = '<GitLab URL>/prod-003.git'
    def STC_INSTALL = "/opt/STC_CLIENT/Spirent_TestCenter_5.22/Spirent_TestCenter_Application_Linux/"
    
    try {
        stage("Prepare Workspace") {
            echo "*** Prepare Workspace ***"
            cleanWs()
            env.WORKSPACE_LOCAL = sh(returnStdout: true, script: 'pwd').trim()
            env.BUILD_TIME = "${BUILD_TIMESTAMP}"
            echo "Workspace set to:" + env.WORKSPACE_LOCAL
            echo "Build time:" + env.BUILD_TIME
            sh """
            cd ${env.WORKSPACE_LOCAL}
            rm -fr *
            """
        }
        stage('Checkout Code') {
            echo "*** Checking Code Out ***"
            git branch: 'master', credentialsId: '', url: repoURL
        }
        stage('Executing Tests') {
            if (env.WanModeCheck == "Yes") {
                echo "Executing WAN Mode Change Before FW Upgrade"
                sh """
                /var/lib/jenkins/.pyenv/shims/python WanMode.py -i $modemIP -m $WanMode
                """
                echo "Starting Firmware Upgrade"
                sh """
                cd ${env.WORKSPACE_LOCAL}
                ./ModemUpgrade.sh -i $modemIP -f $FW -p2
                /var/lib/jenkins/.pyenv/shims/behave -f cucumber -o storetarget-bdd/reporting/cucumber.json --junit --format=json -o target/behave.json --junit ./features/PROD-003.feature
                """
            } else {
                echo "#######################\n# Skipping WAN Mode Change #\n#######################"
            }
            if (env.WanModeCheck == "No") {
                echo "Starting Firmware Upgrade"
                sh """
                cd ${env.WORKSPACE_LOCAL}
                ./ModemUpgrade.sh -i $modemIP -f $FW -p2
                /var/lib/jenkins/.pyenv/shims/behave -f cucumber -o storetarget-bdd/reporting/cucumber.json --junit --format=json -o target/behave.json --junit ./features/fwupgrade.feature
                """
            }
            // Setting variables to use for the Xray Test Execution
            res = sh(returnStdout: true, script: 'awk "/#@/{f=1;next} /#####/{f=0} f" PROD-003-Out.txt | sed -e "s/#//g" -e "s/^ * //g" | tr "\n" "%" | sed -e "s/^%%%%%%//g" -e "s/%%$//g" -e "s/%/\\\\Z/g" -e "s/Z/n/g"')
            env.STResults = res.strip()
            model = sh(returnStdout: true, script: 'grep Model: PROD-003-Out.txt')
            env.Model = model.strip()
            wanmode = sh(returnStdout: true, script: 'grep CPE PROD-003-Out.txt')
            env.WanMode = wanmode.strip()
            serialnum = sh(returnStdout: true, script: 'grep Number: PROD-003-Out.txt')
            env.SerialNum = serialnum.strip()
            echo "End of test phase"
        }
        stage('Expose report') {
            echo "*** Expose Reports ***"
            echo "*** Archive Artifacts ***"
            archiveArtifacts "**/cucumber.json"
            echo "*** cucumber cucumber.json ***"
            cucumber '**/cucumber.json'
            junit skipPublishingChecks: true, allowEmptyResults: true, keepLongStdio: true, testResults: 'reports/*.xml'
            cucumber buildStatus: "UNSTABLE",
            fileIncludePattern: "**/cucumber.json",
            jsonReportDirectory: 'reports'
        }
    
        stage('Import results to Xray') {
            echo "*** Import Results to XRAY ***"
            def description = "Jenkins Project: ${env.JOB_NAME}\n\nCucumber Test Report: [${env.JOB_NAME}-Link|${env.BUILD_URL}/cucumber-html-reports/overview-features.html]\n\nJenkins Console Output: [${env.JOB_NAME}-Console-Link|${env.BUILD_URL}/console]\n\nCPE IP: ${modemIP}\n\nCPE FW File Name: ${FW}\n\n${env.STResults}"
            def labels = '["regression","automated_regression"]'
            def environment = "DEV"
            def testExecutionFieldId = 10552
            def testEnvironmentFieldName = "customfield_10372"
            def projectKey = "AARC"
            def projectId = 10608
            def xrayConnectorId = "e66d84d8-f978-4af6-9757-93d5804fde1d"
            // def xrayConnectorId = "${xrayConnectorId}"
            def info = '''{
                "fields": {
                    "project": {
                        "id": "''' + projectId + '''"
                    },
                    "labels":''' + labels + ''',
                    "description":"''' + description + '''",
                    "summary": "''' + env.JOB_NAME + ' ' + env.Model + ' ' + env.WanMode + ' ' + env.SerialNum + ''' Test Executed ''' + env.BUILD_TIME + ''' " ,
                    "issuetype": {
                    "id": "''' + testExecutionFieldId + '''"
                    }
                }
            }'''

            echo info

            step([$class: 'XrayImportBuilder', 
            endpointName: '/cucumber/multipart', 
            importFilePath: 'storetarget-bdd/reporting/cucumber.json', 
            importInfo: info, 
            inputInfoSwitcher: 'fileContent', 
            serverInstance: xrayConnectorId])
        }
    }                                    
    catch(e) {                           
        // If there was an exception thrown, the build failed
        currentBuild.result = "FAILED"
        throw e
    } finally {
        // Success or failure, always send notifications
        echo "Sending final test status to Slack"
        // notifyBuild(currentBuild.result)
    }

}
def notifyBuild(String buildStatus = 'STARTED') {
    // build status of null means successful
    buildStatus =  buildStatus ?: 'SUCCESSFUL'

    // Default values
    def colorName = 'RED'
    def colorCode = '#FF0000'
    def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
    def summary = "${subject} (${env.BUILD_URL})"
    def details = """<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
      <p>Check console output at &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>"""

      // Override default values based on build status
      if (buildStatus == 'STARTED') {
        color = 'BLUE'
        colorCode = '#0000FF'
        msg = "Build: ${env.JOB_NAME} has started: ${BUILD_TIMESTAMP}"
      } else if (buildStatus == 'UNSTABLE') {
        color = 'YELLOW'
        colorCode = '#FFFF00'
        msg = "Build: ${env.JOB_NAME} was listed as unstable. Look at ${env.BUILD_URL} and Report: ${env.BUILD_URL}/cucumber-html-reports/overview-features.html"
      } else if (buildStatus == 'SUCCESSFUL') {
        color = 'GREEN'
        colorCode = '#00FF00'
        msg = "Build: ${env.JOB_NAME} Completed Successfully ${env.BUILD_URL} Report: ${env.BUILD_URL}/cucumber-html-reports/overview-features.html"
      } else {
        color = 'RED'
        colorCode = '#FF0000'
        msg = "Build: ${env.JOB_NAME} had an issue ${env.BUILD_URL}/console"
      }

    // Send notifications
    slackSend (color: colorCode, message: summary)
    slackSend baseUrl: 'https://hooks.slack.com/services/', 
    channel: '#wopr-private', 
    color: colorCode, 
    message: msg,
    teamDomain: '<Slack URL>', 
    tokenCredentialId: 'Jenkins-Slack-Token', 
    username: 'JenkinsAutomation'
}

特征文件

Feature: SNMP Firmware Upgrade Test
  @demo @AARC-3428
  Scenario: SNMP Firmware Upgrade Executed against the DUT
    Given ModemUpgrade.sh Script Exists
    When SNMP Firmware Upgrade Executed
    Then I expect Result Pass

步骤文件

from behave import *
import pathlib
from pathlib import Path

@given('ModemUpgrade.sh Script Exists')
def step_impl(context):
    STCFile = pathlib.Path('ModemUpgrade.sh')
    if STCFile.exists():
        print("SNMP Firmware Upgrade file exists")
        pass
    # else:
    #     print("SNMP Firmware Upgrade file does not exists")
    #     assert context.failed

@when('SNMP Firmware Upgrade Executed')
def step_impl(context):
    path_to_file = 'PROD-003-Out.txt'
    path = Path(path_to_file)
    if path.is_file():
        print(f'Output file {path_to_file} exists')
    else:
        print(f'Output file {path_to_file} does not exists')

@then('I expect Result Pass')
def step_impl(context):
    Result = False
    with open("PROD-003-Out.txt") as FwUpgradeResults:
        for line in FwUpgradeResults:
            if 'Upgrade Status: Passed'.lower() in line.strip().lower():
                Result = True
                break
            else:
                Result = False
                break
    if Result is False:
        print("Error: Upgrade Failed")
        assert context.failed

建议使用|| /usr/bin/true 似乎适用于上述代码。现在我有第二个实例,当 DUT 失败 DHCP 绑定时,我的 Python 测试抛出异常

def wait_for_dhcp_bind():
    try: 
        stc.perform("Dhcpv4BindWait", objectlist=project)
    except Exception:
        raise Exception("DHCP Bind Failed")

我试图在 Python 脚本之后添加相同的内容,但 Jenkins 构建失败,没有 Xray 测试更新失败。

这是 Jenkinsfile 中的样子

            echo "Starting Speed Test"
            // def ModemMac = sh(returnStdout: true, script: './ModemUpgrade.sh -i ${modemIP} -f mac')
            sh """
                export STC_PRIVATE_INSTALL_DIR=${STC_INSTALL}
                cd ${env.WORKSPACE_LOCAL}
                /var/lib/jenkins/.pyenv/shims/python SpeedTest.py -d $dsp -u $usp -i $iterations -x $imix -f $frames -m $ModemMac || /usr/bin/true
                /var/lib/jenkins/.pyenv/shims/behave -f cucumber -o storetarget-bdd/reporting/cucumber.json --junit --format=json -o target/behave.json --junit ./features/speedtest.feature || /usr/bin/true
            """

你的案子应该很容易解决。如果测试失败,行为实用程序 returns 退出代码 1..

只需将其添加到您的 behave 命令的末尾 || /usr/bin/true(请确保“true”命令的路径)。 这将使您的命令始终 return 为真,即使行为存在一些问题。

所以你的整体命令应该是这样的:

/var/lib/jenkins/.pyenv/shims/behave -f cucumber -o storetarget-bdd/reporting/cucumber.json --junit --format=json -o target/behave.json --junit ./features/PROD-003.feature || /usr/bin/true