Skip to content

Commit 3bd4328

Browse files
added reusable action for publishing validators to private pypi
1 parent 30eeb4c commit 3bd4328

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Publish Validator PyPi
2+
description: Re-Usable action to publish a Validator to Guardrails PyPi
3+
inputs:
4+
github_token:
5+
description: 'GitHub Token'
6+
required: true
7+
guardrails_token:
8+
description: 'Guardrails Token'
9+
required: true
10+
pypi_repository_url:
11+
description: 'PyPi Repository URL'
12+
required: false
13+
default: 'https://pypi.guardrailsai.com'
14+
15+
runs:
16+
using: "composite"
17+
steps:
18+
- name: Checkout "Validator" Repository
19+
uses: actions/checkout@v3
20+
with:
21+
path: 'validator'
22+
23+
- name: Checkout "Action" repository
24+
uses: actions/checkout@v3
25+
with:
26+
token: ${{ inputs.github_token }}
27+
repository: guardrails-ai/shared-ci
28+
ref: main
29+
path: shared-ci-scripts
30+
sparse-checkout: |
31+
.github
32+
33+
- name: Set up Python
34+
uses: actions/setup-python@v4
35+
with:
36+
python-version: '3.10'
37+
38+
- name: Install Twine & Build
39+
shell: bash
40+
run: |
41+
python -m pip install --upgrade pip
42+
pip install twine build toml
43+
44+
- name: Create .pypirc
45+
shell: bash
46+
run: |
47+
touch ~/.pypirc
48+
echo "[distutils]" >> ~/.pypirc
49+
echo "index-servers =" >> ~/.pypirc
50+
echo " private-repository" >> ~/.pypirc
51+
echo "" >> ~/.pypirc
52+
echo "[private-repository]" >> ~/.pypirc
53+
echo "repository = ${{ inputs.pypi_repository_url }}" >> ~/.pypirc
54+
echo "username = __token__" >> ~/.pypirc
55+
echo "password = ${{ inputs.guardrails_token }}" >> ~/.pypirc
56+
57+
- name: Move CI Scripts to Validator
58+
shell: bash
59+
run: |
60+
mv shared-ci-scripts/.github/actions/validator_pypi_publish/add_build_prefix.py ./validator/__shared_ci_script__.py
61+
62+
- name: Add Package Prefix
63+
shell: bash
64+
run: |
65+
cd validator
66+
python __shared_ci_script__.py ./pyproject.toml
67+
echo "===== pyproject.toml file after edits ===="
68+
cat ./pyproject.toml
69+
echo "=========================================="
70+
71+
- name: Build & Upload
72+
shell: bash
73+
run: |
74+
cd validator
75+
python -m build
76+
twine upload dist/* -u __token__ -p ${{ inputs.guardrails_token }} -r private-repository
77+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import toml
2+
import re
3+
import sys
4+
5+
6+
def add_package_name_prefix(pyproject_path):
7+
with open(pyproject_path, "r") as f:
8+
content = f.read()
9+
10+
try:
11+
toml_data = toml.loads(content)
12+
except Exception as e:
13+
print(f"Failed to parse {pyproject_path}: {e}")
14+
sys.exit(1)
15+
16+
existing_name = toml_data.get("project", {}).get("name")
17+
if not existing_name:
18+
print("Could not find the 'project.name' in pyproject.toml.")
19+
sys.exit(1)
20+
21+
new_name = f"guardrails-ai-validator-{existing_name}"
22+
23+
updated_content = re.sub(
24+
rf'(^name\s*=\s*")({re.escape(existing_name)})(")',
25+
rf"\1{new_name}\3",
26+
content,
27+
flags=re.MULTILINE,
28+
)
29+
30+
with open(pyproject_path, "w") as f:
31+
f.write(updated_content)
32+
33+
print(f"Updated project name to '{new_name}' in {pyproject_path}")
34+
35+
36+
if __name__ == "__main__":
37+
add_package_name_prefix("pyproject.toml")

0 commit comments

Comments
 (0)