|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +name: Build, test, and upload PyPI package |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - "main" |
| 9 | + tags: |
| 10 | + - "v*" |
| 11 | + pull_request: |
| 12 | + branches: |
| 13 | + - "main" |
| 14 | + release: |
| 15 | + types: |
| 16 | + - published |
| 17 | + |
| 18 | +env: |
| 19 | + LC_ALL: en_US.UTF-8 |
| 20 | + |
| 21 | +defaults: |
| 22 | + run: |
| 23 | + shell: bash |
| 24 | + |
| 25 | +permissions: |
| 26 | + contents: read |
| 27 | + |
| 28 | +jobs: |
| 29 | + # Create and verify release artifacts |
| 30 | + # - build source dist (tar ball) and wheel |
| 31 | + # - validate artifacts with various tools |
| 32 | + # - upload artifacts to GHA |
| 33 | + build-package: |
| 34 | + name: Build and check packages |
| 35 | + runs-on: ubuntu-latest |
| 36 | + steps: |
| 37 | + - name: "Harden Runner" |
| 38 | + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 |
| 39 | + with: |
| 40 | + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs |
| 41 | + |
| 42 | + |
| 43 | + - name: "Checkout" |
| 44 | + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 |
| 45 | + with: |
| 46 | + # for setuptools-scm |
| 47 | + fetch-depth: 0 |
| 48 | + |
| 49 | + - name: "Build and Inspect" |
| 50 | + uses: hynek/build-and-inspect-python-package@b4fc3f6ba2b3da04f09659be99e2a29fb6146a61 # v2.6.0 |
| 51 | + |
| 52 | + # push to Test PyPI on |
| 53 | + # - a new GitHub release is published |
| 54 | + # - a PR is merged into main branch |
| 55 | + publish-test-pypi: |
| 56 | + name: Publish packages to test.pypi.org |
| 57 | + # environment: publish-test-pypi |
| 58 | + if: ${{ (github.repository_owner == 'instructlab') && ((github.event.action == 'published') || ((github.event_name == 'push') && (github.ref == 'refs/heads/main'))) }} |
| 59 | + permissions: |
| 60 | + contents: read |
| 61 | + # see https://docs.pypi.org/trusted-publishers/ |
| 62 | + id-token: write |
| 63 | + runs-on: ubuntu-latest |
| 64 | + needs: build-package |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: "Harden Runner" |
| 68 | + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 |
| 69 | + with: |
| 70 | + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs |
| 71 | + |
| 72 | + - name: "Download build artifacts" |
| 73 | + uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7 |
| 74 | + with: |
| 75 | + name: Packages |
| 76 | + path: dist |
| 77 | + |
| 78 | + - name: "Upload to Test PyPI" |
| 79 | + uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14 |
| 80 | + with: |
| 81 | + repository-url: https://test.pypi.org/legacy/ |
| 82 | + |
| 83 | + # push to Production PyPI on |
| 84 | + # - a new GitHub release is published |
| 85 | + publish-pypi: |
| 86 | + name: Publish release to pypi.org |
| 87 | + # environment: publish-pypi |
| 88 | + if: ${{ (github.repository_owner == 'instructlab') && (github.event.action == 'published') }} |
| 89 | + permissions: |
| 90 | + # see https://docs.pypi.org/trusted-publishers/ |
| 91 | + id-token: write |
| 92 | + # allow gh release upload |
| 93 | + contents: write |
| 94 | + |
| 95 | + runs-on: ubuntu-latest |
| 96 | + needs: build-package |
| 97 | + |
| 98 | + steps: |
| 99 | + - name: "Harden Runner" |
| 100 | + uses: step-security/harden-runner@f086349bfa2bd1361f7909c78558e816508cdc10 # v2.8.0 |
| 101 | + with: |
| 102 | + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs |
| 103 | + |
| 104 | + - name: "Download build artifacts" |
| 105 | + uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7 |
| 106 | + with: |
| 107 | + name: Packages |
| 108 | + path: dist |
| 109 | + |
| 110 | + - name: "Sigstore sign package" |
| 111 | + uses: sigstore/gh-action-sigstore-python@61f6a500bbfdd9a2a339cf033e5421951fbc1cd2 # v2.1.1 |
| 112 | + with: |
| 113 | + inputs: | |
| 114 | + ./dist/*.tar.gz |
| 115 | + ./dist/*.whl |
| 116 | +
|
| 117 | + - name: "Upload artifacts and signatures to GitHub release" |
| 118 | + run: | |
| 119 | + gh release upload '${{ github.ref_name }}' dist/* --repo '${{ github.repository }}' |
| 120 | + env: |
| 121 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 122 | + |
| 123 | + # PyPI does not accept .sigstore artifacts and |
| 124 | + # gh-action-pypi-publish has no option to ignore them. |
| 125 | + - name: "Remove sigstore signatures before uploading to PyPI" |
| 126 | + run: | |
| 127 | + rm ./dist/*.sigstore |
| 128 | +
|
| 129 | + - name: "Upload to PyPI" |
| 130 | + uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14 |
0 commit comments