From 06eb4c4fecd3917c56fc612aa72ba175dce87492 Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Wed, 9 Aug 2023 19:28:14 -0700 Subject: [PATCH] add build script --- .gitea/workflows/build_changed.yml | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .gitea/workflows/build_changed.yml diff --git a/.gitea/workflows/build_changed.yml b/.gitea/workflows/build_changed.yml new file mode 100644 index 0000000..c4558db --- /dev/null +++ b/.gitea/workflows/build_changed.yml @@ -0,0 +1,59 @@ +stages: + - build_and_upload_changed_packages + +build_and_upload_changed_packages: + image: python:3.9 + stage: build_and_upload_changed_packages + script: + - pip install -U pip + - pip install twine build toml + # Determine the list of changed files using git + - git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA > changed_files.txt + + # Loop through each package directory, check if version has changed, build if changed, and then upload + - | + for package_dir in ./*; do + if [ -d "$package_dir" ]; then + package_name=$(basename $package_dir) + if grep -q "$package_name" changed_files.txt; then + echo "Checking version for: $package_name" + if [ -f "$package_dir/pyproject.toml" ]; then + current_version=$(python -c "import toml; data = toml.load('$package_dir/pyproject.toml'); print(data['project']['version']) if 'project' in data and 'version' in data['project'] else print(None)") + echo "Current version: $current_version" + if [ "$current_version" = "None" ]; then + current_version=$(grep -oP '(?<=version=\")[^\"]+' $package_dir/setup.py) + last_version="$(git cat-file -p "$CI_COMMIT_BEFORE_SHA:$package_dir/setup.py" | grep -oP '(?<=version=\")[^\"]+')" + else + last_version=$(git cat-file -p "$CI_COMMIT_BEFORE_SHA:$package_dir/pyproject.toml" | python -c "import sys, toml; data = toml.loads(sys.stdin.read()); print(data['project']['version']) if 'project' in data and 'version' in data['project'] else print(None)") + fi + echo "Previous version: $last_version" + elif [ -f "$package_dir/setup.py" ]; then + current_version=$(grep -oP '(?<=version=\")[^\"]+' $package_dir/setup.py) + last_version=$(git cat-file "$CI_COMMIT_BEFORE_SHA:$package_dir/setup.py" | grep -oP '(?<=version=\")[^\"]+') + else + echo "No recognizable version information in $package_name. Skipping." + continue + fi + if [ "$current_version" != "$last_version" ]; then + echo "Version updated... proceeding with build" + # Create ~/.config/pip directory if it doesn't exist + mkdir -p ~/.config/pip + # Create ~/.config/pip/pip.conf file with specific configuration + echo "[global]" > ~/.config/pip/pip.conf + echo "timeout = 60" >> ~/.config/pip/pip.conf + echo "extra-index-url = https://sudacode:$GITEA_TOKEN@gitea.suda.codes/api/packages/sudacode/pypi/simple" >> ~/.config/pip/pip.conf + + echo "Building and uploading $package_name (version $current_version) as it has changes..." + cd $package_dir + python -m build + twine upload --repository-url https://gitea.suda.codes/api/packages/sudacode/pypi --username sudacode --password $GITEA_TOKEN dist/* + cd - + else + echo "Skipping $package_name as the version has not changed." + fi + fi + fi + done + only: + - pushes + - merge_requests