From 754c05f9caf30a044b262f42daeedf9f487217be Mon Sep 17 00:00:00 2001 From: petscheit Date: Tue, 3 Sep 2024 17:46:07 +0200 Subject: [PATCH 1/4] feat: add cairo-program-registry action --- .github/workflows/add-to-registry.yml | 125 ++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 .github/workflows/add-to-registry.yml diff --git a/.github/workflows/add-to-registry.yml b/.github/workflows/add-to-registry.yml new file mode 100644 index 00000000..e1d788da --- /dev/null +++ b/.github/workflows/add-to-registry.yml @@ -0,0 +1,125 @@ +name: Add compile program to registry +on: + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup Scarb + uses: software-mansion/setup-scarb@v1 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + + - name: Cache Python environment + uses: actions/cache@v3 + with: + path: | + ~/.cache/pip + venv + key: ${{ runner.os }}-python-${{ hashFiles('**/requirements.txt') }}-${{ hashFiles('tools/make/setup.sh') }} + restore-keys: | + ${{ runner.os }}-python-${{ hashFiles('**/requirements.txt') }}-${{ hashFiles('tools/make/setup.sh') }} + ${{ runner.os }}-python- + + - name: Install Dependencies + run: make setup + - name: Compile Cairo program + run: | + source venv/bin/activate + mkdir -p build + + # Function to pad hash to 32 bytes + pad_hash() { + # Remove 0x prefix if present + hash=${1#0x} + # Pad to 64 characters (32 bytes) + printf "0x%064s" "$hash" | tr ' ' '0' + } + + # First program (frequently updated) + cairo-compile --cairo_path="packages/eth_essentials" "src/hdp.cairo" --output build/hdp.json + HDP_HASH=$(cairo-hash-program --program build/hdp.json) + HDP_HASH=$(pad_hash $HDP_HASH) + echo "HDP_HASH=$HDP_HASH" >> $GITHUB_ENV + + # Second program (infrequently updated) + cairo-compile --cairo_path="packages/eth_essentials" "src/contract_dry_run.cairo" --output build/contract_dry_run.json + CONTRACT_DRY_RUN_HASH=$(cairo-hash-program --program build/contract_dry_run.json) + CONTRACT_DRY_RUN_HASH=$(pad_hash $CONTRACT_DRY_RUN_HASH) + echo "CONTRACT_DRY_RUN_HASH=$CONTRACT_DRY_RUN_HASH" >> $GITHUB_ENV + + - name: Checkout compilation storage repo + uses: actions/checkout@v2 + with: + repository: petscheit/cairo-program-registry-new + token: ${{ secrets.CAIRO_PROGRAM_REGISTRY_PAT }} + path: cairo-compilations + + - name: Store compilations and update changelog + run: | + cd cairo-compilations + + # Check if first program (HDP) already exists + HDP_UPDATED=false + if [ ! -d "${{ env.HDP_HASH }}" ]; then + mkdir -p ${{ env.HDP_HASH }} + cp ../build/hdp.json ${{ env.HDP_HASH }}/ + HDP_UPDATED=true + fi + + # Store second program (only if folder doesn't exist) + CONTRACT_DRY_RUN_UPDATED=false + if [ ! -d "${{ env.CONTRACT_DRY_RUN_HASH }}" ]; then + mkdir -p ${{ env.CONTRACT_DRY_RUN_HASH }} + cp ../build/contract_dry_run.json ${{ env.CONTRACT_DRY_RUN_HASH }}/ + CONTRACT_DRY_RUN_UPDATED=true + fi + + # Update README with changelog only if there are updates + if [ "$HDP_UPDATED" = true ] || [ "$CONTRACT_DRY_RUN_UPDATED" = true ]; then + DATE=$(date +"%Y-%m-%d") + COMMIT_URL="https://github.com/${{ github.repository }}/commit/${{ github.sha }}" + + if [ ! -f README.md ]; then + echo "# Changelog" > README.md + echo "" >> README.md + fi + + { + echo "## Updates on $DATE" + echo "" + if [ "$HDP_UPDATED" = true ]; then + echo "### HDP Program" + echo "- **Hash:** \`${{ env.HDP_HASH }}\`" + echo "- [View commit]($COMMIT_URL)" + echo "" + fi + if [ "$CONTRACT_DRY_RUN_UPDATED" = true ]; then + echo "### Contract Dry Run" + echo "- **Hash:** \`${{ env.CONTRACT_DRY_RUN_HASH }}\`" + echo "- [View commit]($COMMIT_URL)" + echo "" + fi + echo "$(cat README.md)" + } > README.md.tmp + mv README.md.tmp README.md + + # Commit and push if there are changes + git config user.name github-actions + git config user.email github-actions@github.com + git add . + git commit -m "Add compilation(s) for hash(es) ${{ env.HDP_HASH }} and/or ${{ env.CONTRACT_DRY_RUN_HASH }} and update changelog" + git push + else + echo "No new compilations to store or changelog updates." + fi + From de4ad9edae27b876f5fbd3bd5fb8c80586f43b01 Mon Sep 17 00:00:00 2001 From: petscheit Date: Tue, 3 Sep 2024 17:46:47 +0200 Subject: [PATCH 2/4] feat: add change detection and version bump for pypi package --- .bumpversion.cfg | 6 ++++ .github/workflows/merge.yml | 55 +++++++++++++++++++++++++++++++------ setup.py | 12 ++++---- version.py | 1 + 4 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 .bumpversion.cfg create mode 100644 version.py diff --git a/.bumpversion.cfg b/.bumpversion.cfg new file mode 100644 index 00000000..73791e60 --- /dev/null +++ b/.bumpversion.cfg @@ -0,0 +1,6 @@ +[bumpversion] +current_version = 0.0.7 +commit = True +tag = True + +[bumpversion:file:version.py] \ No newline at end of file diff --git a/.github/workflows/merge.yml b/.github/workflows/merge.yml index 173109a6..4c7c2444 100644 --- a/.github/workflows/merge.yml +++ b/.github/workflows/merge.yml @@ -1,22 +1,26 @@ -name: Build, Push Docker Image, and Deploy Python Package +name: Build Docker and Push Python Package on: push: branches: - main - pull_request: - branches: - - main jobs: build-and-push: runs-on: ubuntu-latest permissions: - contents: read + contents: write id-token: write steps: - name: Checkout code uses: actions/checkout@v2 + with: + fetch-depth: 0 # Fetch all history and tags + + - name: Fetch all branches + run: | + git fetch --all + git fetch --tags - name: Log in to Docker Hub if: github.event_name == 'push' && github.ref == 'refs/heads/main' @@ -61,19 +65,54 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install setuptools wheel twine + pip install setuptools wheel twine bumpversion + + - name: Check for package changes + run: | + if [ "${{ github.event_name }}" = "pull_request" ]; then + # Fetch base branch to compare with PR branch + git fetch origin ${{ github.base_ref }}:${{ github.base_ref }} + CHANGED_FILES=$(git diff --name-only ${{ github.base_ref }}...HEAD) + else + # Compare current commit with the previous commit + CHANGED_FILES=$(git diff --name-only HEAD^ HEAD) + fi + echo "Changed files: $CHANGED_FILES" + if echo "$CHANGED_FILES" | grep -qE '(tools/|contract_bootloader/|compiled_contracts/|setup.py)'; then + echo "CHANGES_DETECTED=true" >> $GITHUB_ENV + else + echo "CHANGES_DETECTED=false" >> $GITHUB_ENV + fi + echo "Changes detected: $CHANGES_DETECTED" + + - name: Debug output + run: | + echo "Event name: ${{ github.event_name }}" + echo "GitHub ref: ${{ github.ref }}" + echo "Changes detected: ${{ env.CHANGES_DETECTED }}" + + - name: Bump version + if: env.CHANGES_DETECTED == 'true' && github.event_name == 'push' && github.ref == 'refs/heads/main' + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + bumpversion patch --allow-dirty + git push --tags + git push - name: Build Python package + if: steps.check_changes.outputs.changes_detected == 'true' run: | python setup.py sdist bdist_wheel - name: Check PyPI package (dry run) + if: steps.check_changes.outputs.changes_detected == 'true' run: | twine check dist/* echo "Package check completed. In a real publish, the package would be uploaded to PyPI." - name: Publish package to PyPI - if: github.event_name == 'push' && github.ref == 'refs/heads/main' + if: steps.check_changes.outputs.changes_detected == 'true' && github.event_name == 'push' && github.ref == 'refs/heads/main' uses: pypa/gh-action-pypi-publish@release/v1 with: - password: ${{ secrets.PYPI_API_TOKEN }} + password: ${{ secrets.PYPI_API_TOKEN }} \ No newline at end of file diff --git a/setup.py b/setup.py index ae24394a..422e19c7 100644 --- a/setup.py +++ b/setup.py @@ -1,20 +1,20 @@ from setuptools import setup from setuptools.command.install import install - from pathlib import Path +# Read version from version.py +version = {} +with open("version.py") as fp: + exec(fp.read(), version) + this_directory = Path(__file__).parent long_description = (this_directory / "package.md").read_text() - class PostInstallCommand(install): """Custom post-installation for installation mode.""" - def run(self): - # Run the standard install process first install.run(self) - # Read the requirements from the requirements.txt file with open("tools/make/requirements.txt") as requirements_file: requirements = requirements_file.read().splitlines() @@ -23,7 +23,7 @@ def run(self): name="hdp-cairo-dev", long_description=long_description, long_description_content_type="text/markdown", - version="0.0.7", + version=version['__version__'], packages=[ "tools", "contract_bootloader", diff --git a/version.py b/version.py new file mode 100644 index 00000000..7d53ea32 --- /dev/null +++ b/version.py @@ -0,0 +1 @@ +__version__ = "0.0.7" \ No newline at end of file From 8fd36dc00ec1ff8bf3e922081a278f8c4a5f759c Mon Sep 17 00:00:00 2001 From: petscheit Date: Tue, 3 Sep 2024 18:00:37 +0200 Subject: [PATCH 3/4] chore: fmt --- setup.py | 5 ++++- version.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 422e19c7..c3b4138c 100644 --- a/setup.py +++ b/setup.py @@ -10,11 +10,14 @@ this_directory = Path(__file__).parent long_description = (this_directory / "package.md").read_text() + class PostInstallCommand(install): """Custom post-installation for installation mode.""" + def run(self): install.run(self) + # Read the requirements from the requirements.txt file with open("tools/make/requirements.txt") as requirements_file: requirements = requirements_file.read().splitlines() @@ -23,7 +26,7 @@ def run(self): name="hdp-cairo-dev", long_description=long_description, long_description_content_type="text/markdown", - version=version['__version__'], + version=version["__version__"], packages=[ "tools", "contract_bootloader", diff --git a/version.py b/version.py index 7d53ea32..6526deb4 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -__version__ = "0.0.7" \ No newline at end of file +__version__ = "0.0.7" From bf3fe4627954811007fa013acd46a290a403eb0e Mon Sep 17 00:00:00 2001 From: petscheit Date: Wed, 4 Sep 2024 12:17:39 +0200 Subject: [PATCH 4/4] chore: change compile program name for legacy compatibility --- .github/workflows/add-to-registry.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/add-to-registry.yml b/.github/workflows/add-to-registry.yml index e1d788da..d0536c8f 100644 --- a/.github/workflows/add-to-registry.yml +++ b/.github/workflows/add-to-registry.yml @@ -72,7 +72,7 @@ jobs: HDP_UPDATED=false if [ ! -d "${{ env.HDP_HASH }}" ]; then mkdir -p ${{ env.HDP_HASH }} - cp ../build/hdp.json ${{ env.HDP_HASH }}/ + cp ../build/hdp.json ${{ env.HDP_HASH }}/program.json HDP_UPDATED=true fi @@ -80,7 +80,7 @@ jobs: CONTRACT_DRY_RUN_UPDATED=false if [ ! -d "${{ env.CONTRACT_DRY_RUN_HASH }}" ]; then mkdir -p ${{ env.CONTRACT_DRY_RUN_HASH }} - cp ../build/contract_dry_run.json ${{ env.CONTRACT_DRY_RUN_HASH }}/ + cp ../build/contract_dry_run.json ${{ env.CONTRACT_DRY_RUN_HASH }}/program.json CONTRACT_DRY_RUN_UPDATED=true fi