|
| 1 | +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-python-poetry-task.md |
| 2 | +name: Test Python |
| 3 | + |
| 4 | +env: |
| 5 | + # See: https://github.com/actions/setup-python/tree/main#available-versions-of-python |
| 6 | + PYTHON_VERSION: "3.11.6" |
| 7 | + |
| 8 | +# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows |
| 9 | +on: |
| 10 | + create: |
| 11 | + push: |
| 12 | + paths: |
| 13 | + - ".github/workflows/test-python-poetry-task.ya?ml" |
| 14 | + - ".github/.?codecov.ya?ml" |
| 15 | + - "dev/.?codecov.ya?ml" |
| 16 | + - ".?codecov.ya?ml" |
| 17 | + - "Taskfile.ya?ml" |
| 18 | + - ".python-version" |
| 19 | + - "poetry.lock" |
| 20 | + - "pyproject.toml" |
| 21 | + - "reportsizedeltas/tests/**" |
| 22 | + - "**.py" |
| 23 | + pull_request: |
| 24 | + paths: |
| 25 | + - ".github/workflows/test-python-poetry-task.ya?ml" |
| 26 | + - ".github/.?codecov.ya?ml" |
| 27 | + - "dev/.?codecov.ya?ml" |
| 28 | + - ".?codecov.ya?ml" |
| 29 | + - "Taskfile.ya?ml" |
| 30 | + - ".python-version" |
| 31 | + - "poetry.lock" |
| 32 | + - "pyproject.toml" |
| 33 | + - "reportsizedeltas/tests/**" |
| 34 | + - "**.py" |
| 35 | + schedule: |
| 36 | + # Run periodically to catch breakage caused by external changes. |
| 37 | + - cron: "0 12 * * WED" |
| 38 | + workflow_dispatch: |
| 39 | + repository_dispatch: |
| 40 | + |
| 41 | +jobs: |
| 42 | + run-determination: |
| 43 | + runs-on: ubuntu-latest |
| 44 | + outputs: |
| 45 | + result: ${{ steps.determination.outputs.result }} |
| 46 | + permissions: {} |
| 47 | + steps: |
| 48 | + - name: Determine if the rest of the workflow should run |
| 49 | + id: determination |
| 50 | + run: | |
| 51 | + RELEASE_BRANCH_REGEX="^refs/heads/v[0-9]+$" |
| 52 | + # The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead. |
| 53 | + if [[ |
| 54 | + "${{ github.event_name }}" != "create" || |
| 55 | + "${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX |
| 56 | + ]]; then |
| 57 | + # Run the other jobs. |
| 58 | + RESULT="true" |
| 59 | + else |
| 60 | + # There is no need to run the other jobs. |
| 61 | + RESULT="false" |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "result=$RESULT" >> $GITHUB_OUTPUT |
| 65 | +
|
| 66 | + test: |
| 67 | + needs: run-determination |
| 68 | + if: needs.run-determination.outputs.result == 'true' |
| 69 | + runs-on: ubuntu-latest |
| 70 | + permissions: |
| 71 | + contents: read |
| 72 | + |
| 73 | + env: |
| 74 | + COVERAGE_DATA_FILENAME: coverage.xml |
| 75 | + |
| 76 | + steps: |
| 77 | + - name: Checkout repository |
| 78 | + uses: actions/checkout@v4 |
| 79 | + |
| 80 | + - name: Install Python |
| 81 | + uses: actions/setup-python@v5 |
| 82 | + with: |
| 83 | + python-version: ${{ env.PYTHON_VERSION }} |
| 84 | + |
| 85 | + - name: Install Poetry |
| 86 | + run: pip install poetry |
| 87 | + |
| 88 | + - name: Install Task |
| 89 | + uses: arduino/setup-task@v1 |
| 90 | + with: |
| 91 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + version: 3.x |
| 93 | + |
| 94 | + - name: Run tests |
| 95 | + uses: liskin/gh-problem-matcher-wrap@v3 |
| 96 | + with: |
| 97 | + linters: pytest |
| 98 | + run: task python:test |
| 99 | + |
| 100 | + - name: Display code coverage report |
| 101 | + run: task python:coverage-report |
| 102 | + |
| 103 | + # codecov/codecov-action only makes the conversion if the `coverage` package is installed in the global runner |
| 104 | + # environment |
| 105 | + - name: Convert code coverage report to format required by Codecov |
| 106 | + run: | |
| 107 | + poetry run \ |
| 108 | + coverage xml \ |
| 109 | + -o "${{ github.workspace }}/${{ env.COVERAGE_DATA_FILENAME }}" |
| 110 | +
|
| 111 | + # A token is used to avoid intermittent spurious job failures caused by rate limiting. |
| 112 | + - name: Set up Codecov upload token |
| 113 | + run: | |
| 114 | + if [[ "${{ github.repository }}" == "arduino/compile-sketches" ]]; then |
| 115 | + # In order to avoid uploads of data from forks, only use the token for runs in the parent repo. |
| 116 | + # Token is intentionally exposed. |
| 117 | + # See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 |
| 118 | + CODECOV_TOKEN="3889efcb-4ed3-4d07-980a-99a3896bd9b8" |
| 119 | + else |
| 120 | + # codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input. |
| 121 | + CODECOV_TOKEN="" |
| 122 | + fi |
| 123 | + echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV" |
| 124 | +
|
| 125 | + - name: Upload coverage report to Codecov |
| 126 | + uses: codecov/codecov-action@v3 |
| 127 | + with: |
| 128 | + fail_ci_if_error: true |
| 129 | + file: ${{ env.COVERAGE_DATA_FILENAME }} |
| 130 | + token: ${{ env.CODECOV_TOKEN }} |
0 commit comments