Skip to content

Commit

Permalink
action
Browse files Browse the repository at this point in the history
  • Loading branch information
traderpedroso committed May 31, 2024
1 parent b118994 commit 9bfd692
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/scripts/create_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from github import Github
import os
import re

# Caminho para o diretório atual do script
script_dir = os.path.dirname(os.path.realpath(__file__))

# Caminho para o setup.py relativo ao diretório do script
setup_path = os.path.join(script_dir, "../../setup.py")

# Leitura da versão do setup.py
with open(setup_path, "r") as f:
setup_contents = f.read()

match = re.search(r"# Version: ([^\n]+)", setup_contents)
if match:
setup_version = match.group(1)
else:
raise ValueError("Could not find version in setup.py")

# Acessa o repositório atual
g = Github(os.getenv("GITHUB_TOKEN"))
repo = g.get_repo(os.getenv("GITHUB_REPOSITORY"))

# Busca a última release
releases = repo.get_releases()
latest_release = next((release for release in releases), None)

# Se a versão do setup.py for diferente da última release, cria uma nova release
if latest_release is None or latest_release.tag_name != setup_version:
repo.create_git_ref(ref=f"refs/tags/{setup_version}", sha=os.getenv("GITHUB_SHA"))
repo.create_git_release(
tag=setup_version,
name=f"Release {setup_version}",
message="",
draft=False,
prerelease=False,
target_commitish=os.getenv("GITHUB_SHA"),
)
47 changes: 47 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Create Release and Upload Python Package

on:
push:
paths:
- "setup.py"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install PyGithub
pip install twine
pip install build
- name: Create Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: python .github/scripts/create_release.py

- name: Build package
run: python -m build

- name: Check if version exists
run: |
python -m twine check dist/*
if python -m twine check dist/* | grep "already exists"; then
echo "Version already exists, skipping publish step"
exit 0
fi
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

0 comments on commit 9bfd692

Please sign in to comment.