簡體   English   中英

Gitlab-ci 階段未完成構建失敗

[英]Gitlab-ci stage not finishing on build failed

我有 gitlab ci 處理單元測試(無論它們是否失敗)和儀器測試(當它們沒有失敗時),但是當./gradlew connectedAndroidTest因為某些測試失敗而失敗時,該命令似乎沒有向 gitlab ci 返回任何內容,並且所以 gitlab ci 等到超時。

我在 ./gradlew connectedAndroidTest 命令上嘗試過參數,但沒有奏效。

stages:
  - tests
  - cleanup
  - quality #not used here
  - distribute #not used here

unit:
  stage: tests
  before_script:
    - bundle install
  script:
    - bundle exec fastlane unit_tests
  only:
    - master
    - tags
  tags:
    - cimacmini
  artifacts:
    name: "reports_${CI_PROJECT_NAME}_${CI_BUILD_REF_NAME}"
    expire_in: 60 days
    paths:
      - app/build/reports/tests/

instrumentation:
  stage: tests
  only:
    - master
    - tags
  tags:
    - cimacmini
  script:
    - emulator @instrumentation &
    - android-wait-for-emulator.sh
    - adb devices
    - adb shell settings put global window_animation_scale 0 &
    - adb shell settings put global transition_animation_scale 0 &
    - adb shell settings put global animator_duration_scale 0 &
    - adb shell input keyevent 82 &
    - ./gradlew --status
    - ./gradlew connectedAndroidTest --continue
    - stop-emulators.sh
  artifacts:
    name: "reports_${CI_PROJECT_NAME}_${CI_BUILD_REF_NAME}"
    expire_in: 60 days
    paths:
      - app/build/reports/androidTests/connected/

closeemulators:
  stage: cleanup
  only:
    - master
    - tags
  tags:
    - cimacmini
  script:
    - stop-emulators.sh
  when: always

因此,如果某些儀器測試失敗,則永遠不會調用stop-emulators.sh 不是來自instrumentation步驟,也不是來自closeemulators步驟。 gitlab 控制台顯示:

在 6m 17s 內構建失敗

58 個可操作任務:58 個已執行

並永遠等待(直到強制超時)。 有人知道該怎么做嗎?

我也遇到過這個問題,這是我的解決方案:根據手冊https://docs.gitlab.com/ee/ci/yaml/README.html#script我們可以使用以下語法:

job:
  script:
    - false || exit_code=$?
    - if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;

並將最后一個命令退出代碼保存到“exit_code”變量並進一步使用它。 這是我來自 gitlab-ci.yml 的測試階段:

test:
  stage: test
  tags:
    - android
    - osx
  script:
    - $ANDROID_HOME/emulator/emulator -avd Pixel_XL_API_29 -wipe-data -no-window -no-audio & EMULATOR_PID=$!
    - chmod +x ./ci/android-wait-for-emulator.sh
    - ./ci/android-wait-for-emulator.sh $ANDROID_HOME/platform-tools
    - ./gradlew cAT || exit_code=$?
    - if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;
    - chmod +x ./ci/stop-emulators.sh
    - ./ci/stop-emulators.sh $ANDROID_HOME/platform-tools
    - if [ $exit_code -ne 0 ]; then exit 1; fi;

因此,如果命令“./gradlew cAT”失敗,作業將繼續執行並執行 stop-emulators.sh,但最終作業將成功,這是不正確的,這就是為什么我們需要最后一行退出作業失敗並在管道中將作業標記為“失敗”。

- if [ $exit_code -ne 0 ]; then exit 1; fi;

所以,我到底有什么https://i.stack.imgur.com/fEU9Z.png

希望這可以幫助。

在我們開發 UI 集成測試的過程中,我們偶然發現了同樣令人難以置信的問題。 這種行為的原因是,當您要求 gitlab 執行腳本時,它將監視它在該腳本期間啟動的所有進程,並等待它們完成以完成此步驟。 其中一個步驟是啟動模擬器。

如果測試失敗stop-emulators.sh將不會執行,因為對於 gitlab ./gradlew connectedAndroidTest --continue是整個管道失敗的指示,它正在等待模擬器停止。 但是模擬器不會停止,因為停止模擬器的下一步根本不會被執行。 這是一種解析為超時的死鎖。

如果您登錄到正在運行測試的機器並手動終止模擬器,您將看到管道將被解除阻塞,並且 gitlab 將完成執行並報告測試錯誤。

解決方案是將./gradlew connectedAndroidTest --continue移動到after_script .gitlab-ci.yml after_script部分,該部分也在構建失敗后執行。

文檔: https : //docs.gitlab.com/ee/ci/yaml/#before_script-and-after_script

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM