Trigger publish-to-pypi
job when `check-version.outputs.new_version…
#2873
This file contains 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: Unittests & Auto-publish | ||
# Allow to trigger the workflow manually (e.g. when deps changes) or on tag pushes. | ||
on: | ||
push: | ||
branches: | ||
- '**' | ||
tags: | ||
- 'v*' # Trigger only on tags starting from 'v'. | ||
workflow_dispatch: # For triggering publication workflows manually, on demend, from GitHub UI. | ||
inputs: | ||
publish_to_pypi: # Creates a boolean `github.event.inputs.publish_to_pypi` variable. | ||
description: 'Publish to PyPI?' | ||
required: false | ||
type: boolean | ||
default: false | ||
publish_to_testpypi: | ||
description: 'Publish to TestPyPI?' | ||
required: false | ||
type: boolean | ||
default: false | ||
jobs: | ||
pytest-job: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 30 | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
steps: | ||
- uses: actions/checkout@v3 | ||
# Install deps | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
# Uncomment to cache of pip dependencies (if tests too slow) | ||
cache: pip | ||
cache-dependency-path: '**/pyproject.toml' | ||
- run: pip --version | ||
- run: pip install -e .[dev] --config-settings editable_mode=strict | ||
- run: pip freeze | ||
# Run tests (in parallel) | ||
- name: Run core tests | ||
run: pytest -vv -n auto | ||
build: | ||
name: Build distribution | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 30 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
persist-credentials: false | ||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
- name: Install pypa/build | ||
run: python3 -m pip install build --user | ||
- name: Build a binary wheel and a source tarball | ||
run: python3 -m build | ||
- name: Store the distribution packages | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: python-package-distributions | ||
path: dist/ | ||
# Check if the current `meridian.__version__` semver has been incremented. If so, sets a | ||
# `new_version` output. | ||
check-version: | ||
name: Version check | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
outputs: | ||
new_tag: ${{ steps.version_check.outputs.new_version }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 # Important: fetch all tags for comparison | ||
# Install deps | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
cache: pip | ||
cache-dependency-path: '**/pyproject.toml' | ||
- run: pip install -e .[dev] --config-settings editable_mode=strict | ||
- run: pip install semver | ||
- id: version_check | ||
name: Check Version | ||
shell: bash | ||
# !! This relies on the git repository having "v{major}.{minor}.{patch}" named tags. | ||
run: | | ||
CURRENT_VERSION=$(python3 -c "import meridian; print(meridian.__version__)") | ||
PREVIOUS_TAG=$(git describe --tags --abbrev=0 --match 'v*' 2>/dev/null || echo "v0.0.0") | ||
PREVIOUS_VERSION=$(echo "$PREVIOUS_TAG" | sed 's/^v//') | ||
echo "CURRENT_VERSION: $CURRENT_VERSION" | ||
echo "PREVIOUS_TAG: $PREVIOUS_TAG" | ||
echo "PREVIOUS_VERSION: $PREVIOUS_VERSION" | ||
if [[ $(python3 -m semver compare "$CURRENT_VERSION" "$PREVIOUS_VERSION") -eq 1 ]]; then | ||
echo "New version detected: $CURRENT_VERSION" | ||
NEW_VERSION=$CURRENT_VERSION | ||
else | ||
echo "No version increment detected." | ||
fi | ||
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | ||
create-tag: | ||
name: Create Version Tag | ||
runs-on: ubuntu-latest | ||
needs: check-version | ||
if: ${{ needs.check-version.outputs.new_version != '' }} # Only run if new_version was discovered. | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Create and Push Tag | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const newTag = 'v${{ needs.check-version.outputs.new_version }}'; | ||
try { | ||
await github.rest.git.createTag({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
tag: newTag, | ||
message: `Release ${newTag}`, | ||
object: context.sha, | ||
type: 'commit' | ||
}); | ||
await github.rest.git.createRef({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
ref: `refs/tags/${newTag}`, | ||
sha: context.sha | ||
}); | ||
console.log(`Created and pushed new tag: ${newTag}`); | ||
} catch (error) { | ||
console.error('Error creating tag:', error); | ||
core.setFailed(`Failed to create tag: ${error.message}`); | ||
} | ||
publish-to-pypi: | ||
name: >- | ||
Publish Python distribution to PyPI | ||
needs: check-version | ||
# Check if it's a tag push or the `publish_to_pypi` workflow is selected in a manual trigger. | ||
if: > | ||
(github.ref_type == 'tag' && startsWith(github.ref, 'refs/tags/')) | ||
|| (github.event_name == 'workflow_dispatch' | ||
&& github.event.inputs.publish_to_pypi == 'true') | ||
|| needs.check-version.outputs.new_version != '' | ||
needs: | ||
- build | ||
- pytest-job | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: pypi | ||
url: https://pypi.org/p/google-meridian | ||
permissions: | ||
id-token: write # IMPORTANT: mandatory for trusted publishing | ||
steps: | ||
- name: Download all the dists | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: python-package-distributions | ||
path: dist/ | ||
- name: Publish distribution to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
with: | ||
verbose: true | ||
publish-to-testpypi: | ||
name: Publish Python distribution to TestPyPI | ||
# Check if the `publish_to_testpypi` workflow is selected in a manual trigger. | ||
if: > | ||
(github.event_name == 'workflow_dispatch' | ||
&& github.event.inputs.publish_to_testpypi == 'true') | ||
needs: | ||
- build | ||
- pytest-job | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: testpypi | ||
url: https://test.pypi.org/p/meridian | ||
permissions: | ||
id-token: write # IMPORTANT: mandatory for trusted publishing | ||
steps: | ||
- name: Download all the dists | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: python-package-distributions | ||
path: dist/ | ||
- name: Publish distribution to TestPyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
with: | ||
repository-url: https://test.pypi.org/legacy/ | ||
verbose: true |