Android 项目的 CircleCI 重新编译单元测试的所有内容

问题描述 投票:0回答:1

我有这个circleci配置。

    version: 2.1

orbs:
  android: circleci/[email protected]
  aws-cli: circleci/[email protected]

references:
  cache_key: &cache_key
    key: cache-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "build.gradle" }}-{{ checksum "app/build.gradle" }}
  
  restore_cache: &restore_cache
    restore_cache:
      <<: *cache_key

  save_cache: &save_cache
    save_cache:
      <<: *cache_key
      paths:
        - ~/.gradle
        - ~/.m2
        - "/opt/android-sdk-linux/licenses/"

  attach_debug_workspace: &attach_debug_workspace
    attach_workspace:
      at: workspace

  persist_debug_workspace: &persist_debug_workspace
    persist_to_workspace:
      root: .
      paths:
        - app/build/intermediates
        - app/build/outputs/androidTest-results
        - app/build/outputs/apk
        - app/build/outputs/code-coverage
        - app/build/test-results

  persist_release_workspace: &persist_release_workspace
    persist_to_workspace:
      root: workspace
      paths:
        - app/build

jobs:

  build_debug:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - run:
          name: Download dependencies
          command: ./gradlew androidDependencies
      - *save_cache
      - run:
          name: Gradle build (debug)
          command: ./gradlew :app:assembleDebug :app:assembleAndroidTest --no-daemon --stacktrace
      - *persist_debug_workspace
      - store_artifacts:
          path: app/build/outputs/apk/
          destination: /apk/
  build_release:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - run:
          name: Download dependencies
          command: ./gradlew androidDependencies
      - *save_cache
      - run:
          name: Gradle build (release)
          command: ./gradlew :app:assembleRelease --no-daemon --stacktrace
      - *persist_release_workspace
      - store_artifacts:
          path: app/build/outputs/apk/
          destination: /apk/
  test_unit:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - run:
          name: Run unit tests
          command: ./gradlew test --no-daemon
      - *persist_debug_workspace
      - store_artifacts:
          path: app/build/reports/
          destination: /reports/
      - store_test_results:
          path: app/build/test-results/
          destination: /test-results/
  test_instrumented:
    parameters:
      system-image:
        type: string
        default: system-images;android-29;default;x86
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - android/start-emulator-and-run-tests:
          test-command: ./gradlew connectedFlavorADebugAndroidTest --no-daemon
          system-image: << parameters.system-image >>
      - run:
          name: Save test results
          command: |
            mkdir -p ~/test-results/junit/
            find . -type f -regex ".*/build/outputs/androidTest-results/.*xml" -exec cp {} ~/test-results/junit/ \;
          when: always
      - *persist_debug_workspace
      - store_artifacts:
          path: app/build/reports/
          destination: /reports/
      - store_test_results:
          path: app/build/outputs/androidTest-results/connected/
          destination: /test-results/
  report_coverage:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - run:
          name: Generate JaCoCo report
          command: ./gradlew jacocoAggregatedReport sonarqube --info
      - store_artifacts:
          path: app/build/reports/
          destination: /reports/
  upload_artifacts:
    executor:
      name: android/android-machine
      resource-class: large
      tag: 2022.12.1
    steps:
      - checkout
      - *restore_cache
      - *attach_debug_workspace
      - run:
          name: Upload to artifactory
          command: |
            ./jfrog upload

workflows:
    version: 2
    test-and-coverage:
      jobs:
        - build_debug:
            context:
              - CodeArtifact
        - test_unit:
            requires:
              - build_debug
        - test_instrumented:
            requires:
              - build_debug
        - report_coverage:
            requires:
              - test_unit
              - test_instrumented
            context:
              - SonarCloud
    conditional-workflow:
      when:
        or:
          - equal: [ develop, << pipeline.git.branch >> ]
          - equal: [ master, << pipeline.git.branch >> ]
      jobs:
        - build_debug:
        - build_release:
        - test_unit:
            requires:
              - build_debug
        - test_instrumented:
            requires:
              - build_debug
        - report_coverage:
            requires:
              - test_unit
              - test_instrumented
            context:
              - SonarCloud
        - upload_artifacts:
           requires:
            - build_release

预期的是,第一个作业是构建,因此 gradle 构建了所有内容,下一个作业(单元测试和 IT 测试)不需要再次编译,因为它之前已编译过,但 gradle 正在编译所有作业中的所有内容。

我做错了什么? 谢谢

android gradle circleci
1个回答
-1
投票

工作是独特的环境。第一个作业中构建的包,第一个作业中真正完成的任何内容,在第二个作业中都不存在。

您要么需要将所有内容合并到一项作业中,要么使用称为工作区的 CircleCI 功能将某些文件从一项作业复制到其他后续作业中。

© www.soinside.com 2019 - 2024. All rights reserved.