Skip to content

Commit b62d23b

Browse files
ronellecaguioaThe Meridian Authors
authored andcommitted
Create workflow for unified schema package
PiperOrigin-RevId: 830951056
1 parent 04f9192 commit b62d23b

File tree

5 files changed

+156
-3
lines changed

5 files changed

+156
-3
lines changed

.github/actions/build/action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ runs:
2020
- name: Build a binary wheel and a source tarball
2121
shell: bash
2222
run: python3 -m build
23+
working-directory: ${{ inputs.working_directory }}
2324
- name: Store the distribution packages
2425
uses: actions/upload-artifact@v4
2526
with:
2627
name: python-package-distributions
27-
path: dist/
28+
path: ${{ inputs.output_path }}

.github/actions/create-version-tag/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ runs:
1414
uses: actions/github-script@v6
1515
with:
1616
script: |
17-
const newTag = 'v${{ inputs.new_version }}';
17+
const newTag = '${{ inputs.new_version }}';
1818
try {
1919
await github.rest.git.createTag({
2020
owner: context.repo.owner,

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jobs:
3838
- uses: ./.github/actions/build
3939
with:
4040
python_version: ${{ env.USE_PYTHON_VERSION }}
41+
working_directory: .
42+
output_path: dist/
4143

4244
# Include for sanity checking that version increment parsing and checking logic works.
4345
check-version:
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
name: Publish Proto # For tagging and publishing releases.
2+
3+
on:
4+
push:
5+
branches:
6+
- main # This entire workflow only runs on a push to the `main` branch (i.e. POST SUBMIT).
7+
paths:
8+
- 'proto/**' # Trigger only on changes to the proto directory.
9+
tags:
10+
- 'proto-v*' # Trigger only on pushed tags whose names start with 'proto-v'.
11+
workflow_dispatch:
12+
inputs:
13+
publish_to_pypi: # Creates a boolean `github.event.inputs.publish_to_pypi` variable.
14+
description: 'Publish to PyPI'
15+
required: false
16+
type: boolean
17+
default: false
18+
publish_to_testpypi:
19+
description: 'Publish to TestPyPI'
20+
required: false
21+
type: boolean
22+
default: false
23+
24+
env:
25+
USE_PYTHON_VERSION: "3.11"
26+
PROTO_DIR: "proto"
27+
PYPI_PACKAGE_NAME: "mmm-schema"
28+
29+
jobs:
30+
build:
31+
runs-on: ubuntu-latest
32+
timeout-minutes: 15
33+
steps:
34+
- uses: actions/checkout@v4
35+
- uses: ./.github/actions/build
36+
with:
37+
python_version: ${{ env.USE_PYTHON_VERSION }}
38+
working_directory: ${{ env.PROTO_DIR }}
39+
output_path: ${{env.PROTO_DIR}}/dist/
40+
41+
# Check if the current `version` semver has been incremented. If so, set a
42+
# `new_version` output.
43+
check-proto-version:
44+
runs-on: ubuntu-latest
45+
timeout-minutes: 10
46+
outputs:
47+
new_version: ${{ steps.read_toml.outputs.new_version }}
48+
steps:
49+
- name: Checkout repository
50+
uses: actions/checkout@v4
51+
52+
- name: Read package version from pyproject.toml
53+
uses: SebRollen/toml-action@v1.2.0
54+
id: read_toml
55+
with:
56+
file: pyproject.toml
57+
field: 'project.version'
58+
59+
- name: Compare versions
60+
run: |
61+
CURRENT_VERSION = "${{ steps.read_toml.outputs.value }}"
62+
PREVIOUS_TAG=$(git describe --tags --abbrev=0 --match 'v*' || { echo "Error: No matching previous tag found." >&2; exit 1; })
63+
PREVIOUS_VERSION=$(echo "$PREVIOUS_TAG" | sed 's/^v//')
64+
65+
echo "CURRENT_VERSION: $CURRENT_VERSION"
66+
echo "PREVIOUS_TAG: $PREVIOUS_TAG"
67+
echo "PREVIOUS_VERSION: $PREVIOUS_VERSION"
68+
69+
if [[ $(python3 -m semver compare "$CURRENT_VERSION" "$PREVIOUS_VERSION") -eq 1 ]]; then
70+
echo "New version detected: $CURRENT_VERSION"
71+
NEW_VERSION=$CURRENT_VERSION
72+
else
73+
echo "No version increment detected."
74+
fi
75+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
76+
77+
create-proto-tag:
78+
needs: check-proto-version
79+
# Only run if new_version was discovered, and do this only on the main branch.
80+
if: >
81+
github.ref == 'refs/heads/main'
82+
&& needs.check-version.outputs.new_version != ''
83+
runs-on: ubuntu-latest
84+
permissions:
85+
contents: write # Allow to create tags
86+
steps:
87+
- uses: actions/checkout@v4
88+
- uses: ./.github/actions/create-version-tag
89+
with:
90+
new_version: proto-v${{ needs.check-version.outputs.new_version }}
91+
92+
publish-to-pypi:
93+
name: Publish Proto Python distribution to PyPI
94+
needs:
95+
- build
96+
- check-proto-version
97+
# Check if it's a tag push, or the `publish_to_pypi` workflow is selected in a manual trigger.
98+
# or if a new version was discovered in a push to the `main` branch.
99+
if: >
100+
github.repository == 'google/meridian' && (
101+
(github.ref_type == 'tag' && startsWith(github.ref, 'refs/tags/proto-v'))
102+
|| (github.event_name == 'workflow_dispatch'
103+
&& github.event.inputs.publish_to_pypi == 'true')
104+
)
105+
runs-on: ubuntu-latest
106+
environment:
107+
name: pypi-proto
108+
url: https://pypi.org/p/${{ env.PYPI_PACKAGE_NAME }}
109+
permissions:
110+
id-token: write # IMPORTANT: mandatory for trusted publishing
111+
steps:
112+
- name: Download all the dists
113+
uses: actions/download-artifact@v4
114+
with:
115+
pattern: proto-python-package-distributions
116+
path: dist/
117+
118+
- name: Publish distribution to PyPI
119+
uses: pypa/gh-action-pypi-publish@release/v1
120+
with:
121+
verbose: true
122+
123+
publish-to-testpypi:
124+
name: Publish Proto Python distribution to TestPyPI
125+
needs: build
126+
# Check if the `publish_to_testpypi` workflow is selected in a manual trigger.
127+
if: >
128+
github.repository == 'google/meridian' && (
129+
(github.event_name == 'workflow_dispatch'
130+
&& github.event.inputs.publish_to_testpypi == 'true')
131+
)
132+
runs-on: ubuntu-latest
133+
environment:
134+
name: testpypi-proto
135+
url: https://test.pypi.org/p/${{ env.PYPI_PACKAGE_NAME }}
136+
permissions:
137+
id-token: write # IMPORTANT: mandatory for trusted publishing
138+
steps:
139+
- name: Download all the dists
140+
uses: actions/download-artifact@v4
141+
with:
142+
pattern: proto-python-package-distribution
143+
path: dist/
144+
- name: Publish distribution to TestPyPI
145+
uses: pypa/gh-action-pypi-publish@release/v1
146+
with:
147+
repository-url: https://test.pypi.org/legacy/
148+
verbose: true

.github/workflows/publish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131
- uses: ./.github/actions/build
3232
with:
3333
python_version: ${{ env.USE_PYTHON_VERSION }}
34+
working_directory: .
35+
output_path: dist/
3436

3537
# Check if the current `meridian.__version__` semver has been incremented. If so, set a
3638
# `new_version` output.
@@ -59,7 +61,7 @@ jobs:
5961
- uses: actions/checkout@v4
6062
- uses: ./.github/actions/create-version-tag
6163
with:
62-
new_version: ${{ needs.check-version.outputs.new_version }}
64+
new_version: v${{ needs.check-version.outputs.new_version }}
6365

6466
publish-to-pypi:
6567
name: Publish Python distribution to PyPI

0 commit comments

Comments
 (0)