1name: Publish generator package to PyPI / pip
2
3on:
4  workflow_dispatch:
5  workflow_call:
6    secrets:
7      PYPI_API_KEY:
8        required: true
9
10jobs:
11  publish_pypi:
12    name: Build and publish pypi package on Ubuntu 20.04
13    runs-on: ubuntu-20.04
14
15    steps:
16      - name: Check out code from GitHub
17        uses: actions/checkout@v2
18        with:
19          path: nanopb
20          fetch-depth: "0"
21
22      - name: Install dependencies
23        run: |
24          python3 -m pip install --user --upgrade pyinstaller poetry protobuf grpcio-tools
25
26      - name: Build PyPI package
27        run: |
28          cd nanopb/extra/poetry
29          ./poetry_build.sh
30
31      - name: Fingerprint package
32        run: |
33          openssl sha256 nanopb/extra/poetry/dist/*.whl
34
35      - name: Check for existence of PyPI package
36        run: |
37          VERSION=$(grep "^version =" nanopb/extra/poetry/build/pyproject.toml | cut -d '"' -f 2)
38          if curl --head --silent --fail https://pypi.org/project/nanopb/$VERSION/; then
39            echo "pypi_exists=true" >> $GITHUB_ENV
40          else
41            echo "pypi_exists=false" >> $GITHUB_ENV
42          fi
43
44      - name: Publish PyPI package
45        if: env.pypi_exists == 'false'
46        env:
47          POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_API_KEY }}
48          POETRY_HTTP_BASIC_PYPI_USERNAME: __token__
49          POETRY_HTTP_BASIC_PYPI_PASSWORD: ${{ secrets.PYPI_API_KEY }}
50        run: |
51          cd nanopb/extra/poetry/build
52          poetry publish -n -v -u __token__ -p "$POETRY_PYPI_TOKEN_PYPI"
53
54  test_pypi:
55    name: Test pypi package
56    runs-on: ubuntu-20.04
57    needs: publish_pypi
58
59    steps:
60      - name: Check out code from GitHub
61        uses: actions/checkout@v2
62        with:
63          path: nanopb
64
65      - name: Wait for package to become visible
66        run: |
67          sleep 60
68
69      - name: Install PyPI package
70        run: |
71          python3 -m pip install --user --upgrade protobuf grpcio-tools scons
72          python3 -m pip install --user --upgrade --pre nanopb
73
74      - name: Test PyPI package
75        run: |
76          cd nanopb/tests/alltypes/
77          nanopb_generator alltypes.proto
78          gcc -Wall -I ../../ -c alltypes.pb.c
79