Skip to content

Commit 1f58e45

Browse files
authored
chore(ci): add workflow to publish Python SDK (#3572)
1 parent 76c032a commit 1f58e45

File tree

8 files changed

+177
-73
lines changed

8 files changed

+177
-73
lines changed

.dagger/release.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,6 @@ func (m *Openmeter) binaryArchive(version string, platform dagger.Platform) *dag
102102
)
103103
}
104104

105-
func (m *Openmeter) publishPythonSdk(ctx context.Context, version string, pypiToken *dagger.Secret) error {
106-
_, err := dag.Python(dagger.PythonOpts{
107-
Container: dag.Python(dagger.PythonOpts{Container: dag.Container().From("pypy:3.10-7.3.16-slim")}).
108-
WithPipCache(cacheVolume("pip")).
109-
Container().
110-
WithExec([]string{"pip", "--disable-pip-version-check", "install", "pipx"}).
111-
WithEnvVariable("PATH", "${PATH}:/root/.local/bin", dagger.ContainerWithEnvVariableOpts{Expand: true}).
112-
WithExec([]string{"pipx", "install", "poetry"}),
113-
}).
114-
WithSource(m.Source.Directory("api/client/python")). // TODO: generate SDK on the fly?
115-
Container().
116-
WithExec([]string{"poetry", "install"}).
117-
WithExec([]string{"poetry", "version", version}).
118-
WithSecretVariable("POETRY_PYPI_TOKEN_PYPI", pypiToken).
119-
WithEnvVariable("CACHE_BUSTER", time.Now().Format(time.RFC3339Nano)).
120-
WithExec([]string{"poetry", "publish", "--build"}).
121-
Sync(ctx)
122-
123-
return err
124-
}
125-
126105
// TODO: keep in sync with api/client/javascript/Makefile for now, if the release process is moved to nix, can be removed
127106
func (m *Openmeter) PublishJavascriptSdk(ctx context.Context, version string, tag string, npmToken *dagger.Secret) error {
128107
// TODO: generate SDK on the fly?

.github/workflows/release.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,48 @@ jobs:
5353
GITHUB_TOKEN: ${{ github.token }}
5454
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
5555
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
56+
57+
sdk-python-release:
58+
name: Python SDK Release
59+
runs-on: depot-ubuntu-latest-8
60+
61+
steps:
62+
- name: Checkout repository
63+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
64+
with:
65+
ref: ${{ github.head_ref }}
66+
persist-credentials: false
67+
68+
- name: Get short SHA
69+
id: get-short-sha
70+
run: |
71+
id=$(echo ${{ github.sha }} | cut -c 1-12)
72+
echo "id=${id}" >> $GITHUB_OUTPUT
73+
74+
# If you keep using Nix for your dev shell / tooling, retain these steps
75+
- name: Set up Nix
76+
uses: nixbuild/nix-quick-install-action@2c9db80fb984ceb1bcaa77cdda3fdf8cfba92035 # v34
77+
with:
78+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
79+
nix_conf: |
80+
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
81+
keep-env-derivations = true
82+
keep-outputs = true
83+
84+
- name: Restore Nix store
85+
uses: nix-community/cache-nix-action/restore@135667ec418502fa5a3598af6fb9eb733888ce6a # v6.1.3
86+
with:
87+
primary-key: ${{ runner.os }}-openmeter-nix-build-${{ github.ref_name }}-${{ hashFiles('flake.*') }}
88+
restore-prefixes-first-match: |
89+
${{ runner.os }}-openmeter-nix-build-${{ github.ref_name }}-
90+
${{ runner.os }}-openmeter-nix-build-main-${{ hashFiles('flake.*') }}
91+
${{ runner.os }}-openmeter-nix-build-main-
92+
${{ runner.os }}-openmeter-nix-build-
93+
94+
- name: Publish Python package via Nix make target
95+
run: |
96+
nix develop --impure .#ci -c make -C api/client/python publish-python-sdk
97+
env:
98+
PY_SDK_RELEASE_VERSION: ${{ github.ref_name }}
99+
COMMIT_SHORT_SHA: ${{ steps.get-short-sha.outputs.id }}
100+
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Python SDK Beta Release
2+
3+
permissions:
4+
contents: read
5+
6+
on:
7+
workflow_dispatch:
8+
push:
9+
branches:
10+
- main
11+
12+
jobs:
13+
release-beta-sdk:
14+
name: Python SDK Beta Release
15+
runs-on: depot-ubuntu-latest-8
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
20+
with:
21+
ref: ${{ github.head_ref }}
22+
persist-credentials: false
23+
24+
- name: Get short SHA
25+
id: get-short-sha
26+
run: |
27+
id=$(echo ${{ github.sha }} | cut -c 1-12)
28+
echo "id=${id}" >> $GITHUB_OUTPUT
29+
30+
# If you keep using Nix for your dev shell / tooling, retain these steps
31+
- name: Set up Nix
32+
uses: nixbuild/nix-quick-install-action@2c9db80fb984ceb1bcaa77cdda3fdf8cfba92035 # v34
33+
with:
34+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
35+
nix_conf: |
36+
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
37+
keep-env-derivations = true
38+
keep-outputs = true
39+
40+
- name: Restore Nix store
41+
uses: nix-community/cache-nix-action/restore@135667ec418502fa5a3598af6fb9eb733888ce6a # v6.1.3
42+
with:
43+
primary-key: ${{ runner.os }}-openmeter-nix-build-${{ github.ref_name }}-${{ hashFiles('flake.*') }}
44+
restore-prefixes-first-match: |
45+
${{ runner.os }}-openmeter-nix-build-${{ github.ref_name }}-
46+
${{ runner.os }}-openmeter-nix-build-main-${{ hashFiles('flake.*') }}
47+
${{ runner.os }}-openmeter-nix-build-main-
48+
${{ runner.os }}-openmeter-nix-build-
49+
50+
- name: Publish Python package via Nix make target
51+
run: |
52+
nix develop --impure .#ci -c make -C api/client/python publish-python-sdk
53+
env:
54+
PY_SDK_RELEASE_TAG: alpha
55+
COMMIT_SHORT_SHA: ${{ steps.get-short-sha.outputs.id }}
56+
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}

.github/workflows/sdk-python.yaml

Lines changed: 0 additions & 52 deletions
This file was deleted.

api/client/python/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
2+
3+
.PHONY: publish-python-sdk
4+
publish-python-sdk: ## Publish Python SDK
5+
$(call print-target)
6+
./scripts/release.sh
7+
8+
.PHONY: help
9+
.DEFAULT_GOAL := help
10+
help:
11+
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
12+
13+
# Variable outputting/exporting rules
14+
var-%: ; @echo $($*)
15+
varexport-%: ; @echo $*=$($*)
16+
17+
define print-target
18+
@printf "Executing target: \033[36m$@\033[0m\n"
19+
endef

api/client/python/openmeter/__init__.py

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/client/python/openmeter/_commit.py

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env sh
2+
3+
set -euo pipefail
4+
5+
# Determine PY_SDK_RELEASE_VERSION if not provided
6+
if [ -z "${PY_SDK_RELEASE_VERSION:-}" ]; then
7+
# Validate PY_SDK_RELEASE_TAG
8+
if [ -z "${PY_SDK_RELEASE_TAG:-}" ]; then
9+
echo "ERROR: PY_SDK_RELEASE_VERSION or PY_SDK_RELEASE_TAG is required"
10+
exit 1
11+
fi
12+
13+
if [ "$PY_SDK_RELEASE_TAG" != "alpha" ]; then
14+
echo "ERROR: PY_SDK_RELEASE_TAG must be 'alpha'"
15+
exit 1
16+
fi
17+
18+
LATEST_VERSION=$(curl -s https://pypi.org/pypi/openmeter/json | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4)
19+
if [ -z "$LATEST_VERSION" ]; then
20+
PY_SDK_RELEASE_VERSION="1.0.0a0"
21+
else
22+
BASE_VERSION=$(echo "$LATEST_VERSION" | grep -o '^[0-9]*\.[0-9]*\.[0-9]*')
23+
PRE_NUM=$(echo "$LATEST_VERSION" | grep -o 'a[0-9]*' | grep -o '[0-9]*' || echo "-1")
24+
NEXT_NUM=$((PRE_NUM + 1))
25+
PY_SDK_RELEASE_VERSION="${BASE_VERSION}a${NEXT_NUM}"
26+
fi
27+
export PY_SDK_RELEASE_VERSION
28+
fi
29+
30+
# Set COMMIT_SHORT_SHA if not provided
31+
if [ -z "${COMMIT_SHORT_SHA:-}" ]; then
32+
COMMIT_SHORT_SHA=$(git rev-parse --short=12 HEAD)
33+
fi
34+
35+
# Convert PY_SDK_RELEASE_VERSION to a valid Python version
36+
export PY_SDK_RELEASE_VERSION=$(echo "$PY_SDK_RELEASE_VERSION" | sed -E 's/^v//' | sed -E 's/-alpha\.?/a/; s/-beta\.?/b/;')
37+
38+
# Update poetry version
39+
poetry version "$PY_SDK_RELEASE_VERSION"
40+
41+
# Write version and commit files
42+
printf "VERSION = \"%s\"" "$PY_SDK_RELEASE_VERSION" > openmeter/_version.py || true
43+
printf "COMMIT = \"%s\"" "$COMMIT_SHORT_SHA" > openmeter/_commit.py || true
44+
45+
# Clean dist directory to avoid prompts about existing files
46+
rm -rf dist
47+
48+
# Publish with poetry
49+
poetry publish --build --no-interaction
50+
51+
echo "Published Python SDK version $PY_SDK_RELEASE_VERSION"
52+

0 commit comments

Comments
 (0)