Update Firmware Redirects #10
This file contains hidden or 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: Update Firmware Redirects | |
| on: | |
| # Run every hour to check for new releases | |
| schedule: | |
| - cron: '0 * * * *' | |
| # Allow manual triggering | |
| workflow_dispatch: | |
| # Can be triggered by ESPresense repo on release | |
| repository_dispatch: | |
| types: [new-release] | |
| jobs: | |
| update-redirects: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Get latest ESPresense release | |
| id: release | |
| run: | | |
| # Get latest release info from ESPresense repo | |
| RELEASE_DATA=$(curl -s https://api.github.com/repos/ESPresense/ESPresense/releases/latest) | |
| TAG=$(echo "$RELEASE_DATA" | jq -r '.tag_name') | |
| echo "Latest release tag: $TAG" | |
| echo "tag=$TAG" >> $GITHUB_OUTPUT | |
| # Get list of binary assets | |
| echo "$RELEASE_DATA" | jq -r '.assets[] | select(.name | endswith(".bin")) | .name' > /tmp/binaries.txt | |
| cat /tmp/binaries.txt | |
| - name: Check if update is needed | |
| id: check | |
| run: | | |
| TAG="${{ steps.release.outputs.tag }}" | |
| NEEDS_UPDATE=false | |
| # Check if _redirects file exists and contains the current tag | |
| if [ -f "_redirects" ]; then | |
| if ! grep -q "download/$TAG/" "_redirects"; then | |
| echo "_redirects file needs update to $TAG" | |
| NEEDS_UPDATE=true | |
| fi | |
| else | |
| echo "_redirects file does not exist" | |
| NEEDS_UPDATE=true | |
| fi | |
| echo "needs_update=$NEEDS_UPDATE" >> $GITHUB_OUTPUT | |
| - name: Update _redirects file | |
| if: steps.check.outputs.needs_update == 'true' | |
| run: | | |
| TAG="${{ steps.release.outputs.tag }}" | |
| # Create the _redirects file header | |
| cat > "_redirects" << 'EOF' | |
| # ESPresense Firmware Redirects | |
| # These redirects are automatically updated by GitHub Actions when new releases are published. | |
| # Cloudflare Pages processes this file to create HTTP 302 redirects. | |
| EOF | |
| # Remove leading whitespace from heredoc | |
| sed -i 's/^ //' "_redirects" | |
| # Add redirect rules for each binary | |
| while IFS= read -r binary; do | |
| echo "/releases/latest-any/download/${binary} https://github.com/ESPresense/ESPresense/releases/download/${TAG}/${binary} 302" >> "_redirects" | |
| done < /tmp/binaries.txt | |
| echo "Updated _redirects file to point to $TAG" | |
| cat "_redirects" | |
| - name: Commit and push changes | |
| if: steps.check.outputs.needs_update == 'true' | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add _redirects | |
| git commit -m "Update firmware redirects to ${{ steps.release.outputs.tag }}" | |
| git push |