Skip to content

Commit

Permalink
ci: Try selecting environment based on trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathonReinhart committed Jul 10, 2023
1 parent 64a49b3 commit 25218b8
Showing 1 changed file with 72 additions and 10 deletions.
82 changes: 72 additions & 10 deletions .github/workflows/publish-main.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
name: Upload to Test PyPI

# On every push to main, push to Test PyPI
on:
# On every push to main, push to Test PyPI
push:
#branches:
# - main
# - main # TODO

# On new releases, push to PyPI
release:
types: [published]

# TODO: De-duplicate with build-test.yml
# TODO: De-duplicate with python-publish-release.yml

jobs:
build:
runs-on: ubuntu-latest

# Map a step output to a job output
outputs:
ENV_NAME: ${{ steps.environment_select.outputs.ENV_NAME}}

steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down Expand Up @@ -45,13 +52,63 @@ jobs:
name: python-dist
path: dist/

- name: Environment select
# https://www.codewrecks.com/post/github/choose-environment-from-branch/
# https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs
# https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
id: environment_select
run: |
echo "github.event_name=\"${{ github.event_name }}\""
echo "github.ref_type=\"${{ github.ref_type }}\""
echo "github.ref=\"${{ github.ref }}\""
case "${{ github.event_name }}" in
push)
echo "Triggered by push to ${{ github.ref }}"
if [ "${{ github.ref_type }}" == "branch" ]; then
if [ "${{ github.ref_name }}" == "publish-to-pypi" ]; then # TODO: main
echo "Will deploy to TestPyPI"
echo "ENV_NAME=TestPyPI" >> $GITHUB_OUTPUT
else
echo "Unexpected ref_name: \"${{ github.ref_name }}\""
exit 6
fi
else
echo "Unexpected ref_type: \"${{ github.ref_type}}\""
exit 6
fi
;;
release)
echo "Triggered by release to ${{ github.ref }}"
if [ "${{ github.ref_type }}" == "tag" ]; then
echo "Will deploy to PyPI"
echo "ENV_NAME=PyPI" >> $GITHUB_OUTPUT
else
echo "Unexpected ref_type: \"${{ github.ref_type}}\""
exit 6
fi
;;
*)
echo "Unexpected trigger: ${{ github.event_name }}"
exit 6
;;
esac
echo "Contents of \$GITHUB_OUTPUT ($GITHUB_OUTPUT):"
cat $GITHUB_OUTPUT
pypi-publish:
name: Upload release to Test PyPI
# TODO dynamically select name based on env
name: Upload release to PyPI
needs: [build]
runs-on: ubuntu-latest
environment:
name: TestPyPI
url: ${{ vars.PROJECT_URL }}
name: ${{ needs.build.outputs.ENV_NAME }}
url: ${{ vars.PROJECT_URL }} # from environment
# TODO: We could compute the exact URL in environment_select
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
Expand All @@ -61,11 +118,16 @@ jobs:
name: python-dist
path: dist/

- name: Display downloaded files
run: ls -R
- name: Dry run
run: |
ls -R
echo "Selected environment: \"${{ needs.build.outputs.ENV_NAME }}\""
echo "PROJECT_URL: ${{ vars.PROJECT_URL }}"
echo "UPLOAD_URL: ${{ vars.UPLOAD_URL }}"
- name: Publish
if: false
uses: pypa/gh-action-pypi-publish@release/v1
with:
print-hash: true
repository-url: ${{ vars.UPLOAD_URL }}
repository-url: ${{ vars.UPLOAD_URL }} # from environment

0 comments on commit 25218b8

Please sign in to comment.