-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
162 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,149 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python | ||
# Poetry install: https://python-poetry.org/docs/cli/#install | ||
# https://stackoverflow.com/questions/77446605/running-python-poetry-unit-test-in-github-actions | ||
# Based on: | ||
# ArjanCodes https://www.youtube.com/watch?v=NMQwzI9hprg | ||
# https://github.com/ArjanCodes/examples/blob/main/2024/publish_pypi/release.yaml | ||
|
||
name: Python package | ||
name: release | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
types: [closed] # Run after PR is merged | ||
tags: | ||
- "v[0-9]+.[0-9]+.[0-9]+" | ||
- "v[0-9]+.[0-9]+.[0-9]+a[0-9]+" | ||
- "v[0-9]+.[0-9]+.[0-9]+b[0-9]+" | ||
- "v[0-9]+.[0-9]+.[0-9]+rc[0-9]+" | ||
|
||
env: | ||
PACKAGE_NAME: "celai-chatwoot" | ||
OWNER: "cel-ai" | ||
|
||
jobs: | ||
build: | ||
details: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
new_version: ${{ steps.release.outputs.new_version }} | ||
suffix: ${{ steps.release.outputs.suffix }} | ||
tag_name: ${{ steps.release.outputs.tag_name }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Extract tag and Details | ||
id: release | ||
run: | | ||
if [ "${{ github.ref_type }}" = "tag" ]; then | ||
TAG_NAME=${GITHUB_REF#refs/tags/} | ||
NEW_VERSION=$(echo $TAG_NAME | awk -F'-' '{print $1}') | ||
SUFFIX=$(echo $TAG_NAME | grep -oP '[a-z]+[0-9]+' || echo "") | ||
echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT" | ||
echo "suffix=$SUFFIX" >> "$GITHUB_OUTPUT" | ||
echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT" | ||
echo "Version is $NEW_VERSION" | ||
echo "Suffix is $SUFFIX" | ||
echo "Tag name is $TAG_NAME" | ||
else | ||
echo "No tag found" | ||
exit 1 | ||
fi | ||
check_pypi: | ||
needs: details | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Fetch information from PyPI | ||
run: | | ||
response=$(curl -s https://pypi.org/pypi/${{ env.PACKAGE_NAME }}/json || echo "{}") | ||
latest_previous_version=$(echo $response | jq --raw-output "select(.releases != null) | .releases | keys_unsorted | last") | ||
if [ -z "$latest_previous_version" ]; then | ||
echo "Package not found on PyPI." | ||
latest_previous_version="0.0.0" | ||
fi | ||
echo "Latest version on PyPI: $latest_previous_version" | ||
echo "latest_previous_version=$latest_previous_version" >> $GITHUB_ENV | ||
- name: Compare versions and exit if not newer | ||
run: | | ||
NEW_VERSION=${{ needs.details.outputs.new_version }} | ||
LATEST_VERSION=$latest_previous_version | ||
if [ "$(printf '%s\n' "$LATEST_VERSION" "$NEW_VERSION" | sort -rV | head -n 1)" != "$NEW_VERSION" ] || [ "$NEW_VERSION" == "$LATEST_VERSION" ]; then | ||
echo "The new version $NEW_VERSION is not greater than the latest version $LATEST_VERSION on PyPI." | ||
exit 1 | ||
else | ||
echo "The new version $NEW_VERSION is greater than the latest version $LATEST_VERSION on PyPI." | ||
fi | ||
setup_and_build: | ||
needs: [details, check_pypi] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.11" | ||
|
||
- name: Install Poetry | ||
run: | | ||
curl -sSL https://install.python-poetry.org | python3 - | ||
echo "$HOME/.local/bin" >> $GITHUB_PATH | ||
- name: Set project version with Poetry | ||
run: | | ||
poetry version ${{ needs.details.outputs.new_version }} | ||
- name: Install dependencies | ||
run: poetry install --sync --no-interaction | ||
|
||
- name: Build source and wheel distribution | ||
run: | | ||
poetry build | ||
- name: Upload artifacts | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: dist | ||
path: dist/ | ||
|
||
pypi_publish: | ||
name: Upload release to PyPI | ||
needs: [setup_and_build, details] | ||
runs-on: ubuntu-latest | ||
environment: release | ||
environment: | ||
name: release | ||
permissions: | ||
# IMPORTANT: this permission is mandatory for trusted publishing | ||
id-token: write | ||
contents: write # required for commiting version changes | ||
steps: | ||
- name: Download artifacts | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: dist | ||
path: dist/ | ||
|
||
- name: Publish distribution to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1.9 | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: ["3.11"] | ||
|
||
github_release: | ||
name: Create GitHub Release | ||
needs: [setup_and_build, details] | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install flake8 pytest bump2version | ||
pip install poetry | ||
poetry install --with dev | ||
- name: Set up Git credentials for pushing | ||
run: git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Configure Git user | ||
run: | | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
git config --global user.name "GitHub Actions" | ||
- name: Auto-increment version | ||
run: | | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
git config --global user.name "GitHub Actions" | ||
bump2version patch --allow-dirty | ||
poetry lock --no-update | ||
cat pyproject.toml | grep version | ||
- name: Commit and Push changes | ||
run: | | ||
git add pyproject.toml | ||
git add poetry.lock | ||
git commit -m "Increment version [skip ci]" || echo "No changes to commit" | ||
git push origin main | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
|
||
- name: Lint with flake8 | ||
run: | | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics | ||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide | ||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test with pytest | ||
env: | ||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | ||
run: | | ||
poetry run pytest | ||
- name: Build package | ||
run: | | ||
poetry build | ||
- name: Publish package | ||
uses: pypa/gh-action-pypi-publish@release/v1.9 | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Download artifacts | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: dist | ||
path: dist/ | ||
|
||
- name: Create GitHub Release | ||
id: create_release | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
run: | | ||
gh release create ${{ needs.details.outputs.tag_name }} dist/* --title ${{ needs.details.outputs.tag_name }} --generate-notes | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters