Skip to content

chore(git): Add logging and user feedback for test upload #196 #34

chore(git): Add logging and user feedback for test upload #196

chore(git): Add logging and user feedback for test upload #196 #34

Workflow file for this run

name: Release and Deploy
on:
pull_request:
branches: [main]
paths-ignore:
- "**.md"
- "docs/**"
workflow_dispatch:
jobs:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get branch name
id: get_branch_name
shell: bash
run: |
echo "Version hex dump:"
grep "version" version.toml | awk -F'"' '{print $2}' | xxd
echo "📖 Reading version from version.toml..."
version=$(grep "__version__" version.toml | awk -F'"' '{print $2}' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "📌 Found version: '${version}'"
branch_name="release-${version}"
echo "🔄 Creating branch name: ${branch_name}"
echo "BRANCH_NAME=${branch_name}" >> $GITHUB_ENV
echo "✅ Branch Name $branch_name created successfully"
- name: Check if branch exists
run: |
echo "🔍 Debug: Full branch name to check: 'refs/heads/${BRANCH_NAME}'"
branch_check=$(git ls-remote --heads origin "refs/heads/${BRANCH_NAME}")
exit_code=$?
echo "🔍 Debug: git ls-remote output: '${branch_check}'"
echo "🔍 Debug: git ls-remote exit code: ${exit_code}"
echo "🔍 Checking if branch ${{ env.BRANCH_NAME }} already exists..."
if [ -n "${branch_check}" ]; then
echo "❌ Branch ${{ env.BRANCH_NAME }} already exists!"
echo "::error::Branch ${{ env.BRANCH_NAME }} already exists!" >> $GITHUB_STEP_SUMMARY
echo "This likely means one of:" >> $GITHUB_STEP_SUMMARY
echo " 1. This version was already released and potentially deployed" >> $GITHUB_STEP_SUMMARY
echo " 2. The version number in version.toml needs to be incremented" >> $GITHUB_STEP_SUMMARY
echo " 3. An old release branch needs to be cleaned up" >> $GITHUB_STEP_SUMMARY
exit 1
fi
echo "✅ Branch does not exist, proceeding with creation"
- name: Create release branch
run: |
echo "🔄 Checking out main branch..."
git checkout main
echo "🌱 Creating new branch: ${{ env.BRANCH_NAME }}"
git checkout -b ${{ env.BRANCH_NAME }}
echo "⬆️ Pushing branch to origin..."
git push origin ${{ env.BRANCH_NAME }}
echo "✅ Branch successfully pushed to origin"
- name: Post creation summary
run: |
echo "📝 Generating creation summary..."
echo "✅ Branch ${{ env.BRANCH_NAME }} created successfully"
echo "Created from: main"
echo "Created by: ${{ github.actor }}"
echo "Created at: $(date '+%Y-%m-%d %H:%M:%S')"
echo "📝 Adding summary to job output..."
echo "✅ Branch ${{ env.BRANCH_NAME }} created successfully" >> $GITHUB_STEP_SUMMARY
echo "Created from: main" >> $GITHUB_STEP_SUMMARY
echo "Created by: ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY
echo "Created at: $(date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_STEP_SUMMARY
build-and-deploy:
needs: create-release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ env.BRANCH_NAME }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
echo "✅ Dependencies installed successfully" >> $GITHUB_STEP_SUMMARY
- name: Build package
id: build
run: |
echo "🔨 Starting package build..."
echo "📦 Building package..." >> $GITHUB_STEP_SUMMARY
# Capture build output
BUILD_OUTPUT=$(python -m build 2>&1)
BUILD_STATUS=$?
echo "$BUILD_OUTPUT"
# Log build results
if [ $BUILD_STATUS -eq 0 ]; then
echo "✅ Package built successfully" >> $GITHUB_STEP_SUMMARY
echo "📂 Built files:" >> $GITHUB_STEP_SUMMARY
ls -l dist/ >> $GITHUB_STEP_SUMMARY
else
echo "❌ Package build failed" >> $GITHUB_STEP_SUMMARY
echo "Error output:" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "$BUILD_OUTPUT" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Check distribution
id: check
run: |
echo "🔍 Checking distribution files..."
echo "🔍 Checking distribution files..." >> $GITHUB_STEP_SUMMARY
# Run twine check with detailed output
CHECK_OUTPUT=$(twine check dist/* 2>&1)
CHECK_STATUS=$?
echo "$CHECK_OUTPUT"
# Log check results
if [ $CHECK_STATUS -eq 0 ]; then
echo "✅ Distribution files check passed" >> $GITHUB_STEP_SUMMARY
else
echo "❌ Distribution files check failed" >> $GITHUB_STEP_SUMMARY
echo "Error details:" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "$CHECK_OUTPUT" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Upload to TestPyPI
id: upload
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
TWINE_REPOSITORY_URL: ${{ secrets.TEST_PYPI_REPOSITORY_URL }}
run: |
echo "⬆️ Starting upload to TestPyPI..."
echo "⬆️ Uploading to TestPyPI..." >> $GITHUB_STEP_SUMMARY
# Run twine upload with verbose output
UPLOAD_OUTPUT=$(twine upload --verbose --repository-url ${{ secrets.TEST_PYPI_REPOSITORY_URL }} dist/* 2>&1)
UPLOAD_STATUS=$?
echo "$UPLOAD_OUTPUT"
# Extract package version for summary
VERSION=$(python setup.py --version 2>/dev/null || grep -m1 "version" pyproject.toml | cut -d'"' -f2)
# Log upload results
if [ $UPLOAD_STATUS -eq 0 ]; then
echo "✅ Upload successful" >> $GITHUB_STEP_SUMMARY
echo "📦 Package version: $VERSION" >> $GITHUB_STEP_SUMMARY
echo "🔗 View at: https://test.pypi.org/project/django-tag-me/$VERSION/" >> $GITHUB_STEP_SUMMARY
else
echo "❌ Upload failed" >> $GITHUB_STEP_SUMMARY
echo "Error details:" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
echo "$UPLOAD_OUTPUT" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
# Check for specific error types
if echo "$UPLOAD_OUTPUT" | grep -q "403 Forbidden"; then
echo "🔑 Authentication Error: Please check your TestPyPI API token" >> $GITHUB_STEP_SUMMARY
echo "Make sure TEST_PYPI_API_TOKEN secret is correctly set in repository settings" >> $GITHUB_STEP_SUMMARY
fi
if echo "$UPLOAD_OUTPUT" | grep -q "400 Bad Request"; then
echo "⚠️ Version Error: This version might already exist on TestPyPI" >> $GITHUB_STEP_SUMMARY
echo "Try incrementing the version number in your project" >> $GITHUB_STEP_SUMMARY
fi
exit 1
fi
cleanup-old-branches:
needs: build-and-deploy
runs-on: ubuntu-latest
env:
ROLLBACK_NUMBER: ${{ vars.VERSION_ROLLBACK_NUMBER }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Cleanup old release branches
run: |
echo "🔍 Starting cleanup process..."
echo "📊 Rollback number set to: $ROLLBACK_NUMBER"
echo "📝 Getting list of release branches..."
release_branches=$(git for-each-ref --sort=-creatordate --format '%(refname:short)' refs/remotes/origin | grep '^origin/release-' | sed 's|origin/||')
echo "📋 Converting branch list to array..."
mapfile -t branch_array <<< "$release_branches"
# Print all found branches
echo "📊 Found release branches (sorted by date, newest first):"
for branch in "${branch_array[@]}"; do
echo " - $branch"
done
# Calculate how many branches to keep
keep_count=$ROLLBACK_NUMBER
total_branches=${#branch_array[@]}
if [ $total_branches -gt $keep_count ]; then
echo "🔄 Found $total_branches release branches, keeping newest $keep_count"
for ((i=keep_count; i<total_branches; i++)); do
branch="${branch_array[i]}"
echo " ❌ Deleting: $branch"
git push origin --delete "$branch"
if [ $? -eq 0 ]; then
echo " ✅ Successfully deleted $branch"
echo " ✅ Successfully deleted $branch" >> $GITHUB_STEP_SUMMARY
else
echo " ❌ Failed to delete $branch"
echo " ❌ Failed to delete $branch" >> $GITHUB_STEP_SUMMARY
fi
done
else
echo "✅ Only found $total_branches release branches, no cleanup needed"
echo "✅ Only found $total_branches release branches, no cleanup needed" >> $GITHUB_STEP_SUMMARY
fi
echo "🎉 Cleanup process completed!"
echo "🎉 Cleanup process completed!" >> $GITHUB_STEP_SUMMARY
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}