Publish Package #40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish Package | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: The environment to publish to. | |
| default: 'none' | |
| required: true | |
| type: choice | |
| options: | |
| - none | |
| - pypi | |
| - testpypi | |
| env: | |
| dist-artifact-name: package-distribution-packages | |
| environment: ${{ github.event_name == 'release' && 'pypi' || inputs.environment }} | |
| environment-info: | | |
| { | |
| "pypi": { | |
| "base-url": "https://pypi.org", | |
| "upload-url": "https://upload.pypi.org/legacy/" | |
| }, | |
| "testpypi": { | |
| "base-url": "https://test.pypi.org", | |
| "upload-url": "https://test.pypi.org/legacy/" | |
| } | |
| } | |
| jobs: | |
| check_package: | |
| name: Check package | |
| uses: ./.github/workflows/PR.yml | |
| build_package: | |
| name: Build package | |
| runs-on: ubuntu-latest | |
| needs: [check_package] | |
| steps: | |
| - name: Check out repo | |
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
| - name: Set up Python | |
| uses: ni/python-actions/setup-python@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1 | |
| - name: Set up Poetry | |
| uses: ni/python-actions/setup-poetry@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1 | |
| # If the version is 0.1.0-alpha.0, this will set the version to 0.1.0 | |
| - name: Promote package version to release | |
| run: poetry version patch | |
| - name: Check project version | |
| if: github.event_name == 'release' | |
| uses: ni/python-actions/check-project-version@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1 | |
| - name: Build distribution packages | |
| run: poetry build | |
| - name: Upload build artifacts | |
| uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 | |
| with: | |
| name: ${{ env.dist-artifact-name }} | |
| path: dist/* | |
| publish_to_pypi: | |
| name: Publish package to PyPI | |
| if: github.event_name == 'release' || inputs.environment != 'none' | |
| runs-on: ubuntu-latest | |
| needs: [build_package] | |
| environment: | |
| # This logic is duplicated because `name` doesn't support the `env` context. | |
| name: ${{ github.event_name == 'release' && 'pypi' || inputs.environment }} | |
| url: ${{ fromJson(env.environment-info)[env.environment].base-url }}/p/ni-python-styleguide | |
| permissions: | |
| id-token: write | |
| steps: | |
| - name: Download build artifacts | |
| uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 | |
| with: | |
| name: ${{ env.dist-artifact-name }} | |
| path: dist/ | |
| - run: ls -lR | |
| - name: Upload to ${{ env.environment }} | |
| uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4 | |
| with: | |
| repository-url: ${{ fromJson(env.environment-info)[env.environment].upload-url }} | |
| update_version: | |
| name: Update package version to next alpha version | |
| runs-on: ubuntu-latest | |
| needs: [build_package] | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Check out repo | |
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
| - name: Set up Python | |
| uses: ni/python-actions/setup-python@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1 | |
| - name: Set up Poetry | |
| uses: ni/python-actions/setup-poetry@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1 | |
| # If the version is 0.1.0-alpha.0, this will set the version to 0.1.0 | |
| - name: Promote package version to release | |
| run: poetry version patch | |
| - name: Update project version | |
| uses: ni/python-actions/update-project-version@5286c12d65d90b2ea738bd57d452dc4366497581 # v0.4.1 | |
| with: | |
| # The default GITHUB_TOKEN cannot trigger PR workflows. | |
| token: ${{ secrets.ADMIN_PAT }} | |
| version-rule: "prepatch" | |
| use-dev-suffix: false |