pet-lang-pypi #2
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
name: pet-lang-pypi | |
on: | |
push: | |
tags: | |
- 'v*' | |
pull_request: | |
branches: | |
- main | |
workflow_dispatch: | |
env: | |
SETUP_FILE: setup.py | |
TAG_FILE: version.txt | |
IS_RELEASE: is_release.txt | |
MATRIX_VERSION_PUBLISHING: "3.10" | |
SOURCE_CODE_PATH: pet_lang/pet_lang.py | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
python-version: ["3.9", "3.10", "3.11", "3.12"] | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ matrix.python-version }} | |
- name: Get Latest Tag | |
id: get_latest_tag | |
run: | | |
echo "Getting tags" | |
git fetch --tags # Make sure to fetch the tags first | |
echo "Updated the list" | |
latest_tag=$(git tag --list "v*" | sort -rV | head -n 1) | |
echo "Latest tag = $latest_tag" | |
if [ "$latest_tag" == "" ]; then | |
echo "No tags found matching 'v*'." | |
latest_tag="v1.0.0" | |
echo "Using a default tag: $latest_tag" | |
else | |
echo "Latest Tag: $latest_tag" | |
fi | |
echo "$latest_tag" > ${{env.TAG_FILE}} | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
echo "new_tag = $latest_tag" | |
echo "env.new_tag = ${{env.tag_file}}" | |
echo "cat env.new_tag = $(cat ${{env.TAG_FILE}})" | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
- name: Check if Latest Tag is a Release | |
id: is_release | |
run: | | |
latest_tag="$(cat ${{env.TAG_FILE}})" | |
is_release="false" | |
latest_updated_tag=$(git tag --list "v*" | sort -rV | head -n 1) | |
if [[ $latest_updated_tag == $latest_tag ]]; then | |
is_release="true" | |
fi | |
echo "$is_release" > ${{env.IS_RELEASE}} | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
echo "Is_release = $is_release" | |
echo "env.Is_release = ${{env.is_release}}" | |
echo "cat env.Is_release = $(cat ${{env.is_release}})" | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
- name: Increment Tag Version | |
id: increment_tag | |
run: | | |
is_release="$(cat ${{env.IS_RELEASE}})" | |
latest_tag="$(cat ${{env.TAG_FILE}})" | |
echo "ls = $(ls)" | |
echo "is_release = $is_release" | |
echo "latest_tag = $latest_tag" | |
if [[ $is_release == "true" ]]; then | |
# Parse the latest tag and increment the version component you want | |
# Example: Convert "v1.2.3" to "v1.2.4" | |
new_tag="${latest_tag/v/}" # Remove the leading "v" | |
echo "new_tag = $new_tag" | |
IFS='.' read -ra version <<< "$new_tag" | |
echo "declare -p version = $(declare -p $version)" | |
echo "declare -p version[0] = $(declare -p ${version[0]})" | |
echo "declare -p version[1] = $(declare -p ${version[1]})" | |
echo "declare -p version[2] = $(declare -p ${version[2]})" | |
echo "version[2]+1 = $(expr "${version[2]}" + 1)" | |
echo "version[*] = ${version[*]}" | |
echo "version = ${version}" | |
echo "#version = ${#version}" | |
echo "version[0] = ${version[0]}" | |
echo "version[1] = ${version[1]}" | |
echo "version[2] = ${version[2]}" | |
version[2]=$(expr "${version[2]}" + 1) # Increment the third version component | |
echo "Version = ${version[*]}" | |
new_tag="v${version[0]}.${version[1]}.${version[2]}" | |
echo "New Tag: $new_tag" | |
echo "$new_tag" > ${{env.TAG_FILE}} | |
else | |
echo "Latest tag is not a release. Skipping tag increment." | |
fi | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
echo "new_tag = $new_tag" | |
echo "env.new_tag = ${{env.TAG_FILE}}" | |
echo "env.is_release = $(cat ${{env.IS_RELEASE}})" | |
echo "cat env.new_tag = $(cat ${{env.TAG_FILE}})" | |
echo "cat env.is_release = $(cat ${{env.IS_RELEASE}})" | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
- name: Update module version in source code and builder file | |
run: | | |
latest_tag="$(cat ${{env.TAG_FILE}})" | |
if [ -f ${{env.SETUP_FILE}} ] | |
then | |
echo "Updating version in ${{env.SETUP_FILE}}" | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
echo "Tag: $latest_tag" | |
latest_tag=${latest_tag/"v"/""} | |
echo "Tag: $latest_tag" | |
sed -i "s/version='.*'/version='$latest_tag'/g" ${{env.SETUP_FILE}} | |
sed -i "s/self.__version__ = '.*'/self.__version__ = '$latest_tag'/g" ${{env.SOURCE_CODE_PATH}} | |
fi | |
echo "Content: $(ls)" | |
echo "Content of file: $(cat ${{env.SETUP_FILE}})" | |
echo -e "\n\n\n\n\n\n\n\n\n" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
python -m pip install flake8 pytest | |
lib_packages=$(find ./*/ -name "requirements.txt" -type f) | |
first_requirement=$(echo $lib_packages | cut -d " " -f 1) | |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi | |
if [ ${#first_requirements} -gt 0 ]; then pip install -r $first_requirements.txt; fi | |
- 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 | |
run: | | |
pytest | |
- name: building the package | |
run: | | |
pip install build | |
python -m build | |
- name: Update author | |
run: | | |
git config --local user.name "${{secrets.USER_NAME_E}}" | |
git config --local user.email "${{secrets.USER_EMAIL_E}}" | |
# Add the following steps if you want to publish to PyPI | |
- name: Build and publish to PyPI | |
if: matrix.python-version == env.MATRIX_VERSION_PUBLISHING | |
run: | | |
python -m build | |
pip install twine | |
twine upload dist/* | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} |