Shu*_*dey 5 pipeline bitbucket-pipelines
我正在运行我所有的测试用例,其中一些用例有时会失败,管道检测到它并使步骤和构建失败。这会阻止要执行的下一步(压缩报告文件夹)。我想将该 zip 文件作为电子邮件附件发送。
这是我的 bitbucket-pipelines.yml文件
custom: # Pipelines that can only be triggered manually
QA2: # The name that is displayed in the list in the Bitbucket Cloud GUI
- step:
image: openjdk:8
caches:
- gradle
size: 2x # double resources available for this step to 8G
script:
- apt-get update
- apt-get install zip
- cd config/geb
- ./gradlew -DBASE_URL=qa2 clean BSchrome_win **# This step fails**
- cd build/reports
- zip -r testresult.zip BSchrome_winTest
after-script: # On test execution completion or build failure, send test report to e-mail lists
- pipe: atlassian/email-notify:0.3.11
variables:
<<: *email-notify-config
TO: 'email@email.com'
SUBJECT: "Test result for QA2 environment"
BODY_PLAIN: |
Please find the attached test result report to the email.
ATTACHMENTS: config/geb/build/reports/testresult.zip
Run Code Online (Sandbox Code Playgroud)

步骤:
- cd build/reports
and
- zip -r testresult.zip BSchrome_winTest
Run Code Online (Sandbox Code Playgroud)
不要因为- ./gradlew -DBASE_URL=qa2 clean BSchrome_win失败而被执行
我不希望 bitbucket 使该步骤失败并停止执行 Queue 的步骤。
更好的解决方案
如果您希望脚本中的所有命令无论出现错误都执行,那么请将set +e其放在脚本的顶部。
如果您只想忽略某个特定命令的错误,则将其放在set +e该命令之前和set -e之后。
例子:
- set +e
- ./gradlew -DBASE_URL=qa2 clean BSchrome_win **# This step fails**
- set -e
Run Code Online (Sandbox Code Playgroud)
对于命令组也有效:
- set +e
- cd config/geb
- ./gradlew -DBASE_URL=qa2 clean BSchrome_win **# This step fails**
- cd config/geb
- set -e
Run Code Online (Sandbox Code Playgroud)
该bitbucket-pipelines.yml文件只是在 Unix 上运行 bash/shell 命令。脚本运行器查找每个命令的返回状态代码,以查看它是成功(status = 0)还是失败(status = non-zero)。因此,您可以使用各种技术来控制此状态代码:
" || true"到命令的末尾./gradlew -DBASE_URL=qa2 clean BSchrome_win || true
Run Code Online (Sandbox Code Playgroud)
当您添加"|| true"到 shell 命令的末尾时,它的意思是“忽略任何错误,并始终返回成功代码 0”。更多信息:
./gradlew -DBASE_URL=qa2 clean BSchrome_win --continue
Run Code Online (Sandbox Code Playgroud)
该"--continue"标志可用于防止单个测试失败停止整个任务。因此,如果一个测试或子步骤失败,gradle 将尝试继续运行其他测试,直到所有测试都运行完毕。但是,如果重要步骤失败,它仍可能返回错误。更多信息:忽略 Gradle 构建失败并继续构建脚本?
after-script部分after-script:
- cd config/geb # You may need this, if the current working directory is reset. Check with 'pwd'
- cd build/reports
- zip -r testresult.zip BSchrome_winTest
Run Code Online (Sandbox Code Playgroud)
如果您将创建 zip 的 2 个步骤移至该after-script部分,则无论上一步的成功/失败状态如何,它们都将始终运行。