Skip to content

Publish Proto

Publish Proto #6

Workflow file for this run

name: Publish Proto # For tagging and publishing releases.
on:
workflow_dispatch:
inputs:
publish_proto_to_pypi: # Creates a boolean `github.event.inputs.publish_to_pypi` variable.
description: 'Publish Proto to PyPI'
required: false
type: boolean
default: false
publish_proto_to_testpypi:
description: 'Publish Proto to TestPyPI'
required: false
type: boolean
default: false
env:
USE_PYTHON_VERSION: "3.11"
PROTO_DIR: "proto"
PYPI_PACKAGE_NAME: "mmm-proto-schema"
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/build
with:
python_version: ${{ env.USE_PYTHON_VERSION }}
working_directory: ${{ env.PROTO_DIR }}
output_path: ${{env.PROTO_DIR}}/dist/
# Check if the current `version` semver has been incremented. If so, set a
# `new_version` output.
check-proto-version:
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
new_version: ${{ steps.compare_versions.outputs.new_version }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Important: fetch all tags for comparison
fetch-tags: true
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.USE_PYTHON_VERSION }}
- name: Install semver
shell: bash
run: pip install semver
- name: Get version
id: get_version
shell: bash
run: |
VERSION=$(python -c """
import sys
import tomllib
try:
filepath = '${{ env.PROTO_DIR }}/pyproject.toml'
with open(filepath, 'rb') as f:
data = tomllib.load(f)
version = data.get('project', {}).get('version', '')
if version:
print(version)
else:
print(f"Error: Version not found in {filepath} under [project.version]")
sys.exit(1)
except FileNotFoundError:
print(f"Error: File {filepath} not found", file=sys.stderr)
sys.exit(1)
""" )
if [ $? -ne 0 ]; then
echo "Failed to get version from ${{ env.PROTO_DIR}}/pyproject.toml"
exit 1
fi
echo "value=$VERSION" >> $GITHUB_OUTPUT
- name: Compare versions
id: compare_versions
run: |
CURRENT_VERSION="${{ steps.get_version.outputs.value }}"
PREVIOUS_TAG=$(git describe --tags --abbrev=0 --match 'v*' || { echo "Error: No matching previous tag found." >&2; exit 1; })
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-proto-tag:
needs: check-proto-version
# Only run if new_version was discovered, and do this only on the main branch.
if: >
github.ref == 'refs/heads/main'
&& needs.check-proto-version.outputs.new_version != ''
runs-on: ubuntu-latest
permissions:
contents: write # Allow to create tags
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/create-version-tag
with:
new_version: proto-v${{ needs.check-proto-version.outputs.new_version }}
publish-proto-to-pypi:
name: Publish Proto Python distribution to PyPI
needs:
- build
- check-proto-version
# Check if it's a tag push, or the `publish_proto_to_pypi` workflow is selected in a manual trigger.
# or if a new version was discovered in a push to the `main` branch.
if: >
github.repository == 'google/meridian' && (
(github.ref_type == 'tag' && startsWith(github.ref, 'refs/tags/proto-v'))
|| (github.event_name == 'workflow_dispatch'
&& github.event.inputs.publish_proto_to_pypi == 'true')
)
runs-on: ubuntu-latest
environment:
name: pypi-proto
url: https://pypi.org/p/${{ env.PYPI_PACKAGE_NAME }}
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
pattern: proto-python-package-distributions
path: dist/
- name: Publish distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
verbose: true
publish-proto-to-testpypi:
name: Publish Proto Python distribution to TestPyPI
needs: build
# Check if the `publish_proto_to_testpypi` workflow is selected in a manual trigger.
if: >
github.repository == 'google/meridian' && (
(github.event_name == 'workflow_dispatch'
&& github.event.inputs.publish_proto_to_testpypi == 'true')
)
runs-on: ubuntu-latest
environment:
name: testpypi-proto
url: https://test.pypi.org/p/${{ env.PYPI_PACKAGE_NAME }}
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing
steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
pattern: proto-python-package-distribution
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