|
| 1 | +# This workflow will upload a Python Package using Twine when a release is created |
| 2 | +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries |
| 3 | + |
| 4 | +# This workflow uses actions that are not certified by GitHub. |
| 5 | +# They are provided by a third-party and are governed by |
| 6 | +# separate terms of service, privacy policy, and support |
| 7 | +# documentation. |
| 8 | + |
| 9 | +name: Publish Python Package and Docker Image |
| 10 | + |
| 11 | +on: |
| 12 | + push: |
| 13 | + branches: |
| 14 | + - release-v* |
| 15 | + release: |
| 16 | + types: [published] |
| 17 | + |
| 18 | +jobs: |
| 19 | + upload_pypi: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v2 |
| 23 | + with: |
| 24 | + fetch-depth: 0 # unshallow checkout enables setuptools_scm to infer PyPi version from Git |
| 25 | + |
| 26 | + - name: Set up Python |
| 27 | + uses: actions/setup-python@v2 |
| 28 | + with: |
| 29 | + python-version: '3.6' |
| 30 | + |
| 31 | + - name: Install dependencies |
| 32 | + run: | |
| 33 | + python3 -m pip install --upgrade pip |
| 34 | + pip3 install build |
| 35 | +
|
| 36 | + - name: Build Package |
| 37 | + run: python3 -m build |
| 38 | + |
| 39 | + - name: Publish Test Package |
| 40 | + uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 |
| 41 | + with: |
| 42 | + user: __token__ |
| 43 | + password: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 44 | + repository_url: https://test.pypi.org/legacy/ |
| 45 | + |
| 46 | + - name: Temporarily Store the Python Package in GitHub # for convenient reference, troubleshooting, investigation, etc... |
| 47 | + uses: actions/upload-artifact@v2 |
| 48 | + with: |
| 49 | + name: netfoundry-pypi-${{ github.run_id }} |
| 50 | + path: dist/netfoundry-*.tar.gz |
| 51 | + |
| 52 | + - name: Read version string |
| 53 | + id: read_version |
| 54 | + run: | |
| 55 | + PYPI_VERSION=$(python setup.py --version) |
| 56 | + [[ ${PYPI_VERSION} =~ ^[0-9]+\.[0-9]+\.[0-9]+.* ]] || { |
| 57 | + echo "ERROR: unexpected version string '${PYPI_VERSION}'" >&2 |
| 58 | + exit 1 |
| 59 | + } |
| 60 | + echo ::set-output name=pypi_version::${PYPI_VERSION} |
| 61 | +
|
| 62 | + - name: Append 'latest' tag if release published |
| 63 | + env: |
| 64 | + GITHUB_ACTION: ${{ github.event.action }} |
| 65 | + PYPI_VERSION: ${{ steps.read_version.outputs.pypi_version }} |
| 66 | + id: compose_tags |
| 67 | + run: | |
| 68 | + CONTAINER_TAGS="netfoundry/python:${PYPI_VERSION}" |
| 69 | + if [[ ${GITHUB_ACTION} == "published" ]]; then |
| 70 | + CONTAINER_TAGS+=",netfoundry/python:latest" |
| 71 | + fi |
| 72 | + echo ::set-output name=container_tags::${CONTAINER_TAGS} |
| 73 | +
|
| 74 | + - name: Publish Release Package |
| 75 | + if: github.event.action == 'published' |
| 76 | + uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 |
| 77 | + with: |
| 78 | + user: __token__ |
| 79 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 80 | + |
| 81 | + - name: Set up QEMU |
| 82 | + uses: docker/setup-qemu-action@master |
| 83 | + with: |
| 84 | + platforms: amd64,arm,arm64 |
| 85 | + |
| 86 | + - name: Set up Docker BuildKit |
| 87 | + id: buildx |
| 88 | + uses: docker/setup-buildx-action@master |
| 89 | + |
| 90 | + - name: Login to Docker Hub |
| 91 | + uses: docker/login-action@v1 |
| 92 | + with: |
| 93 | + username: ${{ secrets.DOCKER_HUB_API_USER }} |
| 94 | + password: ${{ secrets.DOCKER_HUB_API_TOKEN }} |
| 95 | + |
| 96 | + - name: Build & Push Multi-Platform Container Image to Hub |
| 97 | + uses: docker/build-push-action@v2 |
| 98 | + with: |
| 99 | + context: . # build context is workspace so we can copy artifacts from ./dist/ |
| 100 | + builder: ${{ steps.buildx.outputs.name }} |
| 101 | + platforms: linux/amd64,linux/arm/v7,linux/arm64 |
| 102 | + push: true |
| 103 | + tags: ${{ steps.compose_tags.outputs.container_tags }} |
0 commit comments