|
| 1 | +name: Build, Test, and Publish Python Package |
| 2 | + |
| 3 | +on: [push] |
| 4 | + |
| 5 | +jobs: |
| 6 | + test: |
| 7 | + |
| 8 | + runs-on: ubuntu-latest |
| 9 | + strategy: |
| 10 | + fail-fast: false |
| 11 | + matrix: |
| 12 | + python-version: ["3.11"] |
| 13 | + |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 |
| 16 | + - name: Set up Python ${{ matrix.python-version }} |
| 17 | + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d |
| 18 | + with: |
| 19 | + python-version: ${{ matrix.python-version }} |
| 20 | + - name: Install dependencies |
| 21 | + run: | |
| 22 | + pip install -r requirements.txt |
| 23 | + - name: Lint with autopep8 and get statistics from flake8 |
| 24 | + run: | |
| 25 | + # exit-zero treats all errors as warnings. |
| 26 | + # show statistics for Python syntax errors or undefined names. |
| 27 | + # the GitHub editor is 127 chars wide. |
| 28 | + flake8 . --count --exit-zero --extend-ignore E402 --max-complexity=10 --max-line-length=127 --show-source --statistics |
| 29 | + # stop the build if there are Python syntax errors. |
| 30 | + autopep8 . --ignore E402 --recursive --diff --exit-code |
| 31 | + - name: Install local package |
| 32 | + run: | |
| 33 | + python -m pip install --editable . |
| 34 | + - name: Test with pytest |
| 35 | + run: | |
| 36 | + coverage run -m pytest |
| 37 | + - name: Report coverage statistics on modules |
| 38 | + run: | |
| 39 | + coverage report --fail-under=80 --show-missing |
| 40 | + - name: Type checking with mypy |
| 41 | + run: | |
| 42 | + mypy --strict --exclude='src' . |
| 43 | +
|
| 44 | + build: |
| 45 | + |
| 46 | + needs: [test] |
| 47 | + runs-on: ${{ matrix.os }} |
| 48 | + strategy: |
| 49 | + fail-fast: false |
| 50 | + matrix: |
| 51 | + os: [ubuntu-latest] |
| 52 | + python-version: ["3.11"] |
| 53 | + |
| 54 | + env: |
| 55 | + PACKAGE_NAME: bytecorecompiler |
| 56 | + |
| 57 | + steps: |
| 58 | + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 |
| 59 | + - name: Setup Python |
| 60 | + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d |
| 61 | + with: |
| 62 | + python-version: ${{ matrix.python-version }} |
| 63 | + - name: Install dependencies |
| 64 | + run: pip install -r requirements.txt |
| 65 | + - name: Build package |
| 66 | + run: python -m build |
| 67 | + - name: Test self building package |
| 68 | + run: | |
| 69 | + pip install dist/${{ env.PACKAGE_NAME }}-*.tar.gz |
| 70 | + python -c "import ${{ env.PACKAGE_NAME }}; print(${{ env.PACKAGE_NAME }})" |
| 71 | + shell: bash |
| 72 | + - name: Upload distribution archives |
| 73 | + uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 |
| 74 | + with: |
| 75 | + name: distribution-archives-${{ matrix.os }}-${{ matrix.python-version }} |
| 76 | + path: dist |
| 77 | + |
| 78 | + combine: |
| 79 | + |
| 80 | + needs: [test, build] |
| 81 | + runs-on: ubuntu-latest |
| 82 | + |
| 83 | + steps: |
| 84 | + - name: Download All Artifacts |
| 85 | + uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e |
| 86 | + with: |
| 87 | + path: distribution-archives |
| 88 | + pattern: distribution-archives-* |
| 89 | + merge-multiple: true |
| 90 | + - name: List distribution archives |
| 91 | + run: ls --recursive distribution-archives |
| 92 | + - name: Upload combined distribution archives |
| 93 | + uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 |
| 94 | + with: |
| 95 | + name: combined-distribution-archives |
| 96 | + path: distribution-archives |
| 97 | + |
| 98 | + publish: |
| 99 | + |
| 100 | + if: startsWith(github.ref, 'refs/tags/') |
| 101 | + needs: [test, build, combine] |
| 102 | + runs-on: ubuntu-latest |
| 103 | + environment: |
| 104 | + name: pypi |
| 105 | + url: https://pypi.org/project/bytecorecompiler |
| 106 | + permissions: |
| 107 | + id-token: write |
| 108 | + |
| 109 | + env: |
| 110 | + PACKAGE_NAME: bytecorecompiler |
| 111 | + |
| 112 | + steps: |
| 113 | + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 |
| 114 | + - name: Download combined distribution archives |
| 115 | + uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e |
| 116 | + with: |
| 117 | + name: combined-distribution-archives |
| 118 | + path: dist |
| 119 | + - name: List dist folder |
| 120 | + run: ls --recursive dist |
| 121 | + - name: Extract and store tag |
| 122 | + run: | |
| 123 | + TAG=${GITHUB_REF#refs/tags/} |
| 124 | + echo "Tag: $TAG" |
| 125 | + echo "TAG=$TAG" >> $GITHUB_ENV |
| 126 | + - name: Verify version in combined distribution archives |
| 127 | + run: | |
| 128 | + for file in dist/*; do |
| 129 | + filename=$(basename "$file") |
| 130 | + if [[ $filename != ${{ env.PACKAGE_NAME }}-$TAG* ]]; then |
| 131 | + echo "File $filename does not start with ${{ env.PACKAGE_NAME }}-$TAG" |
| 132 | + exit 1 |
| 133 | + fi |
| 134 | + done |
| 135 | + echo "All files in dist directory start with ${{ env.PACKAGE_NAME }}-$TAG" |
| 136 | + - name: Verify existence of source distribution archive |
| 137 | + run: | |
| 138 | + if [[ ! -f dist/${{ env.PACKAGE_NAME }}-$TAG.tar.gz ]]; then |
| 139 | + echo "Error: dist/${{ env.PACKAGE_NAME }}-$TAG.tar.gz does not exist." |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + echo "dist/${{ env.PACKAGE_NAME }}-$TAG.tar.gz exists." |
| 143 | + - name: Verify version in pyproject.toml |
| 144 | + run: | |
| 145 | + VERSION=$(grep --perl-regexp --only-matching '(?<=^version = ")[^"]*' pyproject.toml) |
| 146 | + if [[ "$VERSION" != "$TAG" ]]; then |
| 147 | + echo "Version in pyproject.toml $VERSION does not match the tag $TAG" |
| 148 | + exit 1 |
| 149 | + fi |
| 150 | + echo "Version in pyproject.toml matches the tag: $VERSION" |
| 151 | + - name: Publish package to PyPI |
| 152 | + uses: pypa/gh-action-pypi-publish@ec4db0b4ddc65acdf4bff5fa45ac92d78b56bdf0 |
| 153 | + with: |
| 154 | + verify-metadata: true |
| 155 | + skip-existing: false |
| 156 | + verbose: false |
| 157 | + print-hash: true |
0 commit comments